-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathchains.js
359 lines (301 loc) · 8.03 KB
/
chains.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import { get } from 'ember-metal/property_get';
import { meta as metaFor, peekMeta } from 'ember-metal/meta';
import { watchKey, unwatchKey } from 'ember-metal/watch_key';
import EmptyObject from 'ember-metal/empty_object';
var FIRST_KEY = /^([^\.]+)/;
function firstKey(path) {
return path.match(FIRST_KEY)[0];
}
function isObject(obj) {
return obj && (typeof obj === 'object');
}
function isVolatile(obj) {
return !(isObject(obj) && obj.isDescriptor && obj._volatile === false);
}
function ChainWatchers() {
// chain nodes that reference a key in this obj by key
// we only create ChainWatchers when we are going to add them
// so create this upfront
this.chains = new EmptyObject();
}
ChainWatchers.prototype = {
add(key, node) {
let nodes = this.chains[key];
if (nodes === undefined) {
this.chains[key] = [node];
} else {
nodes.push(node);
}
},
remove(key, node) {
let nodes = this.chains[key];
if (nodes) {
for (var i = 0; i < nodes.length; i++) {
if (nodes[i] === node) {
nodes.splice(i, 1);
break;
}
}
}
},
has(key, node) {
let nodes = this.chains[key];
if (nodes) {
for (var i = 0; i < nodes.length; i++) {
if (nodes[i] === node) {
return true;
}
}
}
return false;
},
revalidateAll() {
for (let key in this.chains) {
this.notify(key, true, undefined);
}
},
revalidate(key) {
this.notify(key, true, undefined);
},
// key: the string key that is part of a path changed
// revalidate: boolean; the chains that are watching this value should revalidate
// callback: function that will be called with the object and path that
// will be/are invalidated by this key change, depending on
// whether the revalidate flag is passed
notify(key, revalidate, callback) {
let nodes = this.chains[key];
if (nodes === undefined || nodes.length === 0) {
return;
}
let affected;
if (callback) {
affected = [];
}
for (let i = 0; i < nodes.length; i++) {
nodes[i].notify(revalidate, affected);
}
if (callback === undefined) {
return;
}
// we gather callbacks so we don't notify them during revalidation
for (let i = 0; i < affected.length; i += 2) {
let obj = affected[i];
let path = affected[i + 1];
callback(obj, path);
}
}
};
function makeChainWatcher() {
return new ChainWatchers();
}
function addChainWatcher(obj, keyName, node) {
if (!isObject(obj)) {
return;
}
let m = metaFor(obj);
m.writableChainWatchers(makeChainWatcher).add(keyName, node);
watchKey(obj, keyName, m);
}
function removeChainWatcher(obj, keyName, node) {
if (!isObject(obj)) {
return;
}
let m = peekMeta(obj);
if (!m || !m.readableChainWatchers()) {
return;
}
// make meta writable
m = metaFor(obj);
m.readableChainWatchers().remove(keyName, node);
unwatchKey(obj, keyName, m);
}
// A ChainNode watches a single key on an object. If you provide a starting
// value for the key then the node won't actually watch it. For a root node
// pass null for parent and key and object for value.
function ChainNode(parent, key, value) {
this._parent = parent;
this._key = key;
// _watching is true when calling get(this._parent, this._key) will
// return the value of this node.
//
// It is false for the root of a chain (because we have no parent)
// and for global paths (because the parent node is the object with
// the observer on it)
this._watching = (value === undefined);
this._chains = undefined;
this._object = undefined;
this.count = 0;
this._value = value;
this._paths = {};
if (this._watching) {
this._object = parent.value();
if (this._object) {
addChainWatcher(this._object, this._key, this);
}
}
}
function lazyGet(obj, key) {
if (!obj) {
return;
}
var meta = peekMeta(obj);
// check if object meant only to be a prototype
if (meta && meta.proto === obj) {
return;
}
// Use `get` if the return value is an EachProxy or an uncacheable value.
if (isVolatile(obj[key])) {
return get(obj, key);
// Otherwise attempt to get the cached value of the computed property
} else {
let cache = meta.readableCache();
if (cache && key in cache) {
return cache[key];
}
}
}
ChainNode.prototype = {
value() {
if (this._value === undefined && this._watching) {
var obj = this._parent.value();
this._value = lazyGet(obj, this._key);
}
return this._value;
},
destroy() {
if (this._watching) {
var obj = this._object;
if (obj) {
removeChainWatcher(obj, this._key, this);
}
this._watching = false; // so future calls do nothing
}
},
// copies a top level object only
copy(obj) {
var ret = new ChainNode(null, null, obj);
var paths = this._paths;
var path;
for (path in paths) {
// this check will also catch non-number vals.
if (paths[path] <= 0) {
continue;
}
ret.add(path);
}
return ret;
},
// called on the root node of a chain to setup watchers on the specified
// path.
add(path) {
let paths = this._paths;
paths[path] = (paths[path] || 0) + 1;
let key = firstKey(path);
let tail = path.slice(key.length + 1);
this.chain(key, tail);
},
// called on the root node of a chain to teardown watcher on the specified
// path
remove(path) {
let paths = this._paths;
if (paths[path] > 0) {
paths[path]--;
}
let key = firstKey(path);
let tail = path.slice(key.length + 1);
this.unchain(key, tail);
},
chain(key, path) {
var chains = this._chains;
var node;
if (chains === undefined) {
chains = this._chains = new EmptyObject();
} else {
node = chains[key];
}
if (node === undefined) {
node = chains[key] = new ChainNode(this, key, undefined);
}
node.count++; // count chains...
// chain rest of path if there is one
if (path) {
key = firstKey(path);
path = path.slice(key.length + 1);
node.chain(key, path);
}
},
unchain(key, path) {
var chains = this._chains;
var node = chains[key];
// unchain rest of path first...
if (path && path.length > 1) {
var nextKey = firstKey(path);
var nextPath = path.slice(nextKey.length + 1);
node.unchain(nextKey, nextPath);
}
// delete node if needed.
node.count--;
if (node.count <= 0) {
chains[node._key] = undefined;
node.destroy();
}
},
notify(revalidate, affected) {
if (revalidate && this._watching) {
var obj = this._parent.value();
if (obj !== this._object) {
removeChainWatcher(this._object, this._key, this);
this._object = obj;
addChainWatcher(obj, this._key, this);
}
this._value = undefined;
}
// then notify chains...
var chains = this._chains;
var node;
if (chains) {
for (var key in chains) {
node = chains[key];
if (node !== undefined) {
node.notify(revalidate, affected);
}
}
}
if (affected && this._parent) {
this._parent.populateAffected(this._key, 1, affected);
}
},
populateAffected(path, depth, affected) {
if (this._key) {
path = this._key + '.' + path;
}
if (this._parent) {
this._parent.populateAffected(path, depth + 1, affected);
} else {
if (depth > 1) {
affected.push(this.value(), path);
}
}
}
};
export function finishChains(obj) {
// We only create meta if we really have to
let m = peekMeta(obj);
if (m) {
m = metaFor(obj);
// finish any current chains node watchers that reference obj
let chainWatchers = m.readableChainWatchers();
if (chainWatchers) {
chainWatchers.revalidateAll();
}
// ensure that if we have inherited any chains they have been
// copied onto our own meta.
if (m.readableChains()) {
m.writableChains();
}
}
}
export {
removeChainWatcher,
ChainNode
};