Code Monkey home page Code Monkey logo

observe-js's Introduction

Learn the tech

Why observe-js?

observe-js is a library for observing changes in JavaScript data. It exposes a high-level API and uses Object.observe if available, and otherwise performs dirty-checking.

Basic Usage

Path observation:

var observer = new PathObserver(obj, 'foo.bar.baz', function(newValue, oldValue) {
  // respond to obj.foo.bar.baz having changed value.
});

Array observation:

var observer = new ArrayObserver(arr, function(splices) {
  // respond to changes to the elements of arr.
  splices.forEach(function(splice) {
    splice.index; // index position that the change occurred.
    splice.removed; // an array of values representing the sequence of elements which were removed
    splice.addedCount; // the number of elements which were inserted.
  });
});

Object observation:

var observer = new ObjectObserver(obj, function(added, removed, changed, getOldValueFn) {
  // respond to changes to the obj.
  Object.keys(added).forEach(function(property) {
    property; // a property which has been been added to obj
    added[property]; // its value
  });
  Object.keys(removed).forEach(function(property) {
    property; // a property which has been been removed from obj
    getOldValueFn(property); // its old value
  });
  Object.keys(changed).forEach(function(property) {
    property; // a property on obj which has changed value.
    changed[property]; // its value
    getOldValueFn(property); // its old value
  });
});

Force delivery of any changes:

var obj = { id: 1 }
var observer = new ObjectObserve(obj, function(added, removed, changed, getOldValueFn) {
  // react.
});

obj.id = 2;
observer.deliver(); // causes the callback to be invoked reporting the change in value to obj.id.

Reset an observer to discard any previous changes:

var arr = [1, 2, 3];
var observer = new ArrayObserver(arr, function(splices) {
  // react.
});

arr.push(4);
observer.reset(); // observer forgets about prior changes
observer.deliver(); // because of the reset, there is nothing to report so callback is not invoked.

Close an observer

var obj = { foo: { bar: 2 } };
var observer = new PathObserver(arr, function(newValue, oldValue) {
  // react.
});
obj.foo.bar = 3;
observer.close(); // the observer is now invalid and will never fire its callback

About observing paths

PathObserver allows code to react to changes to a path value. Details:

  • If a path is unreachable from the provided object, its value is undefined
  • If a path is empty (''), its value is the object provided
  • Path observation respects prototype values.
  • PathObserver.getValueAtPath(obj, 'foo.bar.baz') is provided in order to retrieve a path value without observing it.
  • PathObserver.setValueAtPath(obj, 'foo.bar.baz') is provided in order to set the path value. Setting will create a final property, but not create objects.

About observing Arrays

ArrayObserver allows code to react to changes in the the indexed valued properties of an Array. Details:

  • Changes to non-indexed valued properties are not reported (e.g. arr.foo)
  • Regardless of what caused the change (e.g. splice(), arr[4] = 4, arr.length = 4), the effects are reported as splices.
  • The changes reported are the minimal set of splices required to transform the previous state of arr to the present state.
    • ArrayObserver.applySplices(splices, copyOfOldArray); will do actually do this.
  • ArrayObserver does not respect prototype values.

About observing Objects

ObjectObserver allows code to react to all property changes of a given object. Details:

  • Changes are reported as added, removed, and changed properties. Each is an object whose keys are property names and whose values the present value of that property on the object.
  • The forth argument (getOldValueFn) provided to callback, will retrieve the previous value of a given property if a change to it was reported.
  • ObjectObserver does not respect prototype values.

About delivery of changes

ChangeSummary is intended for use in environments which implement Object.observe, but it supports use in environments which do not.

If Object.observe is present, and observers have changes to report, their callbacks will be invoked at the end of the current turn (microtask). In a browser environment, this is generally at the end of an event.

If Object.observe is absent, Platform.performMicrotaskCheckpoint() must be called to trigger delivery of changes. If Object.observe is implemented, Platform.performMicrotaskCheckpoint() has no effect.

observe-js's People

Contributors

arv avatar dfreedm avatar ebidel avatar mkedwards avatar peterwmwong avatar rafaelw avatar samduvall avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.