Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Unified Diff: src/object-observe.js

Issue 11225058: Initial JS stub implementation of Object.observe. Adds support for .object/.unobserve/.notify/.deli… (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: cr changes Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/object-observe.js
diff --git a/src/object-observe.js b/src/object-observe.js
new file mode 100644
index 0000000000000000000000000000000000000000..b50ca1e7f84939e87fa4dee3e54914be90237e67
--- /dev/null
+++ b/src/object-observe.js
@@ -0,0 +1,136 @@
+// Copyright 2012 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided
+// with the distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived
+// from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+"use strict";
+
+var observerInfoMap = new $WeakMap;
+var objectInfoMap = new $WeakMap;
+
+$Object.observe = function(object, callback) {
+ if (!IS_SPEC_OBJECT(object))
+ throw MakeTypeError("observe_non_object", ["observe"]);
+ if (!IS_SPEC_FUNCTION(callback))
+ throw MakeTypeError("observe_non_function", ["observe"]);
+ if ($Object.isFrozen(callback))
+ throw MakeTypeError("observe_callback_frozen");
rossberg 2012/10/24 09:57:24 I wonder, is this actually necessary? Why does the
rafaelw 2012/10/24 11:18:04 Sadly, yes. This was necessary to prevent a side-c
+
+ var observerInfo = observerInfoMap.get(callback);
+ if (IS_UNDEFINED(observerInfo)) {
+ observerInfo = new $Object();
rossberg 2012/10/24 09:57:24 Nit: {} is clearer and potentially more efficient
rafaelw 2012/10/24 11:18:04 Done.
+ observerInfo.pendingChangeRecords = null;
+ // TODO: setup observerInfo.priority.
+ observerInfoMap.set(callback, observerInfo);
+ }
+
+ var objectInfo = objectInfoMap.get(object);
+ if (IS_UNDEFINED(objectInfo)) {
+ objectInfo = new $Object();
+ objectInfo.changeObservers = new $Array(callback);
rossberg 2012/10/24 09:57:24 Nit: [callback] instead of invoking Array construc
rafaelw 2012/10/24 11:18:04 Done.
+ // TODO: setup objectInfo.notifier
+ objectInfoMap.set(object, objectInfo);
+ return;
+ }
+
+ var changeObservers = objectInfo.changeObservers;
+ if (changeObservers.indexOf(callback) >= 0)
rossberg 2012/10/24 09:57:24 Why not use a Set for this?
rafaelw 2012/10/24 11:18:04 AFAIC, The V8 implementation for Map & Set isn't c
rossberg 2012/10/24 12:03:13 Ah, you are right of course. Maybe put in a TODO t
rafaelw 2012/10/24 14:56:05 Done.
+ return;
+
+ changeObservers.push(callback);
+}
+
+$Object.unobserve = function(object, callback) {
+ if (!IS_SPEC_OBJECT(object))
+ throw MakeTypeError("observe_non_object", ["unobserve"]);
+
+ var objectInfo = objectInfoMap.get(object);
+ if (IS_UNDEFINED(objectInfo))
rossberg 2012/10/24 09:57:24 OT: Is there a reason why the spec does not make t
rafaelw 2012/10/24 11:18:04 You mean: unobserving something that isn't observe
+ return;
+
+ var observerInfo = observerInfoMap.get(callback);
+
+ var changeObservers = objectInfo.changeObservers;
+ var index = changeObservers.indexOf(callback);
+ if (index < 0)
+ return;
+
+ changeObservers.splice(index, 1);
+}
+
+function EnqueueChangeRecord(changeRecord, observers) {
+ for (var i = 0; i < observers.length; i++) {
+ var observer = observers[i];
+ var observerInfo = observerInfoMap.get(observer);
+
+ // TODO: "activate" the observer
+
+ var pendingChangeRecords = observerInfo.pendingChangeRecords;
+ if (IS_NULL(pendingChangeRecords)) {
+ pendingChangeRecords = observerInfo.pendingChangeRecords = new $Array(changeRecord);
rossberg 2012/10/24 09:57:24 Line too long.
rafaelw 2012/10/24 11:18:04 Done.
+ } else {
+ pendingChangeRecords.push(changeRecord);
+ }
+ }
+}
+
+$Object.notify = function(object, changeRecord) {
+ // TODO: notifier needs to be [[THIS]]
rossberg 2012/10/24 09:57:24 Check that this is an object.
rafaelw 2012/10/24 11:18:04 I asked Arv about this in the spec. He pointed out
+ if (!IS_STRING(changeRecord['type']))
rossberg 2012/10/24 09:57:24 Preferable: changeRecord.type
rafaelw 2012/10/24 11:18:04 Done.
+ throw MakeTypeError("observe_type_non_string");
+
+ var objectInfo = objectInfoMap.get(object);
+ if (IS_UNDEFINED(objectInfo))
+ return;
+
+ var newRecord = new $Object();
rossberg 2012/10/24 09:57:24 {}
rafaelw 2012/10/24 11:18:04 Done.
+ newRecord['object'] = object; // TODO: Needs to be 'object' retreived from notifier
rossberg 2012/10/24 09:57:24 newRecord.object
rossberg 2012/10/24 09:57:24 Line too long
rafaelw 2012/10/24 11:18:04 Done.
+ for (var prop in changeRecord) {
+ if (prop === 'object')
+ continue;
+ newRecord[prop] = changeRecord[prop];
+ }
+ $Object.freeze(newRecord);
+
+ EnqueueChangeRecord(newRecord, objectInfo.changeObservers);
+}
+
+$Object.deliverChangeRecords = function(callback) {
+ if (!IS_SPEC_FUNCTION(callback))
+ throw MakeTypeError("observe_non_function", ["deliverChangeRecords"]);
+
+ var observerInfo = observerInfoMap.get(callback);
+ if (IS_UNDEFINED(observerInfo))
+ return;
+
+ var pendingChangeRecords = observerInfo.pendingChangeRecords;
+ if (IS_NULL(pendingChangeRecords))
+ return;
+
+ observerInfo.pendingChangeRecords = null;
+
+ callback(pendingChangeRecords);
+ return;
+}

Powered by Google App Engine
This is Rietveld 408576698