您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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