Comparing version 1.2.0 to 1.3.0
@@ -0,1 +1,34 @@ | ||
declare namespace onChange { | ||
interface Options { | ||
/** | ||
Deep changes will not trigger the callback. Only changes to the immediate properties of the original object. | ||
@default false | ||
@example | ||
``` | ||
import onChange = require('on-change'); | ||
const object = { | ||
a: { | ||
b: false | ||
} | ||
}; | ||
let i = 0; | ||
const watchedObject = onChange(object, () => { | ||
console.log('Object changed:', ++i); | ||
}, {isShallow: true}); | ||
watchedObject.a.b = true; | ||
// Nothing happens | ||
watchedObject.a = true; | ||
//=> 'Object changed: 1' | ||
``` | ||
*/ | ||
readonly isShallow?: boolean; | ||
} | ||
} | ||
declare const onChange: { | ||
@@ -73,3 +106,4 @@ /** | ||
previousValue: unknown | ||
) => void | ||
) => void, | ||
options?: onChange.Options | ||
): ObjectType; | ||
@@ -76,0 +110,0 @@ |
@@ -19,3 +19,3 @@ 'use strict'; | ||
const onChange = (object, onChange) => { | ||
const onChange = (object, onChange, options = {}) => { | ||
let inApply = false; | ||
@@ -68,3 +68,3 @@ let changed = false; | ||
const value = Reflect.get(target, property, receiver); | ||
if (isPrimitive(value) || property === 'constructor') { | ||
if (isPrimitive(value) || property === 'constructor' || options.isShallow === true) { | ||
return value; | ||
@@ -71,0 +71,0 @@ } |
{ | ||
"name": "on-change", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Watch an object or array for changes", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -78,3 +78,3 @@ # on-change [](https://travis-ci.org/sindresorhus/on-change) | ||
### onChange(object, onChange) | ||
### onChange(object, onChange, [options]) | ||
@@ -85,3 +85,3 @@ Returns a version of `object` that is watched. It's the exact same object, just with some `Proxy` traps. | ||
Type: `Object` | ||
Type: `object` | ||
@@ -103,3 +103,14 @@ Object to watch for changes. | ||
#### options | ||
Type: `object` | ||
##### isShallow | ||
Type: `boolean`<br> | ||
Default: `false` | ||
Deep changes will not trigger the callback. Only changes to the immediate properties of the original object. | ||
## Use-case | ||
@@ -106,0 +117,0 @@ |
10705
215
173