watch-me-now
v1.0.5
Published
Wrap an object (such as application state or data model) to listen for changes!
Downloads
4
Maintainers
Readme
Watch Me Now
the teeny-tiny, zero-dependency object observer
- emits on property assignments for all data types
- emits on
delete
- emits on mutable
Array
function calls - works on nested properties (infinitely)
- works in node as well as the browser!
installation:
npm install watch-me-now --save
example usage:
const WatchMeNow = require(`watch-me-now`)
// define a callback for when changes occur
const onStateChange = ({type, current, old, path, key}) => {
console.log(`\nonStateChange() - ${type}, ${path.length ? `path: ${path}, ` : ``}key: ${key}`)
console.log({new_state: JSON.stringify(current)})
console.log({old_state: JSON.stringify(old)})
}
// instantiate an object to observe
const state = WatchMeNow({}, onStateChange)
// go nuts
state.foo = `bar`
state.foo = {bar: `baz`}
state.foo.bar = `bum`
state.foo.bar = 24601
state.foo.bar += 5 + 14
delete state.foo.bar
state.my_array = [1, 2, 3, 4, 5]
state.my_array[0] = `potato`
state.my_array.reverse()
state.foo = {nested_array: [`a`, `b`, `c`]}
state.foo.nested_array.reverse()
state.foo.nested_array.push({six_times_seven: 42})