Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
get(obj: object, keyName: string): any {
let ret: any;
let propertyTag = tagForProperty(obj, keyName) as UpdatableTag;
// We don't use the tag since CPs are not automatic, we just want to avoid
// anything tracking while we get the altKey
untrack(() => {
ret = get(obj, this.altKey);
});
let lastRevision = getLastRevisionFor(obj, keyName);
if (!validate(propertyTag, lastRevision)) {
update(propertyTag, combine(getChainTagsForKey(obj, this.altKey)));
setLastRevisionFor(obj, keyName, value(propertyTag));
finishLazyChains(obj, keyName, ret);
}
consume(propertyTag);
return ret;
}
// lookup the value to chain off of like normal.
if (!(segment in current) && typeof current.unknownProperty === 'function') {
current = current.unknownProperty(segment);
} else {
current = current[segment];
}
} else {
// If the descriptor is defined, then its a normal CP (not an alias, which
// would have been handled earlier). We get the last revision to check if
// the CP is still valid, and if so we use the cached value. If not, then
// we create a lazy chain lookup, and the next time the CP is caluculated,
// it will update that lazy chain.
let lastRevision = getLastRevisionFor(current, segment);
if (validate(propertyTag, lastRevision)) {
current = peekCacheFor(current).get(segment);
} else {
let lazyChains = metaFor(current).writableLazyChainsFor(segment);
let rest = path.substr(segmentEnd + 1);
let placeholderTag = lazyChains[rest];
if (placeholderTag === undefined) {
placeholderTag = lazyChains[rest] = createUpdatableTag();
}
chainTags.push(placeholderTag);
break;
}
get(obj: object, keyName: string): any {
if (this._volatile) {
return this._getter!.call(obj, keyName);
}
let cache = getCacheFor(obj);
let propertyTag = tagForProperty(obj, keyName) as UpdatableTag;
let ret;
if (cache.has(keyName) && validate(propertyTag, getLastRevisionFor(obj, keyName))) {
ret = cache.get(keyName);
} else {
// For backwards compatibility, we only throw if the CP has any dependencies. CPs without dependencies
// should be allowed, even after the object has been destroyed, which is why we check _dependentKeys.
assert(
`Attempted to access the computed ${obj}.${keyName} on a destroyed object, which is not allowed`,
this._dependentKeys === undefined || !metaFor(obj).isMetaDestroyed()
);
let upstreamTag: Tag | undefined = undefined;
if (this._auto === true) {
upstreamTag = track(() => {
ret = this._getter!.call(obj, keyName);
});
} else {
_revalidate() {
if (
!this._arrangedContentIsUpdating &&
!validate(this._arrangedContentTag, this._arrangedContentRevision)
) {
this._arrangedContentIsUpdating = true;
this._updateArrangedContentArray();
this._arrangedContentIsUpdating = false;
this._arrangedContentTag = combine(getChainTagsForKey(this, 'arrangedContent'));
this._arrangedContentRevision = value(this._arrangedContentTag);
}
}
}
activeObservers.forEach((observer, eventName) => {
if (!validate(observer.tag, observer.lastRevision)) {
let sendObserver = () => {
try {
sendEvent(target, eventName, [target, observer.path]);
} finally {
observer.tag = combine(getChainTagsForKey(target, observer.path));
observer.lastRevision = value(observer.tag);
}
};
if (shouldSchedule) {
schedule('actions', sendObserver);
} else {
sendObserver();
}
}
});