What is brcast?
The 'brcast' npm package is a small library for creating a broadcast system. It allows you to create a broadcaster that can notify listeners of changes. This is particularly useful for managing state changes in applications, especially in the context of React or other component-based frameworks.
What are brcast's main functionalities?
Creating a Broadcaster
This feature allows you to create a broadcaster with an initial state. The broadcaster can then be used to manage and notify listeners of state changes.
const createBroadcast = require('brcast');
const broadcast = createBroadcast(initialState);
Subscribing to Changes
This feature allows you to subscribe to state changes. The provided callback function will be called whenever the state changes, allowing you to react to those changes.
const unsubscribe = broadcast.subscribe((state) => {
console.log('State changed:', state);
});
Updating the State
This feature allows you to update the state of the broadcaster. All subscribed listeners will be notified of the new state.
broadcast.setState(newState);
Unsubscribing from Changes
This feature allows you to unsubscribe from state changes, stopping the callback function from being called when the state changes.
unsubscribe();
Other packages similar to brcast
redux
Redux is a popular state management library for JavaScript applications. It provides a more complex and feature-rich approach to managing state compared to brcast, including middleware support, time-travel debugging, and a strict unidirectional data flow.
mobx
MobX is another state management library that focuses on making state management simple and scalable by using observable state and reactions. It provides a more reactive approach compared to brcast, with automatic tracking of state changes.
rxjs
RxJS is a library for reactive programming using Observables, to make it easier to compose asynchronous or callback-based code. It offers a more powerful and flexible way to handle state changes and asynchronous events compared to brcast.
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:
import brcast from 'brcast'
var brcast = require('brcast')
The UMD build is also available on unpkg:
<script src="https://unpkg.com/brcast/dist/brcast.umd.js"></script>
You can find the library on window.brcast
.
Usage
import brcast from 'brcast'
let broadcast = brcast()
const subscriptionId = broadcast.subscribe(state => console.log(state))
broadcast.setState(1)
broadcast.getState()
broadcast.unsubscribe(subscriptionId)
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
MIT License © Alessandro Arnodo