Skip to content

Commit

Permalink
[BUGFIX beta] [PERF] Cache FactoryManagers
Browse files Browse the repository at this point in the history
* 1 FactoryManager per container/normalizeFullName
* cache madeToString per factoryManager
(cherry picked from commit ac6a4a5)
  • Loading branch information
stefanpenner authored and chancancode committed Mar 14, 2017
1 parent 25cf68a commit daefda7
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/container/lib/container.js
Expand Up @@ -38,6 +38,7 @@ export default function Container(registry, options) {
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.factoryCache ? options.factoryCache : null);
this.validationCache = dictionary(options && options.validationCache ? options.validationCache : null);
this._fakeContainerToInject = buildFakeContainerWithDeprecations(this);
this[CONTAINER_OVERRIDE] = undefined;
Expand Down Expand Up @@ -281,6 +282,7 @@ if (isFeatureEnabled('ember-factory-for')) {
*/
Container.prototype.factoryFor = function _factoryFor(fullName, options = {}) {
let normalizedName = this.registry.normalize(fullName);

assert('fullName must be a proper full name', this.registry.validateFullName(normalizedName));

if (options.source) {
Expand All @@ -289,16 +291,23 @@ if (isFeatureEnabled('ember-factory-for')) {
if (!normalizedName) { return; }
}

let cached = this.factoryManagerCache[normalizedName];

if (cached) { return cached; }

let factory = this.registry.resolve(normalizedName);

if (factory === undefined) { return; }
if (factory === undefined) {
return;
}

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

runInDebug(() => {
manager = wrapManagerInDeprecationProxy(manager);
});

this.factoryManagerCache[normalizedName] = manager;
return manager;
};
}
Expand Down Expand Up @@ -667,13 +676,14 @@ class FactoryManager {
this.class = factory;
this.fullName = fullName;
this.normalizedName = normalizedName;
this.madeToString = undefined;
}

create(options = {}) {
let injections = injectionsFor(this.container, this.normalizedName);
let props = assign({}, injections, options);

props[NAME_KEY] = this.container.registry.makeToString(this.class, this.fullName);
props[NAME_KEY] = this.madeToString || (this.madeToString = this.container.registry.makeToString(this.class, this.fullName));

runInDebug(() => {
let lazyInjections;
Expand Down

0 comments on commit daefda7

Please sign in to comment.