"Fossies" - the Fresh Open Source Software Archive

Member "ember.js-3.28.1/packages/@ember/-internals/metal/lib/alias.ts" (30 Aug 2021, 3486 Bytes) of package /linux/www/ember.js-3.28.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import { Meta, meta as metaFor } from '@ember/-internals/meta';
    2 import { inspect } from '@ember/-internals/utils';
    3 import { assert } from '@ember/debug';
    4 import EmberError from '@ember/error';
    5 import {
    6   consumeTag,
    7   tagFor,
    8   tagMetaFor,
    9   untrack,
   10   UpdatableTag,
   11   updateTag,
   12   validateTag,
   13   valueForTag,
   14 } from '@glimmer/validator';
   15 import { CHAIN_PASS_THROUGH, finishLazyChains, getChainTagsForKey } from './chain-tags';
   16 import {
   17   ComputedDescriptor,
   18   Decorator,
   19   descriptorForDecorator,
   20   isElementDescriptor,
   21   makeComputedDecorator,
   22 } from './decorator';
   23 import { defineProperty } from './properties';
   24 import { get } from './property_get';
   25 import { set } from './property_set';
   26 
   27 export type AliasDecorator = Decorator & PropertyDecorator & AliasDecoratorImpl;
   28 
   29 export default function alias(altKey: string): AliasDecorator {
   30   assert(
   31     'You attempted to use @alias as a decorator directly, but it requires a `altKey` parameter',
   32     !isElementDescriptor(Array.prototype.slice.call(arguments))
   33   );
   34 
   35   return makeComputedDecorator(new AliasedProperty(altKey), AliasDecoratorImpl) as AliasDecorator;
   36 }
   37 
   38 // TODO: This class can be svelted once `meta` has been deprecated
   39 class AliasDecoratorImpl extends Function {
   40   readOnly(this: Decorator) {
   41     (descriptorForDecorator(this) as AliasedProperty).readOnly();
   42     return this;
   43   }
   44 
   45   oneWay(this: Decorator) {
   46     (descriptorForDecorator(this) as AliasedProperty).oneWay();
   47     return this;
   48   }
   49 
   50   meta(this: Decorator, meta?: any): any {
   51     let prop = descriptorForDecorator(this) as AliasedProperty;
   52 
   53     if (arguments.length === 0) {
   54       return prop._meta || {};
   55     } else {
   56       prop._meta = meta;
   57     }
   58   }
   59 }
   60 
   61 export class AliasedProperty extends ComputedDescriptor {
   62   readonly altKey: string;
   63 
   64   constructor(altKey: string) {
   65     super();
   66 
   67     this.altKey = altKey;
   68   }
   69 
   70   setup(obj: object, keyName: string, propertyDesc: PropertyDescriptor, meta: Meta): void {
   71     assert(`Setting alias '${keyName}' on self`, this.altKey !== keyName);
   72     super.setup(obj, keyName, propertyDesc, meta);
   73     CHAIN_PASS_THROUGH.add(this);
   74   }
   75 
   76   get(obj: object, keyName: string): any {
   77     let ret: any;
   78 
   79     let meta = metaFor(obj);
   80     let tagMeta = tagMetaFor(obj);
   81     let propertyTag = tagFor(obj, keyName, tagMeta) as UpdatableTag;
   82 
   83     // We don't use the tag since CPs are not automatic, we just want to avoid
   84     // anything tracking while we get the altKey
   85     untrack(() => {
   86       ret = get(obj, this.altKey);
   87     });
   88 
   89     let lastRevision = meta.revisionFor(keyName);
   90 
   91     if (lastRevision === undefined || !validateTag(propertyTag, lastRevision)) {
   92       updateTag(propertyTag, getChainTagsForKey(obj, this.altKey, tagMeta, meta));
   93       meta.setRevisionFor(keyName, valueForTag(propertyTag));
   94       finishLazyChains(meta, keyName, ret);
   95     }
   96 
   97     consumeTag(propertyTag);
   98 
   99     return ret;
  100   }
  101 
  102   set(obj: object, _keyName: string, value: any): any {
  103     return set(obj, this.altKey, value);
  104   }
  105 
  106   readOnly(): void {
  107     this.set = AliasedProperty_readOnlySet;
  108   }
  109 
  110   oneWay(): void {
  111     this.set = AliasedProperty_oneWaySet;
  112   }
  113 }
  114 
  115 function AliasedProperty_readOnlySet(obj: object, keyName: string): never {
  116   // eslint-disable-line no-unused-vars
  117   throw new EmberError(`Cannot set read-only property '${keyName}' on object: ${inspect(obj)}`);
  118 }
  119 
  120 function AliasedProperty_oneWaySet(obj: object, keyName: string, value: any): any {
  121   defineProperty(obj, keyName, null);
  122   return set(obj, keyName, value);
  123 }