redux-watch
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -0,1 +1,5 @@ | ||
2016-05-03 / 1.1.1 | ||
------------------ | ||
- bug fix: infinite loop: [#4][#4] | ||
2015-12-14 / 1.1.0 | ||
@@ -8,1 +12,7 @@ ------------------ | ||
- initial release | ||
[#5]: https://github.com/jprichardson/redux-watch/issues/5 "Any unwatch feature?" | ||
[#4]: https://github.com/jprichardson/redux-watch/pull/4 "call function after changing base value [enhancement]" | ||
[#3]: https://github.com/jprichardson/redux-watch/issues/3 "Problem when subscriber dispatch action" | ||
[#2]: https://github.com/jprichardson/redux-watch/issues/2 "Does not seem to work with Immutable.js" | ||
[#1]: https://github.com/jprichardson/redux-watch/issues/1 "tx for a GREAT lib... if possible please add TypeScript support..." |
13
index.js
@@ -0,1 +1,2 @@ | ||
'use strict' | ||
var getValue = require('object-path').get | ||
@@ -9,9 +10,11 @@ | ||
compare = compare || defaultCompare | ||
var baseVal = getValue(getState(), objectPath) | ||
var currentValue = getValue(getState(), objectPath) | ||
return function w (fn) { | ||
return function () { | ||
var newVal = getValue(getState(), objectPath) | ||
if (compare(baseVal, newVal)) return | ||
fn(newVal, baseVal, objectPath) | ||
baseVal = newVal | ||
var newValue = getValue(getState(), objectPath) | ||
if (!compare(currentValue, newValue)) { | ||
var oldValue = currentValue | ||
currentValue = newValue | ||
fn(newValue, oldValue, objectPath) | ||
} | ||
} | ||
@@ -18,0 +21,0 @@ } |
{ | ||
"name": "redux-watch", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Watch Redux state for changes.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "standard && ava test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/jprichardson/redux-watch.git" | ||
}, | ||
"keywords": [ | ||
@@ -24,17 +16,31 @@ "redux", | ||
], | ||
"author": "JP Richardson", | ||
"license": "MIT", | ||
"homepage": "https://github.com/jprichardson/redux-watch#readme", | ||
"bugs": { | ||
"url": "https://github.com/jprichardson/redux-watch/issues" | ||
}, | ||
"homepage": "https://github.com/jprichardson/redux-watch#readme", | ||
"license": "MIT", | ||
"author": "JP Richardson", | ||
"files": [ | ||
"index.js" | ||
], | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/jprichardson/redux-watch.git" | ||
}, | ||
"scripts": { | ||
"lint": "standard", | ||
"test": "npm run lint && npm run unit", | ||
"unit": "tape test/*.js | tap-spec" | ||
}, | ||
"dependencies": { | ||
"object-path": "^0.9.2" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.7.0", | ||
"redux": "^3.0.5", | ||
"reselect": "^2.0.1", | ||
"standard": "^5.4.1" | ||
}, | ||
"dependencies": { | ||
"object-path": "^0.9.2" | ||
"standard": "7.x", | ||
"tap-spec": "^4.1.1", | ||
"tape": "^4.5.1" | ||
} | ||
} |
@@ -1,42 +0,33 @@ | ||
redux-watch | ||
=========== | ||
# redux-watch | ||
Watch/observe [Redux](http://redux.js.org/) store state changes. | ||
[![NPM Package](https://img.shields.io/npm/v/redux-watch.svg?style=flat-square)](https://www.npmjs.org/package/redux-watch) | ||
[![Build Status](https://img.shields.io/travis/jprichardson/redux-watch.svg?branch=master&style=flat-square)](https://travis-ci.org/jprichardson/redux-watch) | ||
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) | ||
Why? | ||
---- | ||
Watch/observe [Redux](http://redux.js.org/) store state changes. | ||
Redux provides you with a `subscribe()` method so that you can be notified when | ||
the state changes. However, it does not let you know what changed. `redux-watch` | ||
will let you know what changed. | ||
## Why? | ||
Redux provides you with a `subscribe()` method so that you can be notified when the state changes. However, it does not let you know what changed. `redux-watch` will let you know what changed. | ||
Install | ||
------- | ||
npm i --save redux-watch | ||
## Install | ||
``` | ||
npm i --save redux-watch | ||
``` | ||
## Usage | ||
Usage | ||
----- | ||
`watch(getState [, objectPath [, comparison]])` -> `function` | ||
### watch() | ||
- `getState`: A `function` that is used to return the state. Also useful in conjunction with selectors. | ||
- `objectPath`: An **optional** `string` or `Array` that represents the path in an object. Uses [object-path](https://www.npmjs.com/package/object-path) ([mariocasciaro/object-path](https://github.com/mariocasciaro/object-path)) for value extraction. | ||
- `comparison`: An **optional** function to pass for comparison of the fields. Defaults to strict equal comparison (`===`). | ||
**Signature:** `watch(getState, [objectPath], [comparison])` | ||
## Example | ||
**Parameters:** | ||
##### basic example | ||
- `getState`: A `function` that is used to return the state. Also useful in conjunction | ||
with selectors. | ||
- `objectPath`: An **optional** `string` or `Array` that represents the path in an object. Fully compatible with | ||
[npm/object-path](https://www.npmjs.com/package/object-path). | ||
- `comparison`: An **optional** function to pass for comparison of the fields. Defaults to strict | ||
equal comparison (`===`). Could use [npm/deep-equal](https://www.npmjs.com/package/deep-equal). | ||
**Returns:** A `function` that can be used to watch for the changes. | ||
**Example (basic):** | ||
```js | ||
@@ -60,6 +51,5 @@ // ... other imports/requires | ||
**Example (w/ [reselect](https://github.com/rackt/reselect) selectors):** | ||
##### example (w/ [reselect](https://www.npmjs.com/package/reselect) ([reactjs/reselect](https://github.com/reactjs/reselect)) selectors) | ||
When using with selectors, you often times won't need to pass the object path. | ||
Most times the selectors will handle this for you. | ||
When using with selectors, you often times won't need to pass the object path. Most times the selectors will handle this for you. | ||
@@ -78,8 +68,22 @@ ```js | ||
#### Note on Comparisons. | ||
License | ||
------- | ||
By default, `redux-watch` uses `===` (strict equal) operator to check for changes. This may not be want you want. Sometimes you may want to do a deep inspection. You should use either [deep-equal](https://www.npmjs.com/package/deep-equal) ([substack/node-deep-equal](https://github.com/substack/node-deep-equal)) or [is-equal](https://www.npmjs.com/package/is-equal) ([ljharb/is-equal](https://github.com/ljharb/is-equal)). `is-equal` is better since it supports ES6 types like Maps/Sets. | ||
##### deep equal example | ||
```js | ||
import isEqual from 'is-equal' | ||
import watch from 'redux-watch' | ||
let w = watch(store.getState, 'admin', isEqual) | ||
store.subscribe(w((newVal, oldVal, objectPath) => { | ||
// response to changes | ||
})) | ||
``` | ||
## License | ||
MIT | ||
Copyright (c) [JP Richardson](https://github.com/jprichardson) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5489
20
88
5
4