https://github.com/caiogondim/redux-whenever.js
:clock430: Redux store subscriber for specific state change
https://github.com/caiogondim/redux-whenever.js
Last synced: 2 months ago
JSON representation
:clock430: Redux store subscriber for specific state change
- Host: GitHub
- URL: https://github.com/caiogondim/redux-whenever.js
- Owner: caiogondim
- Created: 2017-02-22T13:00:06.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-17T17:44:55.000Z (over 7 years ago)
- Last Synced: 2025-03-24T21:51:09.650Z (3 months ago)
- Language: JavaScript
- Homepage: https://npm.im/redux-whenever
- Size: 328 KB
- Stars: 21
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# redux-whenever
Subscribe to a state subtree (or leaf) and run callbacks `whenever` it evaluates to a given value.
## Installation
```
npm install --save redux-whenever
```## Usage
### Adding subscriber
```js
const whenever = require('redux-whenever')
const redux = require('redux')// Pass `whenever` as an enhancer
const store = redux.createStore(reducer, whenever)// Pass the state selector you are interested in as a string.
// `callback` will execute only when `player.isReady` becames `true`
store.whenever('player.isReady', true, (curState, prevState) => {
// Your magic here
})
```### Removing subscriber
```js
const unsubscribe = store.whenever('player.isReady', true, callback)
unsubscribe() // Removes previously added listener
```## API
### `const unsubscribe = store.whenever(selector, assertion, callback)`
Returns a function that, if called, removes the added subscriber.#### `selector`
- type: `String|Function`Should return a piece of the state tree
#### `assertion`
- type: `String|Number|Object|Function`If a function, it will be executed. If not, it's equality will be compared against current state.
#### `callback`
- type: `Function`## Credits
- Icon by Scott Lewis from the Noun Project---
[caiogondim.com](https://caiogondim.com) ·
GitHub [@caiogondim](https://github.com/caiogondim) ·
Twitter [@caio_gondim](https://twitter.com/caio_gondim)