-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b27e344
commit de44861
Showing
11 changed files
with
664 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/@ember/-internals/metal/lib/decorator-utils/-private/descriptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { DEBUG } from '@glimmer/env'; | ||
|
||
import { assert } from '@ember/debug'; | ||
|
||
const DESCRIPTOR = '__DESCRIPTOR__'; | ||
|
||
function isCPGetter(getter: any) { | ||
// Hack for descriptor traps, we want to be able to tell if the function | ||
// is a descriptor trap before we call it at all | ||
return getter !== null && typeof getter === 'function' && getter.toString().indexOf('CPGETTER_FUNCTION') !== -1; | ||
} | ||
|
||
function isDescriptorTrap(possibleDesc: any) { | ||
throw new Error('Cannot call `isDescriptorTrap` in production'); | ||
} | ||
|
||
export function isComputedDescriptor(possibleDesc: any) { | ||
return possibleDesc !== null && typeof possibleDesc === 'object' && possibleDesc.isDescriptor; | ||
} | ||
|
||
export function computedDescriptorFor(obj: any, keyName: any) { | ||
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 { value: possibleDesc, get: possibleCPGetter } = Object.getOwnPropertyDescriptor(obj, keyName); | ||
|
||
return isComputedDescriptor(possibleDesc) ? possibleDesc : undefined; | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/@ember/-internals/metal/lib/decorator-utils/-private/modifier-meta.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const MODIFIER_META_MAP = new WeakMap(); | ||
|
||
export function getModifierMeta(target: any) { | ||
return MODIFIER_META_MAP.get(target); | ||
} | ||
|
||
export function getOrCreateModifierMeta(target: any) { | ||
if (!MODIFIER_META_MAP.has(target)) { | ||
MODIFIER_META_MAP.set(target, {}); | ||
} | ||
|
||
return MODIFIER_META_MAP.get(target); | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/@ember/-internals/metal/lib/decorator-utils/collapse-proto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default function collapseProto(target: any) { | ||
// We must collapse the superclass prototype to make sure that the `actions` | ||
// object will exist. Since collapsing doesn't generally happen until a class is | ||
// instantiated, we have to do it manually. | ||
if (typeof target.constructor.proto === 'function') { | ||
target.constructor.proto(); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/@ember/-internals/metal/lib/decorator-utils/computed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// import { defineProperty } from '@ember/object'; | ||
import { decoratorWithParams, decoratorWithRequiredParams, decorator } from './decorator'; | ||
// import { HAS_NATIVE_COMPUTED_GETTERS } from 'ember-compatibility-helpers'; | ||
// import { NEEDS_STAGE_1_DECORATORS } from 'ember-decorators-flags'; | ||
|
||
import { computedDescriptorFor, isComputedDescriptor } from './-private/descriptor'; | ||
import { getModifierMeta, getOrCreateModifierMeta } from './-private/modifier-meta'; | ||
|
||
import { assert } from '@ember/debug'; | ||
|
||
export { computedDescriptorFor, getModifierMeta, getOrCreateModifierMeta }; | ||
|
||
/** | ||
* A macro that receives a decorator function which returns a ComputedProperty, | ||
* and defines that property using `Ember.defineProperty`. Conceptually, CPs | ||
* are custom property descriptors that require Ember's intervention to apply | ||
* correctly. In the future, we will use finishers to define the CPs rather than | ||
* directly defining them in the decorator function. | ||
* | ||
* @param {Function} fn - decorator function | ||
*/ | ||
function computedDecoratorInner(fn) { | ||
return (desc, params = []) => { | ||
// All computeds are methods | ||
desc.kind = 'method'; | ||
desc.placement = 'prototype'; | ||
|
||
desc.finisher = function initializeComputedProperty(target) { | ||
let { prototype } = target; | ||
let { key } = desc; | ||
|
||
assert(`ES6 property getters/setters only need to be decorated once, '${key}' was decorated on both the getter and the setter`, !computedDescriptorFor(prototype, key)); | ||
|
||
let computedDesc = fn(desc, params); | ||
|
||
assert(`computed decorators must return an instance of an Ember ComputedProperty descriptor, received ${computedDesc}`, isComputedDescriptor(computedDesc)); | ||
|
||
let modifierMeta = getModifierMeta(prototype); | ||
|
||
if (modifierMeta !== undefined && modifierMeta[key] !== undefined) { | ||
computedDesc[modifierMeta[key]](); | ||
} | ||
|
||
// if (!HAS_NATIVE_COMPUTED_GETTERS) { | ||
// // Until recent versions of Ember, computed properties would be defined | ||
// // by just setting them. We need to blow away any predefined properties | ||
// // (getters/setters, etc.) to allow Ember.defineProperty to work correctly. | ||
// Object.defineProperty(prototype, key, { | ||
// configurable: true, | ||
// writable: true, | ||
// enumerable: true, | ||
// value: undefined | ||
// }); | ||
// } | ||
|
||
// defineProperty(prototype, key, computedDesc); | ||
Object.defineProperty(prototype, key, computedDesc); | ||
|
||
// if (NEEDS_STAGE_1_DECORATORS) { | ||
// // There's currently no way to disable redefining the property when decorators | ||
// // are run, so return the property descriptor we just assigned | ||
// desc.descriptor = Object.getOwnPropertyDescriptor(prototype, key); | ||
// } | ||
|
||
return target; | ||
} | ||
} | ||
} | ||
|
||
export function computedDecorator(fn) { | ||
return decorator(computedDecoratorInner(fn)); | ||
} | ||
|
||
export function computedDecoratorWithParams(fn) { | ||
return decoratorWithParams(computedDecoratorInner(fn)); | ||
} | ||
|
||
export function computedDecoratorWithRequiredParams(fn, name) { | ||
return decoratorWithRequiredParams(computedDecoratorInner(fn), name); | ||
} |
Oops, something went wrong.