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 |
|
13 | The 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 |
|
17 | It's like a data store you can subscribe to, with a setter to pump data in.
|
18 | For 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 |
|
30 | This 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 |
|
36 | Then 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
|
40 | import brcast from 'brcast'
|
41 |
|
42 | // using CommonJS modules
|
43 | var brcast = require('brcast')
|
44 | ```
|
45 |
|
46 | The [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 |
|
52 | You can find the library on `window.brcast`.
|
53 |
|
54 | ## Usage
|
55 |
|
56 | ```js
|
57 | import brcast from 'brcast'
|
58 |
|
59 | let broadcast = brcast()
|
60 |
|
61 | // subscribe
|
62 | const subscriptionId = broadcast.subscribe(state => console.log(state))
|
63 |
|
64 | // setState sets the state and invoke all subscription callbacks passing in the state
|
65 | broadcast.setState(1)
|
66 |
|
67 | // setState returns the current state
|
68 | broadcast.getState()
|
69 |
|
70 | // unsubscribe to unbind the handler
|
71 | broadcast.unsubscribe(subscriptionId)
|
72 | ```
|
73 |
|
74 | ## API
|
75 |
|
76 | ### `brcast([initialState])`
|
77 |
|
78 | Creates a `broadcast` object.
|
79 |
|
80 | #### Arguments
|
81 |
|
82 | 1 - [`initialState`] *(any)*: The initial state.
|
83 |
|
84 | #### Returns
|
85 |
|
86 | (`broadcast`): An object that holds the state.
|
87 |
|
88 | ### `broadcast.setState(state)`
|
89 |
|
90 | Store the new state.
|
91 |
|
92 | #### Arguments
|
93 |
|
94 | 1 - `state` *(any)*: The new state.
|
95 |
|
96 | #### Returns
|
97 |
|
98 | Nothing.
|
99 |
|
100 | ### `broadcast.getState()`
|
101 |
|
102 | Get the stored state.
|
103 |
|
104 | #### Returns
|
105 |
|
106 | (`Any`): The stored state.
|
107 |
|
108 | ### `broadcast.subscribe(handler)`
|
109 |
|
110 | Subscribe to state changes.
|
111 |
|
112 | #### Arguments
|
113 |
|
114 | 1 - `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 |
|
122 | Unsubscribe the change listener.
|
123 |
|
124 | #### Arguments
|
125 |
|
126 | 1 - `subscriptionId` *(Number)*: The subscription id returned by subscribing.
|
127 |
|
128 | #### Returns
|
129 |
|
130 | Nothing.
|
131 |
|
132 | ## Tests
|
133 |
|
134 | ```sh
|
135 | $ npm run test
|
136 | ```
|
137 |
|
138 |
|
139 | [MIT License](LICENSE.md) © [Alessandro Arnodo](https://alessandro.arnodo.net/)
|