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

Commit

Permalink
Add support for new Object.observe change record names
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelw committed Oct 29, 2013
1 parent fb356da commit b68156e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 24 deletions.
85 changes: 65 additions & 20 deletions src/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,66 @@
(function(global) {
'use strict';

var changeRecordTypes = {
add: 'add',
update: 'update',
reconfigure: 'reconfigure',
'delete': 'delete',
splice: 'splice'
};

// Detect and do basic sanity checking on Object/Array.observe.
function detectObjectObserve() {
if (typeof Object.observe !== 'function' ||
typeof Array.observe !== 'function') {
return false;
}

var gotSplice = false;
function callback(records) {
if (records[0].type === 'splice' && records[1].type === 'splice')
gotSplice = true;
var records = [];

function callback(recs) {
records = recs;
}

var test = [0];
var test = {};
Object.observe(test, callback);
test.id = 1;
test.id = 2;
delete test.id;
Object.deliverChangeRecords(callback);
if (records.length !== 3)
return false;

// TODO(rafaelw): Remove this when new change record type names make it to
// chrome release.
if (records[0].type == 'new' &&
records[1].type == 'updated' &&
records[2].type == 'deleted') {
changeRecordTypes.add = 'new';
changeRecordTypes.update = 'updated';
changeRecordTypes.reconfigure = 'reconfigured';
changeRecordTypes['delete'] = 'deleted';
} else if (records[0].type != 'add' ||
records[1].type != 'update' ||
records[2].type != 'delete') {
console.error('Unexpected change record names for Object.observe. ' +
'Using dirty-checking instead');
return false;
}
Object.unobserve(test, callback);

test = [0];
Array.observe(test, callback);
test[1] = 1;
test.length = 0;
Object.deliverChangeRecords(callback);
return gotSplice;
if (records.length != 2)
return false;
if (records[0].type != 'splice' || records[1].type != 'splice')
return false;
Array.unobserve(test, callback);

return true;
}

var hasObserve = detectObjectObserve();
Expand Down Expand Up @@ -819,11 +861,10 @@
}
});

var knownRecordTypes = {
'new': true,
'updated': true,
'deleted': true
};
var expectedRecordTypes = {};
expectedRecordTypes[changeRecordTypes.add] = true;
expectedRecordTypes[changeRecordTypes.update] = true;
expectedRecordTypes[changeRecordTypes['delete']] = true;

function notifyFunction(object, name) {
if (typeof Object.observe !== 'function')
Expand Down Expand Up @@ -855,7 +896,7 @@
var observer = new PathObserver(obj, descriptor.path,
function(newValue, oldValue) {
if (notify)
notify('updated', oldValue);
notify(changeRecordTypes.update, oldValue);
}
);

Expand Down Expand Up @@ -890,7 +931,7 @@

for (var i = 0; i < changeRecords.length; i++) {
var record = changeRecords[i];
if (!knownRecordTypes[record.type]) {
if (!expectedRecordTypes[record.type]) {
console.error('Unknown changeRecord type: ' + record.type);
console.error(record);
continue;
Expand All @@ -899,10 +940,10 @@
if (!(record.name in oldValues))
oldValues[record.name] = record.oldValue;

if (record.type == 'updated')
if (record.type == changeRecordTypes.update)
continue;

if (record.type == 'new') {
if (record.type == changeRecordTypes.add) {
if (record.name in removed)
delete removed[record.name];
else
Expand All @@ -911,7 +952,7 @@
continue;
}

// type = 'deleted'
// type = 'delete'
if (record.name in added) {
delete added[record.name];
delete oldValues[record.name];
Expand Down Expand Up @@ -1299,12 +1340,12 @@
for (var i = 0; i < changeRecords.length; i++) {
var record = changeRecords[i];
switch(record.type) {
case 'splice':
case changeRecordTypes.splice:
mergeSplice(splices, record.index, record.removed.slice(), record.addedCount);
break;
case 'new':
case 'updated':
case 'deleted':
case changeRecordTypes.add:
case changeRecordTypes.update:
case changeRecordTypes['delete']:
if (!isIndex(record.name))
continue;
var index = toNumber(record.name);
Expand Down Expand Up @@ -1351,4 +1392,8 @@
global.PathObserver = PathObserver;
global.CompoundPathObserver = CompoundPathObserver;
global.Path = Path;

// TODO(rafaelw): Only needed for testing until new change record names
// make it to release.
global.Observer.changeRecordTypes = changeRecordTypes;
})(typeof global !== 'undefined' && global ? global : this);
8 changes: 4 additions & 4 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ suite('PathObserver Tests', function() {
get: function() { return _b; },
set: function(b) {
Object.getNotifier(this).notify({
type: 'updated',
type: Observer.changeRecordTypes.update,
name: 'b',
oldValue: _b
});
Expand Down Expand Up @@ -799,18 +799,18 @@ suite('PathObserver Tests', function() {
{
object: target,
name: 'computed',
type: 'new'
type: Observer.changeRecordTypes.add
},
{
object: target,
name: 'computed',
type: 'updated',
type: Observer.changeRecordTypes.update,
oldValue: 1
},
{
object: target,
name: 'computed',
type: 'reconfigured'
type: Observer.changeRecordTypes.reconfigure
}
]);

Expand Down

0 comments on commit b68156e

Please sign in to comment.