@sacoding/databus.js
v1.0.0
Published
redux-saga based databus
Downloads
1
Readme
databus.js
databus.js
is a library providing databus for frontend applications.
Installation
npm install --save @sacoding/databus.js
Usage
To use databus.js
only import of databus
object is required.
import databus from '@sacoding/databus.js';
databus
object is singleton so it does not matter where it is imported.
Examples
Dispatching an event
import databus from '@sacoding/databus.js';
databus.dispatchEvent('foo', { a: 1 });
Subscribing an event
import databus from '@sacoding/databus.js';
databus.subscribeEvent('foo', payload => console.log(payload));
databus.dispatchEvent('foo', { a: 1 });
Adding new state reducer
import databus from '@sacoding/databus.js';
databus.addStateReducer('foo', (state = {}, { type, payload }) => {
if ('foo' !== type) {
return state;
}
return { ...state, ...payload };
});
Getting state
import databus from '@sacoding/databus.js';
databus.addStateReducer('foo', (state = {}, { type, payload }) => {
if ('foo' !== type) {
return state;
}
return { ...state, ...payload };
});
databus.dispatchEvent('foo', { a: 1 });
console.log(databus.getState());