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 all 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
25 changes: 10 additions & 15 deletions packages/container/lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,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 @@ -218,7 +218,7 @@ Container.prototype = {

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 @@ -359,16 +359,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]);
}

if (DEBUG) {
Expand Down Expand Up @@ -397,7 +396,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 @@ -449,8 +448,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 @@ -460,11 +458,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 @@ -518,8 +514,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
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