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

Side by Side 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: Created 8 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/flag-definitions.h ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 "use strict";
29
30 var observerInfoMap = new $WeakMap;
31 var objectInfoMap = new $WeakMap;
32
33 $Object.observe = function(object, callback) {
34 if (!IS_SPEC_OBJECT(object))
35 throw MakeTypeError("TODO", ["TODO"])
36 if (!IS_SPEC_FUNCTION(callback))
37 throw MakeTypeError("TODO", ["TODO"])
38 if ($Object.isFrozen(callback))
39 throw MakeTypeError("TODO", ["TODO"])
40
41 var objectInfo = objectInfoMap.get(object);
42 var observerInfo = observerInfoMap.get(callback);
43
44 if (IS_UNDEFINED(objectInfo)) {
45 objectInfo = new $Object();
46 objectInfo.changeObservers = new $Array(1);
47 objectInfo.changeObservers[0] = callback;
adamk 2012/10/23 15:35:57 Even if we need $Array to avoid array literals her
rafaelw 2012/10/23 16:18:20 Done.
48 // TODO: setup objectInfo.notifier
49 objectInfoMap.set(object, objectInfo);
50
51 observerInfo = new $Object();
adamk 2012/10/23 15:35:57 This doesn't seem right, here you want to see if o
rafaelw 2012/10/23 16:18:20 Good catch. For some reason I was thinking these a
52 observerInfo.pendingChangeRecords = null;
53 // TODO: setup observerInfo.priority.
54 observerInfoMap.set(callback, observerInfo);
55 return;
56 }
57
58 var changeObservers = objectInfo.changeObservers;
59 for (var i = 0; i < changeObservers.length; i++) {
adamk 2012/10/23 15:35:57 Can you use changeObservers.indexOf() instead of t
rafaelw 2012/10/23 16:18:20 Done.
60 if (changeObservers[i] === callback)
61 return;
62 }
63
64 changeObservers.push(callback);
65 }
66
67 $Object.unobserve = function(object, callback) {
68 if (!IS_SPEC_OBJECT(object))
69 throw MakeTypeError("TODO", ["TODO"])
70
71 var objectInfo = objectInfoMap.get(object);
72 var observerInfo = observerInfoMap.get(callback);
adamk 2012/10/23 15:35:57 Can move this hash lookup after the undefined chec
rafaelw 2012/10/23 16:18:20 Done.
73 if (IS_UNDEFINED(objectInfo))
74 return;
75
76 var changeObservers = objectInfo.changeObservers;
77 for (var i = 0; i < changeObservers.length; i++) {
adamk 2012/10/23 15:35:57 Same question re: indexOf
rafaelw 2012/10/23 16:18:20 Done.
78 if (changeObservers[i] === callback) {
79 changeObservers.splice(i, 1);
80 return;
81 }
82 }
83 }
84
85 function EnqueueChangeRecord(changeRecord, observers) {
86 for (var i = 0; i < observers.length; i++) {
87 var observer = observers[i];
88 var observerInfo = observerInfoMap.get(observer);
89
90 // TODO: "activate" the observer
91
92 var pendingChangeRecords = observerInfo.pendingChangeRecords;
93 if (IS_NULL(pendingChangeRecords)) {
94 pendingChangeRecords = observerInfo.pendingChangeRecords = new $Array(1);
adamk 2012/10/23 15:35:57 Again, $Array(changeRecord) should suffice if lite
rafaelw 2012/10/23 16:18:20 Done.
95 pendingChangeRecords[0] = changeRecord
96 } else {
97 pendingChangeRecords.push(changeRecord);
98 }
99 }
100 }
101
102 $Object.notify = function(object, changeRecord) {
103 // TODO: notifier needs to be [[THIS]]
104 var type = changeRecord['type'];
adamk 2012/10/23 15:35:57 Nit: no need to copy this into a local, methinkgs:
rafaelw 2012/10/23 16:18:20 Done.
105 if (!IS_STRING(type))
106 MakeTypeError("TODO", ["TODO"]); // TODO
107
108 var newRecord = new $Object();
109 newRecord['object'] = object; // TODO: Needs to be 'object' retreived from not ifier
110 for (var prop in changeRecord) {
111 if (prop === 'object')
112 continue;
113 newRecord[prop] = changeRecord[prop];
114 }
115
116 $Object.freeze(newRecord);
117 var objectInfo = objectInfoMap.get(object);
118 if (IS_UNDEFINED(objectInfo))
adamk 2012/10/23 15:35:57 Probably want to move this before the creation of
rafaelw 2012/10/23 16:18:20 Done. Also updated spec to clarify this behavior
119 return;
120
121 EnqueueChangeRecord(newRecord, objectInfo.changeObservers);
122 }
123
124 $Object.deliverChangeRecords = function(callback) {
125 if (!IS_SPEC_FUNCTION(callback))
126 throw MakeTypeError("TODO", ["TODO"])
127
128 var observerInfo = observerInfoMap.get(callback);
129 if (!observerInfo)
adamk 2012/10/23 15:35:57 Should be if (IS_UNDEFINED()), I think?
rafaelw 2012/10/23 16:18:20 Done.
130 return false;
adamk 2012/10/23 15:35:57 Should be just return, per spec: Note: The user f
rafaelw 2012/10/23 16:18:20 Done.
131
132 var pendingChangeRecords = observerInfo.pendingChangeRecords;
133 if (IS_NULL(pendingChangeRecords))
134 return false;
adamk 2012/10/23 15:35:57 Ditto, should return undefined.
rafaelw 2012/10/23 16:18:20 Done.
135
136 observerInfo.pendingChangeRecords = null;
137
138 callback(pendingChangeRecords);
139 return true;
adamk 2012/10/23 15:35:57 ditto, should return undefined.
rafaelw 2012/10/23 16:18:20 Done.
140 }
OLDNEW
« no previous file with comments | « src/flag-definitions.h ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698