UNPKG

ember-legacy-class-transform

Version:
52 lines 1.77 kB
import { EMPTY_CACHE } from '../utils'; import { dict } from '@glimmer/util'; import Meta from '../meta'; import { PropertyReference } from './descriptors'; import { VOLATILE_TAG } from '@glimmer/reference'; export default class PathReference { constructor(parent, property) { this.cache = EMPTY_CACHE; this.inner = null; this.chains = null; this.lastParentValue = EMPTY_CACHE; this._guid = 0; this.tag = VOLATILE_TAG; this.parent = parent; this.property = property; } value() { 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) { let chains = this._getChains(); if (prop in chains) return chains[prop]; return chains[prop] = new PathReference(this, prop); } label() { return '[reference Direct]'; } _getChains() { if (this.chains) return this.chains; return this.chains = dict(); } _parentValue() { let parent = this.parent.value(); this.lastParentValue = parent; return parent; } } export { PathReference };