deru-event-emitter
v1.1.2
Published
global listening events (analogue - Vue event bus)
Downloads
1
Readme
event-emitter
global listening events (analogue - Vue event bus)
(best for async logic)
Installation
npm i deru-event-emitter --save
Methods
| Method | Description | | ---- | ---- | | subscribe | subscribe to event | | emit | calling event |
Params
| Prop | type | Default | Description |
| ---- | ------------| -----| ---- |
| once | Boolean
| false
| bind event once and remove after calling |
How Usage?
Import Class with module
import EventEmitter from 'deru-event-emitter';
or require type
require EventEmitter = require('deru-event-emitter');
Use new instance of Class:
// main.js
export const emitter = new EventEmitter();
Example: async confirm modal actions
// component.js
import { emitter } from './main.js';
confirmAction() {
return new Promise(resolve => {
emitter.subscribe('confirm', res => {
return resolve(res);
})
})
}
async openModal() {
const confirm = await confirmAction();
if (confirm) {
// some logic
}
}
// modal.js
import { emitter } from './main.js';
confirmModal() {
emitter.emit('confirm', true);
}