Skip to content

Commit

Permalink
Merge pull request #1 from cb1kenobi/v3
Browse files Browse the repository at this point in the history
v3.0.0!
  • Loading branch information
cb1kenobi committed Nov 14, 2016
2 parents 9d46197 + ac0a0be commit 75b9ee9
Show file tree
Hide file tree
Showing 19 changed files with 5,081 additions and 3,133 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["nodejs-lts"]
"plugins": ["transform-es2015-modules-commonjs"]
}
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"no-mixed-spaces-and-tabs": 0,
"no-cond-assign": 0,
"no-unused-vars": 0,
"constructor-super": 0
"constructor-super": 0,
"no-class-assign": 0
}
}
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
language: node_js
node_js:
- "4"
- "5"
- "6"
- "7"
os:
- linux
sudo: false
Expand Down
55 changes: 35 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@
[![Dev Deps][david-dev-image]][david-dev-url]

Gawk is a observable model that wraps JavaScript data types. Once a JavaScript
value is wrapped, it allows you to listen for changes. You can observe deeply
nested objects too.
value is gawked, you can listen for changes including deeply nested changes.

Gawk supports the common built-in data types such as string, boolean, number,
array, object, function, null, and undefined. Anything that you can represent
in a JSON object, you can gawk.
Only arrays and objects can be gawked. All other types are passed through.

Gawked arrays, objects, and functions have unique methods for common tasks. For
example, `GawkArray` instances have `push()` and `pop()` methods.

> Note: gawk requires Node.js 4 or newer.
> Note: gawk uses ES2015 proxies and thus requires Node.js 6 or newer.
## Installation

Expand All @@ -28,22 +22,20 @@ example, `GawkArray` instances have `push()` and `pop()` methods.
## Examples

```javascript
import { gawk } from 'gawk';
// or if you're using CommonJS:
// const gawk = require('gawk').gawk;
import gawk from 'gawk';

const obj = gawk({
foo: 'bar'
});

obj.watch(evt => {
gawk.watch(obj, (obj, source) => {
console.info('object changed!');
console.info('new value =', evt.target.toJS());
console.info('new value =', evt.target);
});

obj.set('foo', 'baz');
obj.foo = 'baz';

console.info(obj.toJS()); // { foo: 'baz' }
console.info(obj); // { foo: 'baz' }
```

You can also be notified if a deep child is changed:
Expand All @@ -55,16 +47,39 @@ const obj = gawk({
}
});

obj.watch(evt => {
gawk.watch(obj, (obj, source) => {
console.info('object changed!');
console.info('new value =', evt.target.toJS());
console.info('new value =', evt.target);
});

obj.get(['foo', 'bar']).push('c', 'd');
obj.foo.bar.push('c', 'd');

console.info(obj); // { foo: { bar: ['a', 'b', 'c', 'd'] } }
```

You can also directly create `GawkObject` and `GawkArray` objects:

```javascript
import { GawkArray, GawkObject } from 'gawk';

console.info(obj.toJS()); // { foo: { bar: ['a', 'b', 'c', 'd'] } }
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)
Expand Down
39 changes: 39 additions & 0 deletions benchmark/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const gawk = require('../dist/index').gawk;

const gobj = gawk({
foo: {
bar: {}
}
});

let counter = 0;
gawk.watch(gobj, () => {
counter++;
});

const n = 100000;
console.log(`Set size: ${n}`);

const start = Date.now();

for (let i = 0; i < n; i++) {
gawk.mergeDeep(gobj.foo, {
bar: {
baz: {
a: Math.random(),
b: Math.random(),
c: Math.random()
}
}
});
}

const delta = Date.now() - start;

console.log('Finished in ' + (delta / 1000).toFixed(2) + 's');

if (counter === n) {
console.log(`Worked! Fired watcher ${counter} times`);
} else {
console.log(`Failed! Expected ${n} events, got ${counter}`);
}
33 changes: 16 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gawk",
"version": "2.0.0",
"version": "3.0.0",
"description": "Observable JavaScript object model",
"main": "./dist/index.js",
"author": "Chris Barber <[email protected]> (https://github.com/cb1kenobi)",
Expand All @@ -23,30 +23,29 @@
"test": "gulp test"
},
"dependencies": {
"object-hash": "^1.1.2",
"source-map-support": "^0.4.1"
"source-map-support": "^0.4.6"
},
"devDependencies": {
"babel-eslint": "^6.1.0",
"babel-preset-nodejs-lts": "^1.2.2",
"babel-eslint": "^7.1.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0",
"chai": "^3.5.0",
"codeclimate-test-reporter": "^0.3.3",
"coveralls": "^2.11.9",
"del": "^2.2.1",
"codeclimate-test-reporter": "^0.4.0",
"coveralls": "^2.11.15",
"del": "^2.2.2",
"esdoc-es7-plugin": "^0.0.3",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-babel-istanbul": "^1.4.0",
"gulp-debug": "^2.1.2",
"gulp-esdoc": "^0.2.0",
"gulp-eslint": "^3.0.0",
"gulp-babel-istanbul": "^1.5.0",
"gulp-debug": "^3.0.0",
"gulp-esdoc": "^0.3.0",
"gulp-eslint": "^3.0.1",
"gulp-filter": "^4.0.0",
"gulp-inject-modules": "^1.0.0",
"gulp-load-plugins": "^1.2.4",
"gulp-mocha": "^2.2.0",
"gulp-load-plugins": "^1.4.0",
"gulp-mocha": "^3.0.1",
"gulp-plumber": "^1.1.0",
"gulp-sourcemaps": "^2.0.0-alpha",
"sinon": "^1.17.4",
"gulp-sourcemaps": "^2.2.0",
"sinon": "^1.17.6",
"sinon-chai": "^2.8.0"
},
"homepage": "https://github.com/cb1kenobi/gawk",
Expand All @@ -56,6 +55,6 @@
"url": "git://github.com/cb1kenobi/gawk.git"
},
"engines": {
"node": ">=4.0.0"
"node": ">=6.0.0"
}
}
Loading

0 comments on commit 75b9ee9

Please sign in to comment.