Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
implement root object property filter for observedSets
Browse files Browse the repository at this point in the history
the idea here is that observed sets have a "root" object which is shared by all paths observed by the observedSet. In general, we treat any change to any observed object as a reason to check everything and verify dependencies, but this patch adds a filter so that if all changes are to the root object, but to properties on which no path depends, then we can skip doing any work.

R=arv
BUG=

Review URL: https://codereview.appspot.com/88000049
  • Loading branch information
rafaelw committed Apr 16, 2014
1 parent cb17cbb commit b229ed5
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
obj = obj[this[i - 1]];
if (!isObject(obj))
return;
observe(obj);
observe(obj, this[0]);
}
},

Expand Down Expand Up @@ -442,20 +442,40 @@
var observerCount = 0;
var observers = [];
var objects = [];
var rootObj;
var rootObjProps;

function observe(obj) {
function observe(obj, prop) {
if (!obj)
return;

if (obj === rootObj)
rootObjProps[prop] = true;

if (objects.indexOf(obj) < 0) {
objects.push(obj);
Object.observe(obj, callback);
}

observe(Object.getPrototypeOf(obj));
observe(Object.getPrototypeOf(obj), prop);
}

function allRootObjNonObservedProps(recs) {
for (var i = 0; i < recs.length; i++) {
var rec = recs[i];
if (rec.object !== rootObj ||
rootObjProps[rec.name] ||
rec.type === 'setPrototype') {
return false;
}
}
return true;
}

function callback() {
function callback(recs) {
if (allRootObjNonObservedProps(recs))
return;

var observer;
for (var i = 0; i < observers.length; i++) {
observer = observers[i];
Expand All @@ -475,7 +495,12 @@
var record = {
object: undefined,
objects: objects,
open: function(obs) {
open: function(obs, object) {
if (!rootObj) {
rootObj = object;
rootObjProps = {};
}

observers.push(obs);
observerCount++;
obs.iterateObjects_(observe);
Expand All @@ -493,6 +518,8 @@

observers.length = 0;
objects.length = 0;
rootObj = undefined;
rootObjProps = undefined;
observedSetCache.push(this);
}
};
Expand All @@ -507,7 +534,7 @@
lastObservedSet = observedSetCache.pop() || newObservedSet();
lastObservedSet.object = obj;
}
lastObservedSet.open(observer);
lastObservedSet.open(observer, obj);
return lastObservedSet;
}

Expand Down

0 comments on commit b229ed5

Please sign in to comment.