• Stars
    star
    159
  • Rank 231,239 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ES6 Object.observe with nested object support - the way I want it (DEPRECATED)

#observed

ABANDONED 4/22/2016 Object.observe has been removed from future versions of V8. Therefore, this module is no longer being maintained.

NOTE: the Object.observe proposal has been withdrawn and may be removed from V8 in the future. Use at your own risk.


ES6 Object.observe with nested object support; e.g. the way I want it.

Build Status

What?

Do you dream of observing a plain javascript object for changes and reacting to it later? Now you can.

Available in Node >= 0.11.13, the standards compliant Object.observe treasure resides.

Object.observe allows us to register a listener for any type of change to a given object.

var o = { name: 'harmony' };
Object.observe(o, function (changes) {
  console.log(changes);
})
o.name = 'ES6!'
o.kind = 'observed';

// logs..
// [ { type: 'update',
//     object: { name: 'ES6!', kind: 'observed' },
//     name: 'name',
//     oldValue: 'harmony' },
//   { type: 'add',
//     object: { name: 'ES6!', kind: 'observed' },
//     name: 'kind' } ]

You'll notice our callback received an array of all changes that occured. Cool. But what about nested objects? Do they get automatically observed as well?

var o = { nested: { deeper: true }};
Object.observe(o, function (changes) {
  console.log(changes);
})
o.nested.deeper = false
// crickets ..

Turns out they don't. And that's what observed is for: watching object modifications without having to care about whether or not they have nested objects and arrays.

Usage

observed returns an EventEmitter which you listen to for changes. There are five classes of events, closely mirroring Object.observe

  • add
  • update
  • delete
  • reconfigure
  • change - fired when any of the above events are emitted
var O = require('observed')
var object = { name: {} }
var ee = O(object)

ee.on('add', console.log)

object.name.last = 'observed'

// logs
// { path: 'name.last',
//   name: 'last',
//   type: 'add',
//   object: { last: 'observed' },
//   value: 'observed',
//   oldValue: undefined }

You'll notice we now receive more information compared to Object.observe

  • path: full path to the property, including nesting
  • name: name of the path reported by Object.observe
  • type: name of the event reported by Object.observe
  • object: object reported from Object.observe
  • value: current value for the given path. same as object[name]
  • oldValue: previous value of the property as reported by Object.observe

You may also listen for changes to specific paths:

var O = require('observed')
var object = { name: { last: 'Heckmann', first: 'aaron' }}
var ee = O(object)

ee.on('update name.first', console.log)

object.name.first = 'Aaron'

// logs
// { path: 'name.first',
//   name: 'first',
//   type: 'update',
//   object: { last: 'Heckmann', first: 'Aaron' },
//   value: 'Aaron',
//   oldValue: 'aaron' }

stop()

To stop observing the object, call its stop() method. Internally this causes us to Object.unobserve() all the observed objects.

var O = require('observed');
var obj = { movie: { title: 'Mad Max: Fury Road' }};
var ee = O(obj);
// do something with ee
ee.stop();

deliverChanges()

There are occasions where we want to immediately force all pending changes to emit instead of waiting for the next turn of the event loop. observed has us covered here too. Just call its deliverChanges() method and all pending changes will emit.

var O = require('observed');
var obj = { movie: { title: 'Godzilla' }};
var ee = O(obj);

var emitted = false;
ee.on('change', function(){ emitted = true });

obj.movie.year = 2014;
assert.equal(false, emitted);

ee.deliverChanges();
assert.equal(true, emitted);
// :)

Use cases

  1. passing object changes down to a browser in realtime using something like primus.
  2. fanning out object changes across multiple nodes using something like axon.
  3. buffering changes and pass them off to your database of choice in one save action.

Features

  1. Object tracking: Using ES6 Object.observe we provide support for rich object tracking without manual getters/setters.
  2. Unobtrusive: Your object remains untouched and you may work with it as a plain js object.
  3. Events: Receive an EventEmitter back which emits the following events:
  • add
  • update
  • delete
  • reconfigure
  • change - fired when any of the above events are emitted

Node version requirements

Object.observe is available by default in Node >= 0.11.13.

NOTE: the Object.observe proposal has been withdrawn and may be removed from V8 in the future. Use at your own risk.

> node yourProgram.js

If you are running Node >= 0.11.0 < 0.11.13 you must run Node using the --harmony flag and use a 0.0.n version of this module.

> npm install [email protected]
> node --harmony yourProgram.js

Tests

Run em with npm test

License

MIT

More Repositories

1

gm

GraphicsMagick for node
JavaScript
6,923
star
2

gridfs-stream

Easily stream files to and from MongoDB
JavaScript
614
star
3

m

mongodb version management
Shell
257
star
4

node-ses

An Amazon SES api for nodejs with proper error handling.
JavaScript
200
star
5

sliced

A faster JavaScript alternative to [].slice.call(arguments)
JavaScript
96
star
6

mpromise

Promises/A+ conformant implementation
JavaScript
86
star
7

node-email

Simple wrapper for sendmail
JavaScript
83
star
8

greadme

Locally preview your markdown, Github style
JavaScript
77
star
9

gleak

Global variable leak detection for Node.js
JavaScript
70
star
10

gridform

Stream formidable uploads into MongoDB GridFS
JavaScript
59
star
11

muri

Muri is your friendly neighborhood MongoDB URI parser for Node.js
JavaScript
32
star
12

jquery.hook

Enables hooking into any jQuery method
31
star
13

js-styleguide

A javascript style guide.
27
star
14

mongooser

Mongoose REPL
JavaScript
23
star
15

connect-multipart-gridform

Connect multipart middleware configured to use MongoDB GridFS for file storage.
JavaScript
18
star
16

gm-demo

image manipulation demo on heroku
JavaScript
16
star
17

Nodal-Kombat

Multiplayer realtime fighting game
JavaScript
16
star
18

koa-mongodb-session

MongoDB backed session middleware for koa.js
JavaScript
15
star
19

graceful-shutdown

Shuts down a server gracefully upon the first specified signal received
JavaScript
13
star
20

mongodb-schema-miner

Generate schemata from MongoDB collections
JavaScript
12
star
21

jQuery.use

jQuery.use provides you on demand asynchronously loaded jQuery plugins at your fingertips.
JavaScript
11
star
22

channel9

channel9 demo of nodejs + mongodb + graphicsmagick
JavaScript
9
star
23

regexp-clone

Clones RegExps with flag and lastIndex preservation
JavaScript
9
star
24

gomon

MongoDB shell written in Node.js
JavaScript
8
star
25

.vim

vim
Vim Script
8
star
26

koa-session-mongodb

MongoDB backed session middleware for Koa
JavaScript
8
star
27

mongorc.js

.mongorc.js helpers
JavaScript
8
star
28

node-gameroom

My basic starter kit for creating realtime web-based games with nodejs.
JavaScript
7
star
29

once-upon

Executes a callback at most once upon the first of any number of events
JavaScript
7
star
30

express-custom-errors

What if you want to serve customized views for 403, 502, etc? That's what this plugin is for.
JavaScript
3
star
31

jquery-ui-lasso

A simple mouse lasso for jQuery ui
3
star
32

npm-downloads

Prints the number of downloads for a given npm package and the packages that directly depend on it
JavaScript
3
star
33

mongo-replset-test

Quickly set up a mongodb replica set test with authentication enabled
JavaScript
3
star
34

groups-of

Divides arrays into groups of a specified cardinality.
JavaScript
2
star
35

node-gmp

gmp for node
JavaScript
2
star
36

tic-tac-node

A silly demo of node and express
JavaScript
1
star
37

talks

My talks
JavaScript
1
star
38

workshop-04-2013

JavaScript
1
star
39

npm-update-test

Test repo for https://github.com/npm/npm/issues/19107
1
star
40

nproj

bare bones node project generator
Shell
1
star