Skip to content

Commit

Permalink
Merge pull request #17572 from pzuraq/update-tracked
Browse files Browse the repository at this point in the history
[FEAT] Updates @Tracked to match the Tracked Properties RFC
  • Loading branch information
krisselden authored Feb 9, 2019
2 parents 528434e + 287afae commit c17949f
Show file tree
Hide file tree
Showing 22 changed files with 556 additions and 354 deletions.
6 changes: 3 additions & 3 deletions packages/@ember/-internals/metal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export {
PROPERTY_DID_CHANGE,
} from './lib/property_events';
export { defineProperty } from './lib/properties';
export { nativeDescDecorator } from './lib/decorator';
export {
descriptorForProperty,
isComputedDecorator,
setComputedDecorator,
nativeDescDecorator,
} from './lib/decorator';
} from './lib/descriptor_map';
export { watchKey, unwatchKey } from './lib/watch_key';
export { ChainNode, finishChains, removeChainWatcher } from './lib/chains';
export { watchPath, unwatchPath } from './lib/watch_path';
Expand All @@ -49,7 +49,7 @@ export { Mixin, aliasMethod, mixin, observer, applyMixin } from './lib/mixin';
export { default as inject, DEBUG_INJECTION_FUNCTIONS } from './lib/injected_property';
export { setHasViews, tagForProperty, tagFor, markObjectAsDirty } from './lib/tags';
export { default as runInTransaction, didRender, assertNotRendered } from './lib/transaction';
export { tracked } from './lib/tracked';
export { tracked, getCurrentTracker, setCurrentTracker } from './lib/tracked';

export {
NAMESPACES,
Expand Down
2 changes: 1 addition & 1 deletion packages/@ember/-internals/metal/lib/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
addDependentKeys,
ComputedDescriptor,
Decorator,
descriptorForDecorator,
makeComputedDecorator,
removeDependentKeys,
} from './decorator';
import { descriptorForDecorator } from './descriptor_map';
import { defineProperty } from './properties';
import { get } from './property_get';
import { set } from './property_set';
Expand Down
2 changes: 1 addition & 1 deletion packages/@ember/-internals/metal/lib/chains.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meta, meta as metaFor, peekMeta } from '@ember/-internals/meta';
import { getCachedValueFor } from './computed_cache';
import { descriptorForProperty } from './decorator';
import { descriptorForProperty } from './descriptor_map';
import { eachProxyFor } from './each_proxy';
import { get } from './property_get';
import { unwatchKey, watchKey } from './watch_key';
Expand Down
2 changes: 1 addition & 1 deletion packages/@ember/-internals/metal/lib/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import {
addDependentKeys,
ComputedDescriptor,
Decorator,
descriptorForDecorator,
makeComputedDecorator,
removeDependentKeys,
} from './decorator';
import { descriptorForDecorator } from './descriptor_map';
import expandProperties from './expand_properties';
import { defineProperty } from './properties';
import { notifyPropertyChange } from './property_events';
Expand Down
64 changes: 4 additions & 60 deletions packages/@ember/-internals/metal/lib/decorator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Meta, meta as metaFor, peekMeta } from '@ember/-internals/meta';
import { Meta, meta as metaFor } from '@ember/-internals/meta';
import { EMBER_NATIVE_DECORATOR_SUPPORT } from '@ember/canary-features';
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import { setComputedDecorator } from './descriptor_map';
import { unwatch, watch } from './watching';

const DECORATOR_DESCRIPTOR_MAP: WeakMap<Decorator, ComputedDescriptor | boolean> = new WeakMap();

// https://tc39.github.io/proposal-decorators/#sec-elementdescriptor-specification-type
export interface ElementDescriptor {
descriptor: PropertyDescriptor & { initializer?: any };
Expand All @@ -21,61 +20,6 @@ export type Decorator = (
isClassicDecorator?: boolean
) => ElementDescriptor;

// ..........................................................
// DESCRIPTOR
//

/**
Returns the CP descriptor assocaited with `obj` and `keyName`, if any.
@method descriptorFor
@param {Object} obj the object to check
@param {String} keyName the key to check
@return {Descriptor}
@private
*/
export function descriptorForProperty(obj: object, keyName: string, _meta?: Meta | null) {
assert('Cannot call `descriptorFor` on null', obj !== null);
assert('Cannot call `descriptorFor` on undefined', obj !== undefined);
assert(
`Cannot call \`descriptorFor\` on ${typeof obj}`,
typeof obj === 'object' || typeof obj === 'function'
);

let meta = _meta === undefined ? peekMeta(obj) : _meta;

if (meta !== null) {
return meta.peekDescriptors(keyName);
}
}

export function descriptorForDecorator(dec: Decorator) {
return DECORATOR_DESCRIPTOR_MAP.get(dec);
}

/**
Check whether a value is a decorator
@method isComputedDecorator
@param {any} possibleDesc the value to check
@return {boolean}
@private
*/
export function isComputedDecorator(dec: Decorator | null | undefined) {
return dec !== null && dec !== undefined && DECORATOR_DESCRIPTOR_MAP.has(dec);
}

/**
Set a value as a decorator
@method setComputedDecorator
@param {function} decorator the value to mark as a decorator
@private
*/
export function setComputedDecorator(dec: Decorator) {
DECORATOR_DESCRIPTOR_MAP.set(dec, true);
}

// ..........................................................
// DEPENDENT KEYS
//
Expand Down Expand Up @@ -188,7 +132,7 @@ export function makeComputedDecorator(

assert(
'Native decorators are not enabled without the EMBER_NATIVE_DECORATOR_SUPPORT flag',
EMBER_NATIVE_DECORATOR_SUPPORT ? !isClassicDecorator : isClassicDecorator
EMBER_NATIVE_DECORATOR_SUPPORT || isClassicDecorator
);

elementDesc.kind = 'method';
Expand All @@ -208,7 +152,7 @@ export function makeComputedDecorator(
return elementDesc;
};

DECORATOR_DESCRIPTOR_MAP.set(decorator, desc);
setComputedDecorator(decorator, desc);

Object.setPrototypeOf(decorator, DecoratorClass.prototype);

Expand Down
58 changes: 58 additions & 0 deletions packages/@ember/-internals/metal/lib/descriptor_map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Meta, peekMeta } from '@ember/-internals/meta';
import { assert } from '@ember/debug';

const DECORATOR_DESCRIPTOR_MAP: WeakMap<
import('./decorator').Decorator,
import('./decorator').ComputedDescriptor | boolean
> = new WeakMap();

/**
Returns the CP descriptor assocaited with `obj` and `keyName`, if any.
@method descriptorFor
@param {Object} obj the object to check
@param {String} keyName the key to check
@return {Descriptor}
@private
*/
export function descriptorForProperty(obj: object, keyName: string, _meta?: Meta | null) {
assert('Cannot call `descriptorFor` on null', obj !== null);
assert('Cannot call `descriptorFor` on undefined', obj !== undefined);
assert(
`Cannot call \`descriptorFor\` on ${typeof obj}`,
typeof obj === 'object' || typeof obj === 'function'
);

let meta = _meta === undefined ? peekMeta(obj) : _meta;

if (meta !== null) {
return meta.peekDescriptors(keyName);
}
}

export function descriptorForDecorator(dec: import('./decorator').Decorator) {
return DECORATOR_DESCRIPTOR_MAP.get(dec);
}

/**
Check whether a value is a decorator
@method isComputedDecorator
@param {any} possibleDesc the value to check
@return {boolean}
@private
*/
export function isComputedDecorator(dec: import('./decorator').Decorator | null | undefined) {
return dec !== null && dec !== undefined && DECORATOR_DESCRIPTOR_MAP.has(dec);
}

/**
Set a value as a decorator
@method setComputedDecorator
@param {function} decorator the value to mark as a decorator
@private
*/
export function setComputedDecorator(dec: import('./decorator').Decorator, value: any = true) {
DECORATOR_DESCRIPTOR_MAP.set(dec, value);
}
4 changes: 2 additions & 2 deletions packages/@ember/-internals/metal/lib/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import {
ComputedPropertyGetter,
ComputedPropertySetter,
} from './computed';
import { makeComputedDecorator } from './decorator';
import {
descriptorForDecorator,
descriptorForProperty,
isComputedDecorator,
makeComputedDecorator,
} from './decorator';
} from './descriptor_map';
import { addListener, removeListener } from './events';
import expandProperties from './expand_properties';
import { classToString, setUnprocessedMixins } from './namespace_search';
Expand Down
18 changes: 8 additions & 10 deletions packages/@ember/-internals/metal/lib/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
*/

import { Meta, meta as metaFor, peekMeta, UNDEFINED } from '@ember/-internals/meta';
import { EMBER_NATIVE_DECORATOR_SUPPORT } from '@ember/canary-features';
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import {
Decorator,
descriptorForProperty,
ElementDescriptor,
isComputedDecorator,
} from './decorator';
import { Decorator, ElementDescriptor } from './decorator';
import { descriptorForProperty, isComputedDecorator } from './descriptor_map';
import { overrideChains } from './property_events';

export type MandatorySetterFunction = ((this: object, value: any) => void) & {
Expand Down Expand Up @@ -155,16 +150,19 @@ export function defineProperty(

let value;
if (isComputedDecorator(desc)) {
let elementDesc: ElementDescriptor = {
let elementDesc = {
key: keyName,
kind: 'field',
placement: 'own',
descriptor: {
value: undefined,
},
};
toString() {
return '[object Descriptor]';
},
} as ElementDescriptor;

if (DEBUG && !EMBER_NATIVE_DECORATOR_SUPPORT) {
if (DEBUG) {
elementDesc = desc!(elementDesc, true);
} else {
elementDesc = desc!(elementDesc);
Expand Down
2 changes: 1 addition & 1 deletion packages/@ember/-internals/metal/lib/property_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Meta, peekMeta } from '@ember/-internals/meta';
import { symbol } from '@ember/-internals/utils';
import { DEBUG } from '@glimmer/env';
import changeEvent from './change_event';
import { descriptorForProperty } from './decorator';
import { descriptorForProperty } from './descriptor_map';
import { sendEvent } from './events';
import ObserverSet from './observer_set';
import { markObjectAsDirty } from './tags';
Expand Down
2 changes: 1 addition & 1 deletion packages/@ember/-internals/metal/lib/property_get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { HAS_NATIVE_PROXY, symbol } from '@ember/-internals/utils';
import { EMBER_METAL_TRACKED_PROPERTIES } from '@ember/canary-features';
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import { descriptorForProperty } from './decorator';
import { descriptorForProperty } from './descriptor_map';
import { isPath } from './path_cache';
import { tagForProperty } from './tags';
import { getCurrentTracker } from './tracked';
Expand Down
2 changes: 1 addition & 1 deletion packages/@ember/-internals/metal/lib/property_set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HAS_NATIVE_PROXY, lookupDescriptor, toString } from '@ember/-internals/
import { assert } from '@ember/debug';
import EmberError from '@ember/error';
import { DEBUG } from '@glimmer/env';
import { descriptorForProperty } from './decorator';
import { descriptorForProperty } from './descriptor_map';
import { isPath } from './path_cache';
import { MandatorySetterFunction } from './properties';
import { notifyPropertyChange } from './property_events';
Expand Down
Loading

0 comments on commit c17949f

Please sign in to comment.