0.0.2 • Published 6 years ago

@theshy/pub-sub.js v0.0.2

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

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