@theshy/pub-sub.js
v0.0.2
Published
super-pub-sub.js is a ligtweight、none dependence lib, it supports event pulish、subscibe and offline subscibe.
Downloads
6
Maintainers
Readme
pub-sub.js
Introduction
pub-sub.js is a ligtweight、none dependence lib, it supports event pulish、subscibe and offline subscibe.
Install
npm i @theshy/pub-sub.js --save
Usage
Browser
<script src="/dist/pub-sub.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('@theshy/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
once(type, cb, offline, flag)
add handler functon, this function will only execute one time
ps: the handler use once() to add will not be removed by off()
emit(type, message)
publish a messgae in a subject
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