Skip to content

theshying/pub-sub.js

Repository files navigation

Welcome to @theshy/pub-sub.js 👋

build Version Documentation Maintenance License: MIT

super-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

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

✅Run tests

npm run test

Author

👤 theShy

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2019 theShy.
This project is MIT licensed.