From 56957eb9a1812197f1114b3df411a82037154796 Mon Sep 17 00:00:00 2001 From: Artem Gurzhii Date: Wed, 8 Mar 2017 23:01:15 +0200 Subject: [PATCH 1/2] code refactoring for container package --- packages/container/lib/container.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/container/lib/container.js b/packages/container/lib/container.js index 2794401f4aa..760e2145e9e 100644 --- a/packages/container/lib/container.js +++ b/packages/container/lib/container.js @@ -209,7 +209,7 @@ Container.prototype = { @param {String} fullName optional key to reset; if missing, resets everything */ reset(fullName) { - if (arguments.length > 0) { + if (fullName) { resetMember(this, this.registry.normalize(fullName)); } else { resetCache(this); @@ -341,7 +341,7 @@ function lookup(container, fullName, options = {}) { function isSingletonClass(container, fullName, { instantiate, singleton }) { return (singleton !== false && isSingleton(container, fullName)) && - (!instantiate && !shouldInstantiate(container, fullName)); + (instantiate === false && !shouldInstantiate(container, fullName)); } function isSingletonInstance(container, fullName, { instantiate, singleton }) { @@ -487,11 +487,9 @@ function injectionsFor(container, fullName) { return injections; } -function instantiate(factory, props, container, fullName) { +function instantiate(factory, props = {}, container, fullName) { let lazyInjections, validationCache; - props = props || {}; - if (container.registry.getOption(fullName, 'instantiate') === false) { return factory; } From 3e9a2c3cf9333785425ffda7d1678fe39977047e Mon Sep 17 00:00:00 2001 From: Artem Gurzhii Date: Thu, 16 Mar 2017 10:20:33 +0200 Subject: [PATCH 2/2] container cleanup --- packages/container/lib/container.js | 53 +++++++++++------------------ packages/container/lib/registry.js | 12 +++---- 2 files changed, 26 insertions(+), 39 deletions(-) diff --git a/packages/container/lib/container.js b/packages/container/lib/container.js index 252b17e9cdb..16ea9730567 100644 --- a/packages/container/lib/container.js +++ b/packages/container/lib/container.js @@ -171,12 +171,7 @@ Container.prototype = { @method destroy */ destroy() { - eachDestroyable(this, item => { - if (item.destroy) { - item.destroy(); - } - }); - + destroyDestroyables(this); this.isDestroyed = true; }, @@ -255,14 +250,14 @@ if (isFeatureEnabled('ember-factory-for')) { @param {String} [options.source] The fullname of the request source (used for local lookup) @return {any} */ - Container.prototype.factoryFor = function _factoryFor(fullName, options = {}) { + Container.prototype.factoryFor = function (fullName, options = {}) { let normalizedName = this.registry.normalize(fullName); assert('fullName must be a proper full name', this.registry.validateFullName(normalizedName)); if (options.source) { normalizedName = this.registry.expandLocalLookup(fullName, options); - // if expandLocalLookup returns falsey, we do not support local lookup + // if expandLocalLookup returns falsy, we do not support local lookup if (!normalizedName) { return; } @@ -293,7 +288,7 @@ function isSingleton(container, fullName) { return container.registry.getOption(fullName, 'singleton') !== false; } -function shouldInstantiate(container, fullName) { +function isInstantiatable(container, fullName) { return container.registry.getOption(fullName, 'instantiate') !== false; } @@ -330,19 +325,19 @@ function lookup(container, fullName, options = {}) { } function isSingletonClass(container, fullName, { instantiate, singleton }) { - return singleton !== false && isSingleton(container, fullName) && !instantiate && !shouldInstantiate(container, fullName); + return singleton !== false && isSingleton(container, fullName) && !instantiate && !isInstantiatable(container, fullName); } function isSingletonInstance(container, fullName, { instantiate, singleton }) { - return singleton !== false && isSingleton(container, fullName) && instantiate !== false && shouldInstantiate(container, fullName); + return singleton !== false && isSingleton(container, fullName) && instantiate !== false && isInstantiatable(container, fullName); } function isFactoryClass(container, fullname, { instantiate, singleton }) { - return (singleton === false || !isSingleton(container, fullname)) && instantiate === false && !shouldInstantiate(container, fullname); + return (singleton === false || !isSingleton(container, fullname)) && instantiate === false && !isInstantiatable(container, fullname); } function isFactoryInstance(container, fullName, { instantiate, singleton }) { - return (singleton !== false || isSingleton(container, fullName)) && instantiate !== false && shouldInstantiate(container, fullName); + return (singleton !== false || isSingleton(container, fullName)) && instantiate !== false && isInstantiatable(container, fullName); } function instantiateFactory(container, fullName, options) { @@ -381,16 +376,15 @@ function areInjectionsDynamic(injections) { function buildInjections() /* container, ...injections */{ let hash = {}; + let len = arguments.length; - if (arguments.length > 1) { + if (len > 1) { let container = arguments[0]; let injections = []; let injection; - for (let i = 1; i < arguments.length; i++) { - if (arguments[i]) { - injections = injections.concat(arguments[i]); - } + for (let i = 1; i < len; i++) { + injections.push(...arguments[i]); } runInDebug(() => { @@ -414,7 +408,7 @@ function deprecatedFactoryFor(container, fullName, options = {}) { if (options.source) { fullName = registry.expandLocalLookup(fullName, options); - // if expandLocalLookup returns falsey, we do not support local lookup + // if expandLocalLookup returns falsy, we do not support local lookup if (!fullName) { return; } @@ -466,8 +460,7 @@ function deprecatedFactoryFor(container, fullName, options = {}) { function injectionsFor(container, fullName) { let registry = container.registry; - let splitName = fullName.split(':'); - let type = splitName[0]; + let type = fullName.split(':')[0]; let injections = buildInjections(container, registry.getTypeInjections(type), registry.getInjections(fullName)); injections._debugContainerKey = fullName; @@ -533,8 +526,7 @@ function instantiate(factory, props = {}, container, fullName) { function factoryInjectionsFor(container, fullName) { let registry = container.registry; - let splitName = fullName.split(':'); - let type = splitName[0]; + let type = fullName.split(':')[0]; let factoryInjections = buildInjections(container, registry.getFactoryTypeInjections(type), registry.getFactoryInjections(fullName)); factoryInjections._debugContainerKey = fullName; @@ -563,7 +555,7 @@ function injectDeprecatedContainer(object, container) { }); } -function eachDestroyable(container, callback) { +function destroyDestroyables(container) { let cache = container.cache; let keys = Object.keys(cache); @@ -571,19 +563,14 @@ function eachDestroyable(container, callback) { let key = keys[i]; let value = cache[key]; - if (container.registry.getOption(key, 'instantiate') !== false) { - callback(value); + if (isInstantiatable(container, key) && value.destroy) { + value.destroy(); } } } function resetCache(container) { - eachDestroyable(container, value => { - if (value.destroy) { - value.destroy(); - } - }); - + destroyDestroyables(container); container.cache.dict = dictionary(null); } @@ -678,4 +665,4 @@ class FactoryManager { return this.class.create(props); } -} \ No newline at end of file +} diff --git a/packages/container/lib/registry.js b/packages/container/lib/registry.js index 3d9ba5016bd..544d759fa6a 100644 --- a/packages/container/lib/registry.js +++ b/packages/container/lib/registry.js @@ -668,8 +668,8 @@ Registry.prototype = { let localKnown = dictionary(null); let registeredNames = Object.keys(this.registrations); - for (let index = 0; index < registeredNames.length; index++) { - let fullName = registeredNames[index]; + for (let i = 0; i < registeredNames.length; i++) { + let fullName = registeredNames[i]; let itemType = fullName.split(':')[0]; if (itemType === type) { @@ -732,7 +732,7 @@ Registry.prototype = { getInjections(fullName) { let injections = this._injections[fullName] || []; if (this.fallback) { - injections = injections.concat(this.fallback.getInjections(fullName)); + injections.push(...this.fallback.getInjections(fullName)); } return injections; }, @@ -740,7 +740,7 @@ Registry.prototype = { getTypeInjections(type) { let injections = this._typeInjections[type] || []; if (this.fallback) { - injections = injections.concat(this.fallback.getTypeInjections(type)); + injections.push(...this.fallback.getTypeInjections(type)); } return injections; }, @@ -748,7 +748,7 @@ Registry.prototype = { getFactoryInjections(fullName) { let injections = this._factoryInjections[fullName] || []; if (this.fallback) { - injections = injections.concat(this.fallback.getFactoryInjections(fullName)); + injections.push(...this.fallback.getFactoryInjections(fullName)); } return injections; }, @@ -756,7 +756,7 @@ Registry.prototype = { getFactoryTypeInjections(type) { let injections = this._factoryTypeInjections[type] || []; if (this.fallback) { - injections = injections.concat(this.fallback.getFactoryTypeInjections(type)); + injections.push(...this.fallback.getFactoryTypeInjections(type)); } return injections; }