本文整理汇总了TypeScript中@ember/-internals/meta.descriptorFor函数的典型用法代码示例。如果您正苦于以下问题:TypeScript descriptorFor函数的具体用法?TypeScript descriptorFor怎么用?TypeScript descriptorFor使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了descriptorFor函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: key
/**
This function is called just after an object property has changed.
It will notify any observers and clear caches among other things.
Normally you will not need to call this method directly but if for some
reason you can't directly watch a property you can invoke this method
manually.
@method notifyPropertyChange
@for @ember/object
@param {Object} obj The object with the property that will change
@param {String} keyName The property key (or path) that will change.
@param {Meta} meta The objects meta.
@return {void}
@since 3.1.0
@public
*/
function notifyPropertyChange(obj: object, keyName: string, _meta?: Meta | null): void {
let meta = _meta === undefined ? peekMeta(obj) : _meta;
if (meta !== null && (meta.isInitializing() || meta.isPrototypeMeta(obj))) {
return;
}
let possibleDesc = descriptorFor(obj, keyName, meta);
if (possibleDesc !== undefined && typeof possibleDesc.didChange === 'function') {
possibleDesc.didChange(obj, keyName);
}
if (meta !== null && meta.peekWatching(keyName) > 0) {
dependentKeysDidChange(obj, keyName, meta);
chainsDidChange(obj, keyName, meta);
notifyObservers(obj, keyName, meta);
}
if (PROPERTY_DID_CHANGE in obj) {
obj[PROPERTY_DID_CHANGE](keyName);
}
if (meta !== null) {
if (meta.isSourceDestroying()) {
return;
}
markObjectAsDirty(obj, keyName, meta);
}
if (DEBUG) {
assertNotRendered(obj, keyName);
}
}
示例2: get
export function get(obj: object, keyName: string): any {
assert(
`Get must be called with two arguments; an object and a property key`,
arguments.length === 2
);
assert(
`Cannot call get with '${keyName}' on an undefined object.`,
obj !== undefined && obj !== null
);
assert(
`The key provided to get must be a string or number, you passed ${keyName}`,
typeof keyName === 'string' || (typeof keyName === 'number' && !isNaN(keyName))
);
assert(
`'this' in paths is not supported`,
typeof keyName !== 'string' || keyName.lastIndexOf('this.', 0) !== 0
);
let type = typeof obj;
let isObject = type === 'object';
let isFunction = type === 'function';
let isObjectLike = isObject || isFunction;
let value: any;
if (isObjectLike) {
if (EMBER_METAL_TRACKED_PROPERTIES) {
let tracker = getCurrentTracker();
if (tracker) tracker.add(tagForProperty(obj, keyName));
}
let descriptor = descriptorFor(obj, keyName);
if (descriptor !== undefined) {
return descriptor.get(obj, keyName);
}
if (DEBUG && HAS_NATIVE_PROXY) {
value = getPossibleMandatoryProxyValue(obj, keyName);
} else {
value = obj[keyName];
}
} else {
value = obj[keyName];
}
if (value === undefined) {
if (isPath(keyName)) {
return _getPath(obj, keyName);
}
if (
isObject &&
!(keyName in obj) &&
typeof (obj as MaybeHasUnknownProperty).unknownProperty === 'function'
) {
return (obj as MaybeHasUnknownProperty).unknownProperty!(keyName);
}
}
return value;
}
示例3: giveMethodSuper
function giveMethodSuper(
obj: object,
key: string,
method: Function,
values: { [key: string]: any },
descs: { [key: string]: any }
) {
// Methods overwrite computed properties, and do not call super to them.
if (descs[key] !== undefined) {
return method;
}
// Find the original method in a parent mixin
let superMethod = values[key];
// If we didn't find the original value in a parent mixin, find it in
// the original object
if (superMethod === undefined && descriptorFor(obj, key) === undefined) {
superMethod = obj[key];
}
// Only wrap the new method if the original method was a function
if (typeof superMethod === 'function') {
return wrap(method, superMethod);
}
return method;
}
示例4: descriptorFor
meta.forEachInDeps(depKey, (key: string) => {
possibleDesc = descriptorFor(obj, key, meta);
if (possibleDesc !== undefined && possibleDesc._suspended === obj) {
return;
}
method(obj, key, meta);
});
示例5: applyMixin
export function applyMixin(obj: { [key: string]: any }, mixins: Mixin[]) {
let descs = {};
let values = {};
let meta = metaFor(obj);
let keys: string[] = [];
let key, value, desc;
(obj as any)._super = ROOT;
// Go through all mixins and hashes passed in, and:
//
// * Handle concatenated properties
// * Handle merged properties
// * Set up _super wrapping if necessary
// * Set up computed property descriptors
// * Copying `toString` in broken browsers
mergeMixins(mixins, meta, descs, values, obj, keys);
for (let i = 0; i < keys.length; i++) {
key = keys[i];
if (key === 'constructor' || !values.hasOwnProperty(key)) {
continue;
}
desc = descs[key];
value = values[key];
while (desc && desc instanceof Alias) {
let followed = followMethodAlias(obj, desc, descs, values);
desc = followed.desc;
value = followed.value;
}
if (desc === undefined && value === undefined) {
continue;
}
if (descriptorFor(obj, key) !== undefined) {
replaceObserversAndListeners(obj, key, null, value);
} else {
replaceObserversAndListeners(obj, key, obj[key], value);
}
defineProperty(obj, key, desc, value, meta);
}
return obj;
}
示例6: injectedPropertyGet
function injectedPropertyGet(this: any, keyName: string): any {
let desc = descriptorFor(this, keyName);
let owner = getOwner(this) || this.container; // fallback to `container` for backwards compat
assert(
`InjectedProperties should be defined with the inject computed property macros.`,
desc && desc.type
);
assert(
`Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container.`,
!!owner
);
let specifier = `${desc.type}:${desc.name || keyName}`;
return owner.lookup(specifier, {
source: desc.source,
namespace: desc.namespace,
});
}
示例7: giveDescriptorSuper
function giveDescriptorSuper(
meta: Meta,
key: string,
property: ComputedProperty,
values: { [key: string]: any },
descs: { [key: string]: any },
base: object
): ComputedProperty {
let superProperty;
// Computed properties override methods, and do not call super to them
if (values[key] === undefined) {
// Find the original descriptor in a parent mixin
superProperty = descs[key];
}
// If we didn't find the original descriptor in a parent mixin, find
// it on the original object.
if (!superProperty) {
superProperty = descriptorFor(base, key, meta);
}
if (superProperty === undefined || !(superProperty instanceof ComputedProperty)) {
return property;
}
// Since multiple mixins may inherit from the same parent, we need
// to clone the computed property so that other mixins do not receive
// the wrapped version.
property = Object.create(property);
property._getter = wrap(property._getter, superProperty._getter) as ComputedPropertyGetter;
if (superProperty._setter) {
if (property._setter) {
property._setter = wrap(property._setter, superProperty._setter) as ComputedPropertySetter;
} else {
property._setter = superProperty._setter;
}
}
return property;
}
示例8: followMethodAlias
function followMethodAlias(
obj: object,
_desc: Alias,
descs: { [key: string]: any },
values: { [key: string]: any }
) {
let altKey = _desc.methodName;
let possibleDesc;
let desc = descs[altKey];
let value = values[altKey];
if (desc !== undefined || value !== undefined) {
} else if ((possibleDesc = descriptorFor(obj, altKey)) !== undefined) {
desc = possibleDesc;
value = undefined;
} else {
desc = undefined;
value = obj[altKey];
}
return { desc, value };
}
示例9: watchKey
export function watchKey(obj: object, keyName: string, _meta?: Meta): void {
let meta = _meta === undefined ? metaFor(obj) : _meta;
let count = meta.peekWatching(keyName);
meta.writeWatching(keyName, count + 1);
if (count === 0) {
// activate watching first time
let possibleDesc = descriptorFor(obj, keyName, meta);
if (possibleDesc !== undefined && possibleDesc.willWatch !== undefined) {
possibleDesc.willWatch(obj, keyName, meta);
}
if (typeof (obj as MaybeHasWillWatchProperty).willWatchProperty === 'function') {
(obj as MaybeHasWillWatchProperty).willWatchProperty!(keyName);
}
if (DEBUG) {
// NOTE: this is dropped for prod + minified builds
handleMandatorySetter(meta, obj, keyName);
}
}
}
示例10: get
export function get(obj: object, keyName: string): any {
assert(
`Get must be called with two arguments; an object and a property key`,
arguments.length === 2
);
assert(
`Cannot call get with '${keyName}' on an undefined object.`,
obj !== undefined && obj !== null
);
assert(
`The key provided to get must be a string or number, you passed ${keyName}`,
typeof keyName === 'string' || (typeof keyName === 'number' && !isNaN(keyName))
);
assert(
`'this' in paths is not supported`,
typeof keyName !== 'string' || keyName.lastIndexOf('this.', 0) !== 0
);
let type = typeof obj;
let isObject = type === 'object';
let isFunction = type === 'function';
let isObjectLike = isObject || isFunction;
let descriptor;
let value: any;
if (isObjectLike) {
if (EMBER_METAL_TRACKED_PROPERTIES) {
let tracker = getCurrentTracker();
if (tracker) tracker.add(tagForProperty(obj, keyName));
}
descriptor = descriptorFor(obj, keyName);
if (descriptor !== undefined) {
return descriptor.get(obj, keyName);
}
if (DEBUG && HAS_NATIVE_PROXY) {
value = getPossibleMandatoryProxyValue(obj, keyName);
} else {
value = obj[keyName];
}
if (PROPERTY_BASED_DESCRIPTORS && isDescriptor(value)) {
deprecate(
`[DEPRECATED] computed property '${keyName}' was not set on object '${toString(
obj
)}' via 'defineProperty'`,
false,
{
id: '@ember/-internals/meta.descriptor-on-object',
until: '3.5.0',
url:
'https://emberjs.com/deprecations/v3.x#toc_use-defineProperty-to-define-computed-properties',
}
);
Object.defineProperty(obj, keyName, {
configurable: true,
enumerable: value.enumerable === false,
get() {
return value.get(this, keyName);
},
});
meta(obj).writeDescriptors(keyName, value);
value.setup(obj, keyName);
return value.get(obj, keyName);
}
} else {
value = obj[keyName];
}
if (value === undefined) {
if (isPath(keyName)) {
return _getPath(obj, keyName);
}
if (
isObject &&
!(keyName in obj) &&
typeof (obj as MaybeHasUnknownProperty).unknownProperty === 'function'
) {
return (obj as MaybeHasUnknownProperty).unknownProperty!(keyName);
}
}
return value;
}