Skip to content

Commit

Permalink
jshint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nstraub committed Mar 11, 2016
1 parent 054298c commit 987527f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 94 deletions.
1 change: 1 addition & 0 deletions src/inject.constructor.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* exported Injector */
function Injector() {
this.types = {};
this.providers = {};
Expand Down
103 changes: 51 additions & 52 deletions src/inject.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,60 @@
var lifetimes = ['singleton', 'transient', 'root', 'parent', 'state'],
injector;

var old_injector = window.injector;

/*----------------------
-- Injection Methods --
----------------------*/
function _inject (name, parent) {
var descriptor, type, dependency_providers;
if (typeof name === 'string') {
descriptor = this.fakes[name] || this.types[name] || this.providers[name];
if (this.cache[name] && this.cache[name].hashCode === descriptor.hashCode) {
return this.cache[name];
}

} else {
descriptor = this.build_anonymous_descriptor(name);
/* globals Injector: false */
/* globals window: false */
/* exported lifetimes */
/* exported old_injector */
var lifetimes = ['singleton', 'transient', 'root', 'parent', 'state'];

var old_injector = window.injector;

/*----------------------
-- Injection Methods --
----------------------*/
function _inject (name, parent) {
var descriptor, dependency_providers;
if (typeof name === 'string') {
descriptor = this.fakes[name] || this.types[name] || this.providers[name];
if (this.cache[name] && this.cache[name].hashCode === descriptor.hashCode) {
return this.cache[name];
}

if (!descriptor) {
if (parent) {
return null;
} else {
throw 'There is no dependency named "' + name + '" registered.';
}
}
if (descriptor.provider && descriptor.provider !== parent) {
return _inject.call(this, descriptor.provider, name);
}

type = descriptor.type;

dependency_providers = {};
_.each(descriptor.dependencies, function (dependency_name) {
dependency_providers[dependency_name] = _inject.call(this, dependency_name, name);
}, this);
} else {
descriptor = this.build_anonymous_descriptor(name);
}

return this.build_provider(name, descriptor, dependency_providers);
if (!descriptor) {
if (parent) {
return null;
} else {
throw 'There is no dependency named "' + name + '" registered.';
}
}
if (descriptor.provider && descriptor.provider !== parent) {
return _inject.call(this, descriptor.provider, name);
}

Injector.prototype.inject = function (name) {
return _inject.call(this, name);
};
dependency_providers = {};
_.each(descriptor.dependencies, function (dependency_name) {
dependency_providers[dependency_name] = _inject.call(this, dependency_name, name);
}, this);

Injector.prototype.get = function (name, context, adhoc_dependencies) {
var provider = this.inject(name);
if (context) {
return provider.call(context, adhoc_dependencies);
}
return provider(adhoc_dependencies);
};
return this.build_provider(name, descriptor, dependency_providers);
}

Injector.prototype.run = function (context, adhoc_dependencies) {
if (!this.providers.main) {
throw 'No main method registered. Please register one by running injector.registerMain() before running the app';
}
return this.get('main', context, adhoc_dependencies);
};
Injector.prototype.inject = function (name) {
return _inject.call(this, name);
};

Injector.prototype.get = function (name, context, adhoc_dependencies) {
var provider = this.inject(name);
if (context) {
return provider.call(context, adhoc_dependencies);
}
return provider(adhoc_dependencies);
};

Injector.prototype.run = function (context, adhoc_dependencies) {
if (!this.providers.main) {
throw 'No main method registered. Please register one by running injector.registerMain() before running the app';
}
return this.get('main', context, adhoc_dependencies);
};
58 changes: 27 additions & 31 deletions src/inject.providers.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
/**
* Created by nico on 07/04/2015.
*/
function map_dependencies(dependency_providers, adhoc_dependencies) {
var _this = this,
adhoc_dependency_providers = {};
/* globals Injector: false */

_.each(adhoc_dependencies, function (dependency, key) {
adhoc_dependency_providers[key] = function () {
return dependency;
};
});
_.assign(dependency_providers, adhoc_dependency_providers);
function map_dependencies(dependency_providers, adhoc_dependencies) {
var _this = this,
adhoc_dependency_providers = {};

return _.map(dependency_providers, function (provider, key) {
if (!provider) {
throw 'There is no dependency named "' + key + '" registered.';
}
return provider.call(_this);
});
}
_.each(adhoc_dependencies, function (dependency, key) {
adhoc_dependency_providers[key] = function () {
return dependency;
};
});
_.assign(dependency_providers, adhoc_dependency_providers);

return _.map(dependency_providers, function (provider, key) {
if (!provider) {
throw 'There is no dependency named "' + key + '" registered.';
}
return provider.call(_this);
});
}

Injector.prototype.cache = {};
var createObject = Object.create;
Injector.prototype.provide_transient = function (type, dependency_providers) {
function Aux(args) {
return type.apply(this, args);
}

Aux.prototype = type.prototype;

return function (adhoc_dependencies) {
var dependencies = map_dependencies(dependency_providers, adhoc_dependencies);
return new Aux(dependencies);
}
var instance = createObject(type.prototype);

type.apply(instance, map_dependencies(dependency_providers, adhoc_dependencies));
return instance;
};
};
Injector.prototype.provide_singleton = function (name, type, dependency_providers, singleton_cache) {
var _this = this;
Expand All @@ -45,15 +41,15 @@ Injector.prototype.provide_singleton = function (name, type, dependency_provider
};
}
return dependency_to_cache;
}
};
}
return singleton_cache[name];
};
Injector.prototype.provide_provider = function(dependency_providers, type) {
return function (adhoc_dependencies) {
var dependencies = map_dependencies.call(this, dependency_providers, adhoc_dependencies);
return type.apply(this, dependencies);
}
};
};

(function () {
Expand All @@ -71,4 +67,4 @@ Injector.prototype.provide_provider = function(dependency_providers, type) {
return this.cache[name] = this.provide_transient(descriptor.type, dependency_providers);
}
};
}());
}());
18 changes: 11 additions & 7 deletions src/inject.registration.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
/**
* Created by nico on 07/04/2015.
*/
/* globals Injector: false */
/* globals lifetimes: false */

var get_dependency_names = (function () {
var dependency_pattern = /^function ?\w* ?\(((?:\w+|(?:, ?))+)\)/;
var separatorPattern = /, ?/;
return function get_dependency_names(type) {
var serialized_type = type.toString();
var serialized_dependencies;

if (serialized_dependencies = dependency_pattern.exec(serialized_type)) {
return serialized_dependencies[1].split(/, ?/);
return serialized_dependencies[1].split(separatorPattern);
} else {
return null;
}
}
};
}());


Injector.prototype.registerType = function (name, type, lifetime, provider) {
lifetime = lifetime || 'transient';

if (!~lifetimes.indexOf(lifetime)) {
throw 'invalid lifetime "' + lifetime + '" provided. Valid lifetimes are singleton, transient, instance and parent'
throw 'invalid lifetime "' + lifetime + '" provided. Valid lifetimes are singleton, transient, instance and parent';
}

this._register('types', name, type, lifetime);
Expand All @@ -42,7 +46,7 @@ Injector.prototype.registerFake = function (name, type, lifetime) {
lifetime = lifetime || 'transient';

if (!~lifetimes.indexOf(lifetime)) {
throw 'invalid lifetime "' + lifetime + '" provided. Valid lifetimes are singleton, transient, instance and parent'
throw 'invalid lifetime "' + lifetime + '" provided. Valid lifetimes are singleton, transient, instance and parent';
}

this._register('fakes', name, type, lifetime);
Expand All @@ -53,7 +57,7 @@ Injector.prototype._register = function (where, name, type, lifetime) {
var realType, dependencies;
var destination = this[where];
if (typeof destination === 'undefined') {
throw 'invalid destination "' + where + '" provided. Valid destinations are types, providers, fakes and main'
throw 'invalid destination "' + where + '" provided. Valid destinations are types, providers, fakes and main';
}

if (typeof name !== 'string' || name === '') {
Expand Down Expand Up @@ -90,11 +94,11 @@ Injector.prototype.build_anonymous_descriptor = function (name) { // for when in
return {
type: name,
dependencies: get_dependency_names(name)
}
};
} else {
return {
type: name.pop(),
dependencies: name
}
};
}
};
12 changes: 8 additions & 4 deletions src/inject.util.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
/* globals Injector: false */
/* globals injector: false */
/* globals old_injector: false */
/* globals window: false */
Injector.prototype.getType = function (name) {
var type = this.fakes[name] || this.types[name];

if (type) {
return type.type;
}
return null
return null;
};

Injector.prototype.extend = function (parent, child) {
var parent_type = this.types[parent];
if (parent_type) {
child.prototype = this.get(parent);
} else {
throw 'No type "' + parent + '" found.'
throw 'No type "' + parent + '" found.';
}
};

function listener() {
injector.clearState();
};
}

Injector.prototype.noConflict = function () {
window.injector = old_injector;
};

Injector.prototype.removeDefaultListener = function () {
window.removeEventListener('hashchange', listener)
window.removeEventListener('hashchange', listener);
};

/*-------------------
Expand Down
1 change: 1 addition & 0 deletions src/require.shim.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Created by nico on 17/03/2015.
*/
/* globals requirejs: false */
requirejs.config({
shim: {
'lodash': {
Expand Down

0 comments on commit 987527f

Please sign in to comment.