-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathmeta.js
712 lines (618 loc) · 17.4 KB
/
meta.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
import { lookupDescriptor, symbol, toString } from 'ember-utils';
import { protoMethods as listenerMethods } from './meta_listeners';
import { assert } from 'ember-debug';
import { DEBUG } from '@glimmer/env';
import { removeChainWatcher } from './chains';
import { ENV } from 'ember-environment';
let counters;
if (DEBUG) {
counters = {
peekCalls: 0,
peekParentCalls: 0,
peekPrototypeWalks: 0,
setCalls: 0,
deleteCalls: 0,
metaCalls: 0,
metaInstantiated: 0,
};
}
/**
@module ember
*/
export const UNDEFINED = symbol('undefined');
// FLAGS
const SOURCE_DESTROYING = 1 << 1;
const SOURCE_DESTROYED = 1 << 2;
const META_DESTROYED = 1 << 3;
const NODE_STACK = [];
export class Meta {
constructor(obj, parentMeta) {
if (DEBUG) {
counters.metaInstantiated++;
this._values = undefined;
}
this._descriptors = undefined;
this._watching = undefined;
this._mixins = undefined;
if (ENV._ENABLE_BINDING_SUPPORT) {
this._bindings = undefined;
}
this._deps = undefined;
this._chainWatchers = undefined;
this._chains = undefined;
this._tag = undefined;
this._tags = undefined;
// initial value for all flags right now is false
// see FLAGS const for detailed list of flags used
this._flags = 0;
// used only internally
this.source = obj;
// when meta(obj).proto === obj, the object is intended to be only a
// prototype and doesn't need to actually be observable itself
this.proto = undefined;
// The next meta in our inheritance chain. We (will) track this
// explicitly instead of using prototypical inheritance because we
// have detailed knowledge of how each property should really be
// inherited, and we can optimize it much better than JS runtimes.
this.parent = parentMeta;
this._listeners = undefined;
this._listenersFinalized = false;
}
isInitialized(obj) {
return this.proto !== obj;
}
destroy() {
if (this.isMetaDestroyed()) {
return;
}
// remove chainWatchers to remove circular references that would prevent GC
let nodes, key, nodeObject;
let node = this.readableChains();
if (node !== undefined) {
NODE_STACK.push(node);
// process tree
while (NODE_STACK.length > 0) {
node = NODE_STACK.pop();
// push children
nodes = node._chains;
if (nodes !== undefined) {
for (key in nodes) {
if (nodes[key] !== undefined) {
NODE_STACK.push(nodes[key]);
}
}
}
// remove chainWatcher in node object
if (node._isWatching) {
nodeObject = node._object;
if (nodeObject !== undefined) {
let foreignMeta = peekMeta(nodeObject);
// avoid cleaning up chain watchers when both current and
// foreign objects are being destroyed
// if both are being destroyed manual cleanup is not needed
// as they will be GC'ed and no non-destroyed references will
// be remaining
if (foreignMeta && !foreignMeta.isSourceDestroying()) {
removeChainWatcher(nodeObject, node._key, node, foreignMeta);
}
}
}
}
}
this.setMetaDestroyed();
}
isSourceDestroying() {
return this._hasFlag(SOURCE_DESTROYING);
}
setSourceDestroying() {
this._flags |= SOURCE_DESTROYING;
}
isSourceDestroyed() {
return this._hasFlag(SOURCE_DESTROYED);
}
setSourceDestroyed() {
this._flags |= SOURCE_DESTROYED;
}
isMetaDestroyed() {
return this._hasFlag(META_DESTROYED);
}
setMetaDestroyed() {
this._flags |= META_DESTROYED;
}
_hasFlag(flag) {
return (this._flags & flag) === flag;
}
_getOrCreateOwnMap(key) {
return this[key] || (this[key] = Object.create(null));
}
_getOrCreateOwnSet(key) {
return this[key] || (this[key] = new Set());
}
_getInherited(key) {
let pointer = this;
while (pointer !== undefined) {
let map = pointer[key];
if (map !== undefined) {
return map;
}
pointer = pointer.parent;
}
}
_findInherited(key, subkey) {
let pointer = this;
while (pointer !== undefined) {
let map = pointer[key];
if (map !== undefined) {
let value = map[subkey];
if (value !== undefined) {
return value;
}
}
pointer = pointer.parent;
}
}
_hasInInheritedSet(key, value) {
let pointer = this;
while (pointer !== undefined) {
let set = pointer[key];
if (set !== undefined) {
if (set.has(value)) {
return true;
}
}
pointer = pointer.parent;
}
return false;
}
// Implements a member that provides a lazily created map of maps,
// with inheritance at both levels.
writeDeps(subkey, itemkey, value) {
assert(
this.isMetaDestroyed() &&
`Cannot modify dependent keys for \`${itemkey}\` on \`${toString(
this.source
)}\` after it has been destroyed.`,
!this.isMetaDestroyed()
);
let outerMap = this._getOrCreateOwnMap('_deps');
let innerMap = outerMap[subkey];
if (innerMap === undefined) {
innerMap = outerMap[subkey] = Object.create(null);
}
innerMap[itemkey] = value;
}
peekDeps(subkey, itemkey) {
let pointer = this;
while (pointer !== undefined) {
let map = pointer._deps;
if (map !== undefined) {
let value = map[subkey];
if (value !== undefined) {
let itemvalue = value[itemkey];
if (itemvalue !== undefined) {
return itemvalue;
}
}
}
pointer = pointer.parent;
}
}
hasDeps(subkey) {
let pointer = this;
while (pointer !== undefined) {
let deps = pointer._deps;
if (deps !== undefined && deps[subkey] !== undefined) {
return true;
}
pointer = pointer.parent;
}
return false;
}
forEachInDeps(subkey, fn) {
return this._forEachIn('_deps', subkey, fn);
}
_forEachIn(key, subkey, fn) {
let pointer = this;
let seen;
let calls;
while (pointer !== undefined) {
let map = pointer[key];
if (map !== undefined) {
let innerMap = map[subkey];
if (innerMap !== undefined) {
for (let innerKey in innerMap) {
seen = seen === undefined ? new Set() : seen;
if (!seen.has(innerKey)) {
seen.add(innerKey);
calls = calls || [];
calls.push(innerKey, innerMap[innerKey]);
}
}
}
}
pointer = pointer.parent;
}
if (calls !== undefined) {
for (let i = 0; i < calls.length; i += 2) {
fn(calls[i], calls[i + 1]);
}
}
}
writableTags() {
return this._getOrCreateOwnMap('_tags');
}
readableTags() {
return this._tags;
}
writableTag(create) {
assert(
this.isMetaDestroyed() &&
`Cannot create a new tag for \`${toString(this.source)}\` after it has been destroyed.`,
!this.isMetaDestroyed()
);
let ret = this._tag;
if (ret === undefined) {
ret = this._tag = create(this.source);
}
return ret;
}
readableTag() {
return this._tag;
}
writableChainWatchers(create) {
assert(
this.isMetaDestroyed() &&
`Cannot create a new chain watcher for \`${toString(
this.source
)}\` after it has been destroyed.`,
!this.isMetaDestroyed()
);
let ret = this._chainWatchers;
if (ret === undefined) {
ret = this._chainWatchers = create(this.source);
}
return ret;
}
readableChainWatchers() {
return this._chainWatchers;
}
writableChains(create) {
assert(
this.isMetaDestroyed() &&
`Cannot create a new chains for \`${toString(this.source)}\` after it has been destroyed.`,
!this.isMetaDestroyed()
);
let ret = this._chains;
if (ret === undefined) {
if (this.parent === undefined) {
ret = create(this.source);
} else {
ret = this.parent.writableChains(create).copy(this.source);
}
this._chains = ret;
}
return ret;
}
readableChains() {
return this._getInherited('_chains');
}
writeWatching(subkey, value) {
assert(
this.isMetaDestroyed() &&
`Cannot update watchers for \`${subkey}\` on \`${toString(
this.source
)}\` after it has been destroyed.`,
!this.isMetaDestroyed()
);
let map = this._getOrCreateOwnMap('_watching');
map[subkey] = value;
}
peekWatching(subkey) {
return this._findInherited('_watching', subkey);
}
addMixin(mixin) {
assert(
this.isMetaDestroyed() &&
`Cannot add mixins of \`${toString(mixin)}\` on \`${toString(
this.source
)}\` call addMixin after it has been destroyed.`,
!this.isMetaDestroyed()
);
let set = this._getOrCreateOwnSet('_mixins');
set.add(mixin);
}
hasMixin(mixin) {
return this._hasInInheritedSet('_mixins', mixin);
}
forEachMixins(fn) {
let pointer = this;
let seen;
while (pointer !== undefined) {
let set = pointer._mixins;
if (set !== undefined) {
seen = seen === undefined ? new Set() : seen;
set.forEach(mixin => {
if (!seen.has(mixin)) {
seen.add(mixin);
fn(mixin);
}
});
}
pointer = pointer.parent;
}
}
writeBindings(subkey, value) {
assert(
'Cannot invoke `meta.writeBindings` when EmberENV._ENABLE_BINDING_SUPPORT is not set',
ENV._ENABLE_BINDING_SUPPORT
);
assert(
this.isMetaDestroyed() &&
`Cannot add a binding for \`${subkey}\` on \`${toString(
this.source
)}\` after it has been destroyed.`,
!this.isMetaDestroyed()
);
let map = this._getOrCreateOwnMap('_bindings');
map[subkey] = value;
}
peekBindings(subkey) {
assert(
'Cannot invoke `meta.peekBindings` when EmberENV._ENABLE_BINDING_SUPPORT is not set',
ENV._ENABLE_BINDING_SUPPORT
);
return this._findInherited('_bindings', subkey);
}
forEachBindings(fn) {
assert(
'Cannot invoke `meta.forEachBindings` when EmberENV._ENABLE_BINDING_SUPPORT is not set',
ENV._ENABLE_BINDING_SUPPORT
);
let pointer = this;
let seen;
while (pointer !== undefined) {
let map = pointer._bindings;
if (map !== undefined) {
for (let key in map) {
seen = seen || Object.create(null);
if (seen[key] === undefined) {
seen[key] = true;
fn(key, map[key]);
}
}
}
pointer = pointer.parent;
}
}
clearBindings() {
assert(
'Cannot invoke `meta.clearBindings` when EmberENV._ENABLE_BINDING_SUPPORT is not set',
ENV._ENABLE_BINDING_SUPPORT
);
assert(
this.isMetaDestroyed() &&
`Cannot clear bindings on \`${toString(this.source)}\` after it has been destroyed.`,
!this.isMetaDestroyed()
);
this._bindings = undefined;
}
writeDescriptors(subkey, value) {
assert(
this.isMetaDestroyed() &&
`Cannot update descriptors for \`${subkey}\` on \`${toString(
this.source
)}\` after it has been destroyed.`,
!this.isMetaDestroyed()
);
let map = this._getOrCreateOwnMap('_descriptors');
map[subkey] = value;
}
peekDescriptors(subkey) {
let possibleDesc = this._findInherited('_descriptors', subkey);
return possibleDesc === UNDEFINED ? undefined : possibleDesc;
}
removeDescriptors(subkey) {
this.writeDescriptors(subkey, UNDEFINED);
}
forEachDescriptors(fn) {
let pointer = this;
let seen;
while (pointer !== undefined) {
let map = pointer._descriptors;
if (map !== undefined) {
for (let key in map) {
seen = seen === undefined ? new Set() : seen;
if (!seen.has(key)) {
seen.add(key);
let value = map[key];
if (value !== UNDEFINED) {
fn(key, value);
}
}
}
}
pointer = pointer.parent;
}
}
}
for (let name in listenerMethods) {
Meta.prototype[name] = listenerMethods[name];
}
if (DEBUG) {
Meta.prototype.writeValues = function(subkey, value) {
assert(
this.isMetaDestroyed() &&
`Cannot set the value of \`${subkey}\` on \`${toString(
this.source
)}\` after it has been destroyed.`,
!this.isMetaDestroyed()
);
let map = this._getOrCreateOwnMap('_values');
map[subkey] = value;
};
Meta.prototype.peekValues = function(subkey) {
return this._findInherited('_values', subkey);
};
Meta.prototype.deleteFromValues = function(subkey) {
delete this._getOrCreateOwnMap('_values')[subkey];
};
Meta.prototype.readInheritedValue = function(key, subkey) {
let internalKey = `_${key}`;
let pointer = this;
while (pointer !== undefined) {
let map = pointer[internalKey];
if (map !== undefined) {
let value = map[subkey];
if (value !== undefined || subkey in map) {
return value;
}
}
pointer = pointer.parent;
}
return UNDEFINED;
};
Meta.prototype.writeValue = function(obj, key, value) {
let descriptor = lookupDescriptor(obj, key);
let isMandatorySetter =
descriptor !== null && descriptor.set && descriptor.set.isMandatorySetter;
if (isMandatorySetter) {
this.writeValues(key, value);
} else {
obj[key] = value;
}
};
}
const getPrototypeOf = Object.getPrototypeOf;
const metaStore = new WeakMap();
export function setMeta(obj, meta) {
assert('Cannot call `setMeta` on null', obj !== null);
assert('Cannot call `setMeta` on undefined', obj !== undefined);
assert(
`Cannot call \`setMeta\` on ${typeof obj}`,
typeof obj === 'object' || typeof obj === 'function'
);
if (DEBUG) {
counters.setCalls++;
}
metaStore.set(obj, meta);
}
export function peekMeta(obj) {
assert('Cannot call `peekMeta` on null', obj !== null);
assert('Cannot call `peekMeta` on undefined', obj !== undefined);
assert(
`Cannot call \`peekMeta\` on ${typeof obj}`,
typeof obj === 'object' || typeof obj === 'function'
);
let pointer = obj;
let meta;
while (pointer !== undefined && pointer !== null) {
meta = metaStore.get(pointer);
// jshint loopfunc:true
if (DEBUG) {
counters.peekCalls++;
}
if (meta !== undefined) {
return meta;
}
pointer = getPrototypeOf(pointer);
if (DEBUG) {
counters.peekPrototypeWalks++;
}
}
}
/**
Tears down the meta on an object so that it can be garbage collected.
Multiple calls will have no effect.
@method deleteMeta
@for Ember
@param {Object} obj the object to destroy
@return {void}
@private
*/
export function deleteMeta(obj) {
assert('Cannot call `deleteMeta` on null', obj !== null);
assert('Cannot call `deleteMeta` on undefined', obj !== undefined);
assert(
`Cannot call \`deleteMeta\` on ${typeof obj}`,
typeof obj === 'object' || typeof obj === 'function'
);
if (DEBUG) {
counters.deleteCalls++;
}
let meta = peekMeta(obj);
if (meta !== undefined) {
meta.destroy();
}
}
/**
Retrieves the meta hash for an object. If `writable` is true ensures the
hash is writable for this object as well.
The meta object contains information about computed property descriptors as
well as any watched properties and other information. You generally will
not access this information directly but instead work with higher level
methods that manipulate this hash indirectly.
@method meta
@for Ember
@private
@param {Object} obj The object to retrieve meta for
@param {Boolean} [writable=true] Pass `false` if you do not intend to modify
the meta hash, allowing the method to avoid making an unnecessary copy.
@return {Object} the meta hash for an object
*/
export function meta(obj) {
assert('Cannot call `meta` on null', obj !== null);
assert('Cannot call `meta` on undefined', obj !== undefined);
assert(
`Cannot call \`meta\` on ${typeof obj}`,
typeof obj === 'object' || typeof obj === 'function'
);
if (DEBUG) {
counters.metaCalls++;
}
let maybeMeta = peekMeta(obj);
let parent;
// remove this code, in-favor of explicit parent
if (maybeMeta !== undefined) {
if (maybeMeta.source === obj) {
return maybeMeta;
}
parent = maybeMeta;
}
let newMeta = new Meta(obj, parent);
setMeta(obj, newMeta);
return newMeta;
}
if (DEBUG) {
meta._counters = counters;
}
/**
Returns the CP descriptor assocaited with `obj` and `keyName`, if any.
@method descriptorFor
@param {Object} obj the object to check
@param {String} keyName the key to check
@return {Descriptor}
@private
*/
export function descriptorFor(obj, keyName, _meta) {
assert('Cannot call `descriptorFor` on null', obj !== null);
assert('Cannot call `descriptorFor` on undefined', obj !== undefined);
assert(
`Cannot call \`descriptorFor\` on ${typeof obj}`,
typeof obj === 'object' || typeof obj === 'function'
);
let meta = _meta === undefined ? peekMeta(obj) : _meta;
if (meta !== undefined) {
return meta.peekDescriptors(keyName);
}
}
/**
Check whether a value is a CP descriptor.
@method descriptorFor
@param {any} possibleDesc the value to check
@return {boolean}
@private
*/
export function isDescriptor(possibleDesc) {
return possibleDesc !== null && typeof possibleDesc === 'object' && possibleDesc.isDescriptor;
}
export { counters };