UNPKG

ember-legacy-class-transform

Version:
122 lines 3.77 kB
import { PropertyReference } from './references/descriptors'; import RootReference from './references/root'; import { DictSet, dict } from '@glimmer/util'; import { VOLATILE_TAG } from '@glimmer/reference'; const NOOP_DESTROY = { destroy() {} }; class ConstPath { constructor(parent, _property) { this.tag = VOLATILE_TAG; this.parent = parent; } chain() { return NOOP_DESTROY; } notify() {} value() { return this.parent[this.property]; } get(prop) { return new ConstPath(this.parent[this.property], prop); } } class ConstRoot { constructor(value) { this.tag = VOLATILE_TAG; this.inner = value; } update(inner) { this.inner = inner; } chain() { return NOOP_DESTROY; } notify() {} value() { return this.inner; } referenceFromParts(_parts) { throw new Error("Not implemented"); } chainFor(_prop) { throw new Error("Not implemented"); } get(prop) { return new ConstPath(this.inner, prop); } } class ConstMeta /*implements IMeta*/ { constructor(object) { this.object = object; } root() { return new ConstRoot(this.object); } } export const CLASS_META = "df8be4c8-4e89-44e2-a8f9-550c8dacdca7"; const hasOwnProperty = Object.hasOwnProperty; class Meta { constructor(object, { RootReferenceFactory, DefaultPathReferenceFactory }) { this.references = null; this.slots = null; this.referenceTypes = null; this.propertyMetadata = null; this.object = object; this.RootReferenceFactory = RootReferenceFactory || RootReference; this.DefaultPathReferenceFactory = DefaultPathReferenceFactory || PropertyReference; } static for(obj) { if (obj === null || obj === undefined) return new Meta(obj, {}); if (hasOwnProperty.call(obj, '_meta') && obj._meta) return obj._meta; if (!Object.isExtensible(obj)) return new ConstMeta(obj); let MetaToUse = Meta; if (obj.constructor && obj.constructor[CLASS_META]) { let classMeta = obj.constructor[CLASS_META]; MetaToUse = classMeta.InstanceMetaConstructor; } else if (obj[CLASS_META]) { MetaToUse = obj[CLASS_META].InstanceMetaConstructor; } return obj._meta = new MetaToUse(obj, {}); } static exists(obj) { return typeof obj === 'object' && obj._meta; } static metadataForProperty(_key) { return null; } addReference(property, reference) { let refs = this.references = this.references || dict(); let set = refs[property] = refs[property] || new DictSet(); set.add(reference); } addReferenceTypeFor(property, type) { this.referenceTypes = this.referenceTypes || dict(); this.referenceTypes[property] = type; } referenceTypeFor(property) { if (!this.referenceTypes) return PropertyReference; return this.referenceTypes[property] || PropertyReference; } removeReference(property, reference) { if (!this.references) return; let set = this.references[property]; set.delete(reference); } getReferenceTypes() { this.referenceTypes = this.referenceTypes || dict(); return this.referenceTypes; } referencesFor(property) { if (!this.references) return null; return this.references[property]; } getSlots() { return this.slots = this.slots || dict(); } root() { return this.rootCache = this.rootCache || new this.RootReferenceFactory(this.object); } } export default Meta; export function metaFor(obj) { return Meta.for(obj); }