1.0.0 • Published 7 years ago

ovv v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

Ovv

Simple pub/sub libary

Build Status

Install

yarn install Ovv

Usage

import Ovv from 'Ovv'

let o = new Ovv
o.subscribe(msg => { console.log('Observer 1:', msg) })
o.subscribe(msg => { console.log('Observer 2:', msg) })
o.publish('foo')
// => 'Observer 1:', 'foo'
// => 'Observer 2:', 'foo'


/** Unsubscribe - by return value **/
let o = new Ovv
const cancel = o.subscribe(msg => { console.log(msg) })
cancel()
o.publish('foo') // do nothing!!


/** Unsubscribe - by unsubscribe() **/
let o = new Ovv
const logger = msg => { console.log(msg) }
o.subscribe(logger)
o.unsubscribe(logger)
o.publish('foo') // do nothing!!

API

new Ovv( )

return:

  • instance - Ovv

Initialize an Ovv instance.

.subscribe(fn)

argument:

  • fn - Function

return:

  • unsubscribe - Function

Regist fn to the queue and return unsubscribe function.

.publish(value)

argument:

  • value - Any Type

Pass value to all fn s in the queue

.unsubscribe(fn)

argument:

  • fn - Function

Cancel the specific subscription.

const logger = (msg) => { console.log(msg) }
o.subscribe(logger)

o.unsubscribe(logger)
o.publish('foo') // do nothing!!

License

MIT