Observe an object, check when it's change.
npm install spyo --save
<script src="node_modules/spyo/dist/spyo.min.js"></script>
<script src="https://unpkg.com/spyo/dist/spyo.min.js"></script>
const Spyo = require('spyo');
const myObject = {
firstName: 'Mike',
lastName: 'Red'
};
const mySpy = new Spyo(myObject);
myObject.firstName = 'John';
mySpy.onChange((different) => {
console.log('is different:', different);
});
const mySpy = new Spyo(() => {
return Math.random();
});
mySpy.onChange((different) => {
console.log('is different:', different);
});
Kind: global class
- Spyo
- new Spyo(obj, [opts])
- instance
- .refresh(obj)
- .check() ⇒
Spyo
- .watch() ⇒
Spyo
- .unwatch([reset]) ⇒
Spyo
- .isWatching() ⇒
boolean
- .onChange(callback) ⇒
Spyo
- .isChanged() ⇒
boolean
- .reset() ⇒
Spyo
- static
- .isEqual(a, b, exclude) ⇒
boolean
- .isIterable(obj) ⇒
boolean
- .isObject(obj) ⇒
boolean
- .isArray(obj) ⇒
boolean
- .isEqual(a, b, exclude) ⇒
Create instance
Param | Type | Default | Description |
---|---|---|---|
obj | Object | function | object that you want watch |
|
[opts] | Object | configuration object |
|
[opts.autoWatch] | boolean | true | auto watch |
[opts.checkMs] | number | 50 | interval in milliseconds for every check |
[opts.provider] | function |
| optional function called on every check that returns new state |
[opts.exclude] | String | Array |
| exclude a property or more from check |
[opts.autoReset] | boolean | false | reset changes detected after check |
Refresh data source object
Kind: instance method of Spyo
Param |
---|
obj |
spyo.check() ⇒ Spyo
Check if it's different and call onChange
callback
Kind: instance method of Spyo
spyo.watch() ⇒ Spyo
Start watching
Kind: instance method of Spyo
spyo.unwatch([reset]) ⇒ Spyo
Stop watching
Kind: instance method of Spyo
Param | Type | Description |
---|---|---|
[reset] | boolean | reset changes detected |
Detect if check is active
Kind: instance method of Spyo
spyo.onChange(callback) ⇒ Spyo
Fired when object is isChanged
Kind: instance method of Spyo
Param |
---|
callback |
Check if object is changed
Kind: instance method of Spyo
spyo.reset() ⇒ Spyo
Reset changes detected
Kind: instance method of Spyo
Check if two object are equals (deep check)
Kind: static method of Spyo
Param | Type | Description |
---|---|---|
a | object | first object |
b | object | second object |
exclude | Array | exclude properties from check |
Check if is an array or an plain object
Kind: static method of Spyo
Param |
---|
obj |
Check for plain object
Kind: static method of Spyo
Param | Type |
---|---|
obj | * |
Check for array
Kind: static method of Spyo
Param | Type |
---|---|
obj | * |
You can view the changelog here
Spyo is open-sourced software licensed under the MIT license