This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
observe-js-behavior.html
174 lines (153 loc) · 4.68 KB
/
observe-js-behavior.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<link rel="import" href="observe-js-import.html">
<script>
(function() {
['push', 'pop', 'shift', 'unshift', 'splice'].forEach(function(m) {
var orig = Polymer.Base[m];
Polymer.Base[m] = function(path) {
var obj = this.get(path);
this._flushObserveJsObservers(obj);
orig.apply(this, arguments);
this._discardObserveJsObservers(obj);
};
}, this);
})();
Polymer.ObserveJsBehavior = {
_observeJsStatic: {
count: 0,
observerMap: new WeakMap()
},
created: function() {
this._observeJsId = this._observeJsStatic.count++;
},
_propertyChanged: function(path, newValue, oldValue) {
if (oldValue && typeof oldValue == 'object') {
this._removeObserveJsObserver(oldValue, path);
}
if (newValue && typeof newValue == 'object') {
this._addObserveJsObserver(newValue, path);
}
},
// Bookkeeping here ensures only one observer is created for each object
// throughout the tree, with a list of elements and paths that observed the
// array; the same changes are notified to each element/path using the same
// change record (in the case of array notification) to ensure base
// dirty-checking filters out changes already notified through one part of
// the tree
_addObserveJsObserver: function(object, path) {
var obs = this._observeJsStatic.observerMap.get(object);
if (!obs) {
var els = {};
obs = {
count: 1,
observer: this._createObserveJsObserver(object, els),
elements: els
};
this._observeJsStatic.observerMap.set(object, obs);
} else {
obs.count++;
}
var el = obs.elements[this._observeJsId];
if (!el) {
el = obs.elements[this._observeJsId] = {
element: this,
paths: {},
count: 1
};
el.paths[path] = 1;
} else {
var count = el.paths[path];
if (!count) {
el.count++;
}
el.paths[path] = count++;
}
},
_createObserveJsObserver: function(object, els) {
var observer;
if (Array.isArray(object)) {
observer = new ArrayObserver(object);
observer.open(function(splices) {
this._notifyObserveJsPath(els, 'splices', {
keySplices: Polymer.Collection.applySplices(object, splices),
indexSplices: splices
});
}, this);
} else {
observer = new ObjectObserver(object);
observer.open(function(added, removed, changed) {
Object.keys(added).forEach(function(prop) {
this._notifyObserveJsPath(els, prop, added[prop]);
}, this);
Object.keys(removed).forEach(function(prop) {
this._notifyObserveJsPath(els, prop, undefined);
}, this);
Object.keys(changed).forEach(function(prop) {
this._notifyObserveJsPath(els, prop, changed[prop]);
}, this);
}, this);
}
return observer;
},
_notifyObserveJsPath: function(els, prop, value) {
for (var id in els) {
var el = els[id];
for (var path in el.paths) {
el.element.set(path + '.' + prop, value);
}
}
},
_removeObserveJsObserver: function(object, path) {
var el = obs.elements[this._observeJsId];
if (--el.paths[path] === 0) {
delete el.paths[path];
if (--el.count === 0) {
delete obs.elements[this._observeJsId];
if (--obs.count === 0) {
obs.observer.close();
obs.observer = null;
}
}
}
},
_flushObserveJsObservers: function(object) {
var obs = this._observeJsStatic.observerMap.get(object);
if (obs) {
obs.observer.deliver();
}
},
_discardObserveJsObservers: function(object) {
var obs = this._observeJsStatic.observerMap.get(object);
if (obs) {
obs.observer.discardChanges();
}
},
// Hook user code entry points to poll observe-js
_listen: function(node, eventName, handler) {
var host = this;
Polymer.Base._listen.call(this, node, eventName, function() {
handler.apply(host, arguments);
host._observerCheckpoint();
});
},
async: function(callback, waitTime) {
var host = this;
Polymer.Base.async.call(this, function() {
callback.apply(host, arguments);
host._observerCheckpoint();
}, waitTime);
},
debounce: function(jobName, callback, wait) {
Polymer.Base.debounce.call(this, jobName, callback, function() {
callback.apply(this, arguments);
this._observerCheckpoint();
}, wait);
},
_observerCheckpoint: function() {
Polymer.ObserveJsBehavior.debouncer =
Polymer.Debounce(Polymer.ObserveJsBehavior.debouncer,
Platform.performMicrotaskCheckpoint);
}
};
// Poll observe-js all the time
setInterval(Platform.performMicrotaskCheckpoint, 125);
</script>