import { EMPTY_CACHE } from '../utils'; import { dict } from '@glimmer/util'; import Meta from '../meta'; import { PropertyReference } from './descriptors'; import { VOLATILE_TAG, PathReference as IPathReference, Reference, RevisionTag } from '@glimmer/reference'; import { Dict, HasGuid } from '@glimmer/util'; export default class PathReference implements IPathReference, HasGuid { private parent: IPathReference; private property: string; protected cache: any = EMPTY_CACHE; private inner: Reference = null; private chains: Dict> = null; private lastParentValue: any = EMPTY_CACHE; public _guid = null; public tag: RevisionTag = VOLATILE_TAG; constructor(parent: IPathReference, property: string) { this.parent = parent; this.property = property; } value(): any { let { lastParentValue, property, inner } = this; let parentValue = this._parentValue(); if (parentValue === null || parentValue === undefined) { return (this.cache = undefined); } if (lastParentValue === parentValue) { inner = this.inner; } else { let ReferenceType = typeof parentValue === 'object' ? Meta.for(parentValue).referenceTypeFor(property) : PropertyReference; inner = this.inner = new ReferenceType(parentValue, property, this); } // if (typeof parentValue === 'object') { // Meta.for(parentValue).addReference(property, this); // } return (this.cache = inner.value()); } get(prop: string): IPathReference { let chains = this._getChains(); if (prop in chains) return chains[prop]; return (chains[prop] = new PathReference(this, prop)); } label(): string { return '[reference Direct]'; } private _getChains(): Dict> { if (this.chains) return this.chains; return (this.chains = dict>()); } private _parentValue() { let parent = this.parent.value(); this.lastParentValue = parent; return parent; } } export { PathReference };