1.0.0 • Published 6 years ago

@sacoding/databus.js v1.0.0

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

databus.js

NPM Version Build Status

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());