event-bus
Introduction
pub-sub.js is a ligtweight、none dependence lib, it supports event pulish、subscibe and offline subscibe.
Install
npm i super-pub-sub.js --save
Usage
Browser
<script src="../dist/event_bus.js"></script>
var e = new eventBus({
});
const handler = (params) => {
console.log(params)
}
//event subscripte
e.on('msg', handler);
//event distribute
e.emit('msg', 'recive a message');
//remove subscripte
e.off('msg', handler)
Node
const _event = require('super-pub-sub.js')
const e = new _event({}) //init
const handler = (params) => {
console.log(params)
}
//event subscripte
e.on('msg', handler);
//event distribute
e.emit('msg', 'recive a message');
//remove subscripte
e.off('msg', handler)
Api
on(type, cb ,offline, flag)
add handler functon
name | type | default | descriptin |
---|---|---|---|
type | String Number Boolean Expression | - | subject of pub-sub |
cb | Function | - | handler of pub-sub |
offline | Boolean | false | support offline subscibe? default:false. |
flag | Boolean | false | unshift or push handler in event queue(true: unshift, false: push) |
once(type, cb, offline, flag)
add handler functon, this function will only execute one time
name | type | default | descriptin |
---|---|---|---|
type | String Number Boolean Expression | - | subject of pub-sub |
cb | Function | - | handler of pub-sub |
offline | Boolean | false | support offline subscibe? default:false. |
flag | Boolean | false | unshift or push handler in event queue(true: unshift, false: push) |
ps: the handler use once() to add will not be removed by off()
emit(type, message)
publish a messgae in a subject
name | type | default | descriptin |
---|---|---|---|
type | String Number Boolean Expression | - | subject of publish |
cb | Function | - | message of publish |
off(type, cb)
remove hanbler function
ps: off(type) will remove all handler under this subject
emitList()
return all pulish message list
eventList()
return all subscibe handler