gawk
Gawk wraps JavaScript objects and arrays making them observable. Once a JavaScript object is gawked,
you can listen for changes including deeply nested changes. Only objects and arrays can be gawked.
All other types are passed through.
Gawked objects and arrays can be interacted with as if they were regular objects/arrays. Built-in
functions such as JSON.stringify()
work as expected.
Note: gawk uses ES2015 proxies and thus requires Node.js 6 or newer.
Installation
npm install gawk
Examples
import gawk from 'gawk';
const obj = gawk({
foo: 'bar'
});
gawk.watch(obj, (obj, src) => {
console.info('object changed!');
console.info('new value =', obj);
});
obj.foo = 'baz';
console.info(obj);
You can also be notified if a deep child is changed:
const obj = gawk({
foo: {
bar: ['a', 'b']
}
});
gawk.watch(obj, (obj, src) => {
console.info('object changed!');
console.info('new value =', obj);
});
obj.foo.bar.push('c', 'd');
console.info(obj);
To filter watch notifications, simply pass in the property name or array of
property names used to filter the gawk object.
const obj = gawk({
foo: {
bar: 'hello'
}
});
gawk.watch(obj, [ 'foo', 'bar' ], value => {
console.info(`bar changed to ${value}`);
});
obj.foo.bar = 'world!';
To stop watching, simply call gawk.unwatch()
with the original listener function.
const obj = gawk({ });
function onChange(obj, src) {
console.log('changed!');
}
gawk.watch(obj, onChange);
obj.foo = 'bar';
gawk.unwatch(obj, onChange);
obj.foo = 'baz';
Upgrading to v4
Gawk v4 has dropped all gawk data types. You must always call gawk()
.
Change all new GawkObject()
calls to gawk({})
and new GawkArray()
to gawk([])
.
Since Gawk v3 and newer uses ES6 Proxies, you no longer need to call obj.get()
, obj.set()
,
obj.delete()
, etc.
Methods obj.watch()
, obj.merge()
, and obj.mergeDeep()
have moved to gawk.watch()
,
gawk.merge()
, and gawk.mergeDeep()
. The first argument must be a gawk object.
Starting in v3, Gawk no longer hashes values. This means speed. Gawk v3+ is about 19 times faster
than v1 and v2.
License
(The MIT License)
Copyright (c) 2016-2017 Chris Barber
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.