Brcast
Tiny data broadcaster with 0 dependencies
The current size of brcast/dist/brcast.umd.min.js
is:
It's like a data store you can subscribe to, with a setter to pump data in. For browsers and node.
Table of Contents
Install
This project uses node and npm. Go check them out if you don't have them locally installed.
$ npm install --save brcast
Then with a module bundler like rollup or webpack, use as you would anything else:
// using ES6 modules // using CommonJS modulesvar brcast =
The UMD build is also available on unpkg:
You can find the library on window.brcast
.
Usage
let broadcast = // subscribeconst subscriptionId = broadcast // setState sets the state and invoke all subscription callbacks passing in the statebroadcast // setState returns the current statebroadcast // unsubscribe to unbind the handlerbroadcast
API
brcast([initialState])
Creates a broadcast
object.
Arguments
1 - [initialState
] (any): The initial state.
Returns
(broadcast
): An object that holds the state.
broadcast.setState(state)
Store the new state.
Arguments
1 - state
(any): The new state.
Returns
Nothing.
broadcast.getState()
Get the stored state.
Returns
(Any
): The stored state.
broadcast.subscribe(handler)
Subscribe to state changes.
Arguments
1 - handler
(Function): The callback to be invoked any time the state changes.
Returns
(Number
): The subscription id to be used to unsubscribe.
broadcast.unsubscribe(subscriptionId)
Unsubscribe the change listener.
Arguments
1 - subscriptionId
(Number): The subscription id returned by subscribing.
Returns
Nothing.
Tests
$ npm run test