Skip to content

Commit

Permalink
Merge pull request #15193 from rwjblue/remove-lookup-factory
Browse files Browse the repository at this point in the history
Remove `_lookupFactory` support.
  • Loading branch information
rwjblue authored May 1, 2017
2 parents a66b67c + 579f857 commit 85cd220
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 493 deletions.
165 changes: 5 additions & 160 deletions packages/container/lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export default function Container(registry, options) {
this.registry = registry;
this.owner = options && options.owner ? options.owner : null;
this.cache = dictionary(options && options.cache ? options.cache : null);
this.factoryCache = dictionary(options && options.factoryCache ? options.factoryCache : null);
this.factoryManagerCache = dictionary(options && options.factoryManagerCache ? options.factoryManagerCache : null);
this.validationCache = dictionary(options && options.validationCache ? options.validationCache : null);
this[CONTAINER_OVERRIDE] = undefined;
Expand All @@ -53,12 +52,6 @@ Container.prototype = {
@type InheritingDict
*/

/**
@private
@property factoryCache
@type InheritingDict
*/

/**
@private
@property validationCache
Expand Down Expand Up @@ -102,23 +95,6 @@ Container.prototype = {
return lookup(this, this.registry.normalize(fullName), options);
},

/**
Given a fullName, return the corresponding factory.
@private
@method lookupFactory
@param {String} fullName
@param {Object} [options]
@param {String} [options.source] The fullname of the request source (used for local lookup)
@return {any}
*/
lookupFactory(fullName, options) {
assert('fullName must be a proper full name', this.registry.validateFullName(fullName));

deprecate('Using "_lookupFactory" is deprecated. Please use container.factoryFor instead.', false, { id: 'container-lookupFactory', until: '2.13.0', url: 'http://emberjs.com/deprecations/v2.x/#toc_migrating-from-_lookupfactory-to-factoryfor' });

return deprecatedFactoryFor(this, this.registry.normalize(fullName), options);
},

/**
A depth first traversal, destroying the container, its descendant containers and all
their managed objects.
Expand Down Expand Up @@ -190,6 +166,10 @@ Container.prototype = {
return;
}

if (DEBUG && factory && typeof factory._onLookup === 'function') {
factory._onLookup(fullName);
}

let manager = new FactoryManager(this, factory, fullName, normalizedName);

if (DEBUG) {
Expand Down Expand Up @@ -348,61 +328,6 @@ function buildInjections() /* container, ...injections */{
return hash;
}

function deprecatedFactoryFor(container, fullName, options = {}) {
let registry = container.registry;

if (options.source) {
fullName = registry.expandLocalLookup(fullName, options);
// if expandLocalLookup returns falsey, we do not support local lookup
if (!fullName) {
return;
}
}

let cache = container.factoryCache;
if (cache[fullName]) {
return cache[fullName];
}
let factory = registry.resolve(fullName);
if (factory === undefined) {
return;
}

let type = fullName.split(':')[0];
if (!factory || typeof factory.extend !== 'function' || !ENV.MODEL_FACTORY_INJECTIONS && type === 'model') {
if (factory && typeof factory._onLookup === 'function') {
factory._onLookup(fullName);
}

// TODO: think about a 'safe' merge style extension
// for now just fallback to create time injection
cache[fullName] = factory;
return factory;
} else {
let injections = injectionsFor(container, fullName);
let factoryInjections = factoryInjectionsFor(container, fullName);
let cacheable = !areInjectionsDynamic(injections) && !areInjectionsDynamic(factoryInjections);

factoryInjections[NAME_KEY] = registry.makeToString(factory, fullName);
injections._debugContainerKey = fullName;
setOwner(injections, container.owner);

let injectedFactory = factory.extend(injections);

injectedFactory.reopenClass(factoryInjections);

if (factory && typeof factory._onLookup === 'function') {
factory._onLookup(fullName);
}

if (cacheable) {
cache[fullName] = injectedFactory;
}

return injectedFactory;
}
}

function injectionsFor(container, fullName) {
let registry = container.registry;
let splitName = fullName.split(':');
Expand All @@ -413,74 +338,6 @@ function injectionsFor(container, fullName) {
return injections;
}

function instantiate(factory, props, container, fullName) {
let lazyInjections, validationCache;

props = props || {};

if (container.registry.getOption(fullName, 'instantiate') === false) {
return factory;
}

if (factory) {
if (typeof factory.create !== 'function') {
throw new Error(`Failed to create an instance of '${fullName}'. Most likely an improperly defined class or` + ` an invalid module export.`);
}

validationCache = container.validationCache;

if (DEBUG) {
// Ensure that all lazy injections are valid at instantiation time
if (!validationCache[fullName] && typeof factory._lazyInjections === 'function') {
lazyInjections = factory._lazyInjections();
lazyInjections = container.registry.normalizeInjectionsHash(lazyInjections);

container.registry.validateInjections(lazyInjections);
}
}

validationCache[fullName] = true;

let obj;

if (typeof factory.extend === 'function') {
// assume the factory was extendable and is already injected
obj = factory.create(props);
} else {
// assume the factory was extendable
// to create time injections
// TODO: support new'ing for instantiation and merge injections for pure JS Functions
let injections = injectionsFor(container, fullName);
injections._debugContainerKey = fullName;

// Ensure that a container is available to an object during instantiation.
// TODO - remove when Ember reaches v3.0.0
// This "fake" container will be replaced after instantiation with a
// property that raises deprecations every time it is accessed.
injections.container = container._fakeContainerToInject;
obj = factory.create(assign({}, injections, props));

// TODO - remove when Ember reaches v3.0.0
if (!Object.isFrozen(obj)) {
injectDeprecatedContainer(obj, container);
}
}

return obj;
}
}

function factoryInjectionsFor(container, fullName) {
let registry = container.registry;
let splitName = fullName.split(':');
let type = splitName[0];

let factoryInjections = buildInjections(container, registry.getFactoryTypeInjections(type), registry.getFactoryInjections(fullName));
factoryInjections._debugContainerKey = fullName;

return factoryInjections;
}

function destroyDestroyables(container) {
let cache = container.cache;
let keys = Object.keys(cache);
Expand All @@ -503,7 +360,7 @@ function resetCache(container) {
function resetMember(container, fullName) {
let member = container.cache[fullName];

delete container.factoryCache[fullName];
delete container.factoryManagerCache[fullName];

if (member) {
delete container.cache[fullName];
Expand All @@ -514,18 +371,6 @@ function resetMember(container, fullName) {
}
}

class DeprecatedFactoryManager {
constructor(container, factory, fullName) {
this.container = container;
this.class = factory;
this.fullName = fullName;
}

create(props = {}) {
return instantiate(this.class, props, this.container, this.fullName);
}
}

class FactoryManager {
constructor(container, factory, fullName, normalizedName) {
this.container = container;
Expand Down
143 changes: 0 additions & 143 deletions packages/container/lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ export default function Registry(options) {

this._typeInjections = dictionary(null);
this._injections = dictionary(null);
this._factoryTypeInjections = dictionary(null);
this._factoryInjections = dictionary(null);

this._localLookupCache = Object.create(null);
this._normalizeCache = dictionary(null);
Expand Down Expand Up @@ -86,22 +84,6 @@ Registry.prototype = {
*/
_injections: null,

/**
@private
@property _factoryTypeInjections
@type InheritingDict
*/
_factoryTypeInjections: null,

/**
@private
@property _factoryInjections
@type InheritingDict
*/
_factoryInjections: null,

/**
@private
Expand Down Expand Up @@ -549,115 +531,6 @@ Registry.prototype = {
});
},


/**
Used only via `factoryInjection`.
Provides a specialized form of injection, specifically enabling
all factory of one type to be injected with a reference to another
object.
For example, provided each factory of type `model` needed a `store`.
one would do the following:
```javascript
let registry = new Registry();
registry.register('store:main', SomeStore);
registry.factoryTypeInjection('model', 'store', 'store:main');
let store = registry.lookup('store:main');
let UserFactory = registry.lookupFactory('model:user');
UserFactory.store instanceof SomeStore; //=> true
```
@private
@method factoryTypeInjection
@param {String} type
@param {String} property
@param {String} fullName
*/
factoryTypeInjection(type, property, fullName) {
let injections = this._factoryTypeInjections[type] ||
(this._factoryTypeInjections[type] = []);

injections.push({
property: property,
fullName: this.normalize(fullName)
});
},

/**
Defines factory injection rules.
Similar to regular injection rules, but are run against factories, via
`Registry#lookupFactory`.
These rules are used to inject objects onto factories when they
are looked up.
Two forms of injections are possible:
* Injecting one fullName on another fullName
* Injecting one fullName on a type
Example:
```javascript
let registry = new Registry();
let container = registry.container();
registry.register('store:main', Store);
registry.register('store:secondary', OtherStore);
registry.register('model:user', User);
registry.register('model:post', Post);
// injecting one fullName on another type
registry.factoryInjection('model', 'store', 'store:main');
// injecting one fullName on another fullName
registry.factoryInjection('model:post', 'secondaryStore', 'store:secondary');
let UserFactory = container.lookupFactory('model:user');
let PostFactory = container.lookupFactory('model:post');
let store = container.lookup('store:main');
UserFactory.store instanceof Store; //=> true
UserFactory.secondaryStore instanceof OtherStore; //=> false
PostFactory.store instanceof Store; //=> true
PostFactory.secondaryStore instanceof OtherStore; //=> true
// and both models share the same source instance
UserFactory.store === PostFactory.store; //=> true
```
@private
@method factoryInjection
@param {String} factoryName
@param {String} property
@param {String} injectionName
*/
factoryInjection(fullName, property, injectionName) {
let normalizedName = this.normalize(fullName);
let normalizedInjectionName = this.normalize(injectionName);

this.validateFullName(injectionName);

if (fullName.indexOf(':') === -1) {
return this.factoryTypeInjection(normalizedName, property, normalizedInjectionName);
}

let injections = this._factoryInjections[normalizedName] || (this._factoryInjections[normalizedName] = []);

injections.push({
property: property,
fullName: normalizedInjectionName
});
},

/**
@private
@method knownForType
Expand Down Expand Up @@ -743,22 +616,6 @@ Registry.prototype = {
injections = injections.concat(this.fallback.getTypeInjections(type));
}
return injections;
},

getFactoryInjections(fullName) {
let injections = this._factoryInjections[fullName] || [];
if (this.fallback) {
injections = injections.concat(this.fallback.getFactoryInjections(fullName));
}
return injections;
},

getFactoryTypeInjections(type) {
let injections = this._factoryTypeInjections[type] || [];
if (this.fallback) {
injections = injections.concat(this.fallback.getFactoryTypeInjections(type));
}
return injections;
}
};

Expand Down
Loading

0 comments on commit 85cd220

Please sign in to comment.