Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code refactoring for container package #14989

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 22 additions & 37 deletions packages/container/lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,7 @@ Container.prototype = {
@method destroy
*/
destroy() {
eachDestroyable(this, item => {
if (item.destroy) {
item.destroy();
}
});

destroyDestroyables(this);
this.isDestroyed = true;
},

Expand All @@ -187,7 +182,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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 }) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We typically want to avoid lines that are too long. I also feel the former is easier to read

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking at this pull request. Should I replace it?

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) {
Expand Down Expand Up @@ -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(() => {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -477,11 +470,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;
}
Expand Down Expand Up @@ -535,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;
Expand Down Expand Up @@ -565,27 +555,22 @@ function injectDeprecatedContainer(object, container) {
});
}

function eachDestroyable(container, callback) {
function destroyDestroyables(container) {
let cache = container.cache;
let keys = Object.keys(cache);

for (let i = 0; i < keys.length; i++) {
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);
}

Expand Down Expand Up @@ -680,4 +665,4 @@ class FactoryManager {

return this.class.create(props);
}
}
}
12 changes: 6 additions & 6 deletions packages/container/lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -732,31 +732,31 @@ 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));
Copy link
Contributor

@bekzod bekzod Mar 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so you are modifying this._injections[fullName] ? , because former did not do that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope you are not polluting this._injections[fullName] here :P

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before/after is not same here, so each time you call getInjections it it modifies this._injections[fullName] if there is fallback. am I wrong ? @stefanpenner @artemgurzhii

}
return injections;
},

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;
},

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;
},

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;
}
Expand Down