https://github.com/grinat/vue-global-emitter
Global vue emmiter which realising pub/sub without and with pull
https://github.com/grinat/vue-global-emitter
vue
Last synced: about 1 month ago
JSON representation
Global vue emmiter which realising pub/sub without and with pull
- Host: GitHub
- URL: https://github.com/grinat/vue-global-emitter
- Owner: grinat
- License: mit
- Created: 2018-05-04T10:50:53.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T10:55:26.000Z (almost 3 years ago)
- Last Synced: 2025-09-13T07:31:35.451Z (2 months ago)
- Topics: vue
- Language: JavaScript
- Size: 303 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vue-global-emitter
Simple global vue emitter which realising pub/sub without and with pull for Vue.js
### Examples
[Open](https://grinat.github.io/vue-global-emitter/examples/) (see in /examples)
### Installation
```
npm install vue-global-emitter --save
```
#### Use without vue
```js
import {Emitter} from 'vue-global-emitter'
const em = new Emitter()
em.emit('my-event', {foo: 'bar'})
```
#### Use in vue
```js
import Vue from 'vue'
import VueGlobalEmitter from 'vue-global-emitter'
Vue.use(VueGlobalEmitter)
```
### Usage
Send event
```js
this.$emitter.emit('my-event', {foo: 'bar'})
```
Listen event
```js
this.$emitter.listen('my-event', data => console.log(data))
```
Send event with delivery
```js
let delay = 200
this.$emitter.emitWithDelivery('my-event-for-not-created-component', {foo: 'bar'}, delay)
```
Read
```js
this.$emitter.listen('my-event-for-not-created-component', response => {
// notify about read
response.received()
})
```
Mass subscribe/unsubscribe
```js
export default {
created () {
// create subsribe
this.subsGroup = this.$emitter.group(
this.$emitter.listen('im.socket', this.onSocketMessage),
this.$emitter.listen('foo', this.onFoo)
)
},
methods: {
...
},
destroyed () {
// unsubsribe all
this.subsGroup.unsubscribe()
}
}
```
### Recommended use way
```js
export default {
// create subsribe
created () {
this.messageSubs = this.$emitter.listen('im.socket', this.onSocketMessage)
},
methods: {
// handle message
onSocketMessage (data) {
}
},
destroyed () {
// dont forget unsubcribe
this.messageSubs.unsubscribe()
}
}
```
### Development
build:
```
npm i
npm run build
```
test:
```
npm run test
```