UNPKG

ember-source

Version:

A JavaScript framework for creating ambitious web applications

71 lines (61 loc) 2.1 kB
import { getOwner } from '../@ember/-internals/owner/index.js'; import '../@ember/debug/index.js'; import { c as computed, d as defineProperty } from './computed-CJnghmXC.js'; import { i as isElementDescriptor } from './decorator-B5Uh5NFI.js'; import { assert } from '../@ember/debug/lib/assert.js'; let DEBUG_INJECTION_FUNCTIONS; { DEBUG_INJECTION_FUNCTIONS = new WeakMap(); } /** @module ember @private */ /** Read-only property that returns the result of a container lookup. @class InjectedProperty @namespace Ember @constructor @param {String} type The container type the property will lookup @param {String} nameOrDesc (optional) The name the property will lookup, defaults to the property's name @private */ // Decorator factory (with args) // (Also matches non-decorator form, types may be incorrect for this.) // Non-decorator // Decorator (without args) // Catch-all for service and controller injections function inject(type, ...args) { (!(typeof type === 'string') && assert('a string type must be provided to inject', typeof type === 'string')); let elementDescriptor; let name; if (isElementDescriptor(args)) { elementDescriptor = args; } else if (typeof args[0] === 'string') { name = args[0]; } let getInjection = function (propertyName) { let owner = getOwner(this) || this.container; // fallback to `container` for backwards compat (!(Boolean(owner)) && assert(`Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container.`, Boolean(owner))); return owner.lookup(`${type}:${name || propertyName}`); }; { DEBUG_INJECTION_FUNCTIONS.set(getInjection, { type, name }); } let decorator = computed({ get: getInjection, set(keyName, value) { defineProperty(this, keyName, null, value); } }); if (elementDescriptor) { return decorator(elementDescriptor[0], elementDescriptor[1], elementDescriptor[2]); } else { return decorator; } } export { DEBUG_INJECTION_FUNCTIONS as D, inject as i };