0.0.23 • Published 7 years ago
simple-watcher-js v0.0.23
simple-watcher-js
A tool to watch javascript objects.
Install
npm install simple-watcher-js --save Example
// Import watcherJS ES6 module
import watcherJS from 'simple-watcher-js'
// Or import ES5 build
<script src="./node_modules/simple-watcher-js/build/watcherjs.min.js"></script>
// My object
const user = {
username: 'John Doe'
}
// Listen updates on "username" property
const userWatcher = watcherJS.set(user, 'username', (event, property, value) => {
console.log('Your username has been updated!')
})
// Or listen all events for each properties
const userWatcher = watcherJS.watch(user, (event, property, value) => {
if (event === 'set' && property === 'username') {
console.log('Your username has been updated!')
}
})
// Updates to trigger callback
userWatcher.username = 'John Bilout'Usage
The watching functions take at least two parameters, target and callback,
it can also has property parameter in second position.
If property is defined as null then it's as not defined.
// Watch full object.
watcherJS.watch(target, callback)
// Watch only defined property
watcherJS.watch(target, property, callback)target: Object- Object we need to watch.property: String- Property to trigger events.callback: Function- Callback event.
From the callback parameters you can retreive
event- Current affected event.property- Current affected property.value- Current affected value.
API
watcherJS.set(target, ...arguments)- Watch updates events.watcherJS.get(target, ...arguments)- Watch readings events.watcherJS.has(target, ...arguments)- Watch "in" operator usage.watcherJS.watch(target, ...arguments)- Watch all events.