forked from emberjs/ember.js
/
chains.ts
367 lines (312 loc) · 9.31 KB
/
chains.ts
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
360
361
362
363
364
365
366
367
import { descriptorFor, Meta, meta as metaFor, peekMeta } from 'ember-meta';
import { getCachedValueFor } from './computed_cache';
import { eachProxyFor } from './each_proxy';
import { get } from './property_get';
import { unwatchKey, watchKey } from './watch_key';
function isObject(obj: any): boolean {
return typeof obj === 'object' && obj !== null;
}
function isVolatile(obj: any, keyName: string, meta?: Meta): boolean {
let desc = descriptorFor(obj, keyName, meta);
return !(desc !== undefined && desc._volatile === false);
}
class 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
chains: { [key: string]: ChainNode[] } = Object.create(null);
add(key: string, node: ChainNode): void {
let nodes = this.chains[key];
if (nodes === undefined) {
this.chains[key] = [node];
} else {
nodes.push(node);
}
}
remove(key: string, node: ChainNode): void {
let nodes = this.chains[key];
if (nodes !== undefined) {
for (let i = 0; i < nodes.length; i++) {
if (nodes[i] === node) {
nodes.splice(i, 1);
break;
}
}
}
}
has(key: string, node: ChainNode): boolean {
let nodes = this.chains[key];
if (nodes !== undefined) {
for (let i = 0; i < nodes.length; i++) {
if (nodes[i] === node) {
return true;
}
}
}
return false;
}
revalidateAll(): void {
for (let key in this.chains) {
this.notify(key, true, undefined);
}
}
revalidate(key: string): void {
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: string, revalidate: boolean, callback?: (obj: object, path: string) => void): void {
let nodes = this.chains[key];
if (nodes === undefined || nodes.length === 0) {
return;
}
let affected: (any | string)[] | undefined = undefined;
if (callback !== undefined) {
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(): ChainWatchers {
return new ChainWatchers();
}
function makeChainNode(obj: any): ChainNode {
return new ChainNode(null, null as any, obj);
}
function addChainWatcher(obj: object, keyName: string, node: ChainNode): void {
let m = metaFor(obj);
m.writableChainWatchers(makeChainWatcher).add(keyName, node);
watchKey(obj, keyName, m);
}
function removeChainWatcher(obj: object, keyName: string, node: ChainNode, _meta?: Meta): void {
if (!isObject(obj)) {
return;
}
let meta = _meta === undefined ? peekMeta(obj) : _meta;
if (
meta === undefined ||
meta.isSourceDestroying() ||
meta.isMetaDestroyed() ||
meta.readableChainWatchers() === undefined
) {
return;
}
// make meta writable
meta = metaFor(obj);
meta.readableChainWatchers().remove(keyName, node);
unwatchKey(obj, keyName, meta);
}
const NODE_STACK: ChainNode[] = [];
function destroyRoot(root: ChainNode) {
pushChildren(root);
while (NODE_STACK.length > 0) {
let node = NODE_STACK.pop()!;
pushChildren(node);
destroyOne(node);
}
}
function destroyOne(node: ChainNode) {
if (node.isWatching) {
removeChainWatcher(node.object!, node.key, node);
node.isWatching = false;
}
}
function pushChildren(node: ChainNode) {
let nodes = node.chains;
if (nodes !== undefined) {
for (let key in nodes) {
if (nodes[key] !== undefined) {
NODE_STACK.push(nodes[key]!);
}
}
}
}
// 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.
class ChainNode {
private readonly parent: ChainNode | null;
private paths: { [key: string]: number } | undefined = undefined;
private content: any;
readonly key: string;
isWatching = false;
chains: { [key: string]: ChainNode | undefined } | undefined = undefined;
object: object | undefined = undefined;
count = 0;
constructor(parent: ChainNode | null, key: string, value: any) {
this.parent = parent;
this.key = key;
this.content = value;
// It is false for the root of a chain (because we have no parent)
let isWatching = (this.isWatching = parent !== null);
if (isWatching) {
let parentValue = parent!.value();
if (isObject(parentValue)) {
this.object = parentValue;
addChainWatcher(parentValue, key, this);
}
}
}
value(): any {
if (this.content === undefined && this.isWatching) {
let obj = this.parent!.value();
this.content = lazyGet(obj, this.key);
}
return this.content;
}
destroy(): void {
// check if root
if (this.parent === null) {
destroyRoot(this);
} else {
destroyOne(this);
}
}
// copies a top level object only
copyTo(target: ChainNode) {
let paths = this.paths;
if (paths !== undefined) {
let path;
for (path in paths) {
if (paths[path] > 0) {
target.add(path);
}
}
}
}
// called on the root node of a chain to setup watchers on the specified
// path.
add(path: string): void {
let paths = this.paths || (this.paths = {});
paths[path] = (paths[path] || 0) + 1;
let tails = path.split('.');
this.chain(tails.shift()!, tails);
}
// called on the root node of a chain to teardown watcher on the specified
// path
remove(path: string): void {
let paths = this.paths;
if (paths === undefined) {
return;
}
if (paths[path] > 0) {
paths[path]--;
}
let tails = path.split('.');
this.unchain(tails.shift()!, tails);
}
chain(key: string, tails: string[]): void {
let chains = this.chains;
if (chains === undefined) {
chains = this.chains = Object.create(null);
}
let 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 (tails.length > 0) {
node.chain(tails.shift()!, tails);
}
}
unchain(key: string, tails: string[]): void {
let chains = this.chains!;
let node = chains[key]!;
// unchain rest of path first...
if (tails.length > 0) {
node.unchain(tails.shift()!, tails);
}
// delete node if needed.
node.count--;
if (node.count <= 0) {
chains[node.key] = undefined;
node.destroy();
}
}
notify(revalidate: boolean, affected?: (any | string)[]): void {
if (revalidate && this.isWatching) {
let parentValue = this.parent!.value();
if (parentValue !== this.object) {
removeChainWatcher(this.object!, this.key, this);
if (isObject(parentValue)) {
this.object = parentValue;
addChainWatcher(parentValue, this.key, this);
} else {
this.object = undefined;
}
}
this.content = undefined;
}
// then notify chains...
let chains = this.chains;
if (chains !== undefined) {
let node;
for (let key in chains) {
node = chains[key];
if (node !== undefined) {
node.notify(revalidate, affected);
}
}
}
if (affected !== undefined && this.parent !== null) {
this.parent.populateAffected(this.key, 1, affected);
}
}
populateAffected(path: string, depth: number, affected: (any | string)[]) {
if (this.key) {
path = `${this.key}.${path}`;
}
if (this.parent !== null) {
this.parent.populateAffected(path, depth + 1, affected);
} else if (depth > 1) {
affected.push(this.value(), path);
}
}
}
function lazyGet(obj: object, key: string): any {
if (!isObject(obj)) {
return;
}
let meta = peekMeta(obj);
// check if object meant only to be a prototype
if (meta !== undefined && meta.proto === obj) {
return;
}
// Use `get` if the return value is an EachProxy or an uncacheable value.
if (key === '@each') {
return eachProxyFor(obj as any[]);
} else if (isVolatile(obj, key, meta)) {
return get(obj, key);
// Otherwise attempt to get the cached value of the computed property
} else {
return getCachedValueFor(obj, key);
}
}
function finishChains(meta: Meta): void {
// finish any current chains node watchers that reference obj
let chainWatchers = meta.readableChainWatchers();
if (chainWatchers !== undefined) {
chainWatchers.revalidateAll();
}
// ensure that if we have inherited any chains they have been
// copied onto our own meta.
if (meta.readableChains() !== undefined) {
meta.writableChains(makeChainNode);
}
}
export { finishChains, makeChainNode, removeChainWatcher, ChainNode };