gawk
Gawk wraps JavaScript objects and arrays making them observable. Once a
JavaScript value 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!';
You can directly create GawkObject
and GawkArray
objects:
import { GawkArray, GawkObject } from 'gawk';
const obj = new GawkObject({ foo: 'bar' });
const arr = new GawkArray('a', 'b', 'c');
Upgrading to v3
Gawk v3 has dropped all gawk data types except GawkArray
and GawkObject
.
Since Gawk v3 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.
Gawk v3 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 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.