UNPKG

3.37 kBMarkdownView Raw
1# Brcast
2
3> Tiny data broadcaster with 0 dependencies
4
5[![Travis](https://img.shields.io/travis/vesparny/brcast.svg)](https://travis-ci.org/vesparny/brcast)
6[![Code Coverage](https://img.shields.io/codecov/c/github/vesparny/brcast.svg?style=flat-square)](https://codecov.io/github/vesparny/brcast)
7[![David](https://img.shields.io/david/vesparny/brcast.svg)](https://david-dm.org/vesparny/brcast)
8[![npm](https://img.shields.io/npm/v/brcast.svg)](https://www.npmjs.com/package/brcast)
9[![npm](https://img.shields.io/npm/dm/brcast.svg)](https://npm-stat.com/charts.html?package=brcast&from=2017-04-01)
10[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
11[![MIT License](https://img.shields.io/npm/l/brcast.svg?style=flat-square)](https://github.com/vesparny/brcast/blob/master/LICENSE)
12
13The current size of `brcast/dist/brcast.umd.min.js` is:
14
15[![gzip size](http://img.badgesize.io/https://unpkg.com/brcast/dist/brcast.umd.min.js?compression=gzip&label=gzip%20size&style=flat-square)](https://unpkg.com/brcast/dist/)
16
17It's like a data store you can subscribe to, with a setter to pump data in.
18For browsers and node.
19
20## Table of Contents
21
22- [Install](#install)
23- [Usage](#usage)
24- [API](#api)
25- [Testing](#tests)
26- [License](#license)
27
28## Install
29
30This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.
31
32```sh
33$ npm install --save brcast
34```
35
36Then with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.js.org/), use as you would anything else:
37
38```javascript
39// using ES6 modules
40import brcast from 'brcast'
41
42// using CommonJS modules
43var brcast = require('brcast')
44```
45
46The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):
47
48```html
49<script src="https://unpkg.com/brcast/dist/brcast.umd.js"></script>
50```
51
52You can find the library on `window.brcast`.
53
54## Usage
55
56```js
57import brcast from 'brcast'
58
59let broadcast = brcast()
60
61// subscribe
62const subscriptionId = broadcast.subscribe(state => console.log(state))
63
64// setState sets the state and invoke all subscription callbacks passing in the state
65broadcast.setState(1)
66
67// setState returns the current state
68broadcast.getState()
69
70// unsubscribe to unbind the handler
71broadcast.unsubscribe(subscriptionId)
72```
73
74## API
75
76### `brcast([initialState])`
77
78Creates a `broadcast` object.
79
80#### Arguments
81
821 - [`initialState`] *(any)*: The initial state.
83
84#### Returns
85
86(`broadcast`): An object that holds the state.
87
88### `broadcast.setState(state)`
89
90Store the new state.
91
92#### Arguments
93
941 - `state` *(any)*: The new state.
95
96#### Returns
97
98Nothing.
99
100### `broadcast.getState()`
101
102Get the stored state.
103
104#### Returns
105
106(`Any`): The stored state.
107
108### `broadcast.subscribe(handler)`
109
110Subscribe to state changes.
111
112#### Arguments
113
1141 - `handler` *(Function)*: The callback to be invoked any time the state changes.
115
116#### Returns
117
118(`Number`): The subscription id to be used to unsubscribe.
119
120### `broadcast.unsubscribe(subscriptionId)`
121
122Unsubscribe the change listener.
123
124#### Arguments
125
1261 - `subscriptionId` *(Number)*: The subscription id returned by subscribing.
127
128#### Returns
129
130Nothing.
131
132## Tests
133
134```sh
135$ npm run test
136```
137
138
139[MIT License](LICENSE.md) © [Alessandro Arnodo](https://alessandro.arnodo.net/)