Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. change-emitter
  2. ==============
  3. [![build status](https://img.shields.io/travis/acdlite/change-emitter/master.svg?style=flat-square)](https://travis-ci.org/acdlite/change-emitter)
  4. [![npm version](https://img.shields.io/npm/v/change-emitter.svg?style=flat-square)](https://www.npmjs.com/package/change-emitter)
  5. Listen for changes. Like an event emitter that only emits a single event type. Really tiny.
  6. I extracted this from Redux's `createStore()` because I found it to be useful in other contexts. Use it where you want the most minimal event subscription implementation possible.
  7. ## Usage
  8. ```js
  9. import { createChangeEmitter } from 'change-emitter'
  10. const emitter = createChangeEmitter()
  11. // Called `listen` instead of `subscribe` to avoid confusion with observable spec
  12. const unlisten = emitter.listen((...args) => {
  13. console.log(args)
  14. })
  15. emitter.emit(1, 2, 3) // logs `[1, 2, 3]`
  16. unlisten()
  17. emitter.emit(4, 5, 6) // doesn't log
  18. ```
  19. ## Larger example
  20. Here's a (partial) implementation of Redux's `createStore`:
  21. ```js
  22. const createStore = (reducer, initialState) => {
  23. let state = initialState
  24. const emitter = createChangeEmitter()
  25. function dispatch(action) {
  26. state = reducer(state, action)
  27. emitter.emit()
  28. return action
  29. }
  30. function getState() {
  31. return state
  32. }
  33. return {
  34. dispatch,
  35. getState,
  36. subscribe: emitter.listen
  37. }
  38. }
  39. ```