From 7e0c66a1bb4abd8cec671a175eb2c09e626ce5c3 Mon Sep 17 00:00:00 2001 From: uetkaje Date: Fri, 13 Nov 2015 23:09:22 +0200 Subject: [PATCH 1/3] Add ability to create component manually --- lib/container.js | 4 ++-- lib/spec.js | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/container.js b/lib/container.js index 7288d46..4c76a50 100644 --- a/lib/container.js +++ b/lib/container.js @@ -65,7 +65,7 @@ util.inherits(Container, EventEmitter); * @returns {object} * @public */ -Container.prototype.create = function(id, parent) { +Container.prototype.create = function(id, args, parent) { if (parent && id[0] == '.') { // resolve relative component ID id = path.join(path.dirname(parent.id), id); @@ -96,7 +96,7 @@ Container.prototype.create = function(id, parent) { throw new Error("Unable to create object '" + id + "'"); } - var obj = spec.create(this); + var obj = spec.create(this, args); this.emit('create', obj, spec); return obj; } diff --git a/lib/spec.js b/lib/spec.js index 8a72fcc..63c984d 100644 --- a/lib/spec.js +++ b/lib/spec.js @@ -42,27 +42,27 @@ function Spec(id, dependencies, mod) { * * @protected */ -Spec.prototype.create = function(container) { +Spec.prototype.create = function(container, args) { debug('create %s', this.id); // Immediately return cached instance. Optimization for singleton and literal // components. if (this.instance) { return this.instance; } - + args = args || []; var source = container._sources[this._hsource] , deps = this.dependencies - , args = [] , inst, sfn; - for (var i = 0, len = deps.length; i < len; ++i) { - sfn = container._special[deps[i]]; - if (sfn) { - inst = sfn.call(container, source); - } else { - inst = container.create(deps[i], this); - } - args.push(inst); + if (!args.length) { + for (var i = 0, len = deps.length; i < len; ++i) { + sfn = container._special[deps[i]]; + if (sfn) { + inst = sfn.call(container, source); + } else { + inst = container.create(deps[i], this); + } + args.push(inst); + } } - var i = this.instantiate.apply(this, args); // Cache the instance if the component was annotated as being a singleton. if (this.singleton) { this.instance = i; } From 427b0396a5783fdbaa3c87b98bf8b935ba6d0c02 Mon Sep 17 00:00:00 2001 From: uetkaje Date: Fri, 13 Nov 2015 23:19:44 +0200 Subject: [PATCH 2/3] Register components manually --- README.md | 22 ++++++++++++++++++++++ lib/container.js | 14 ++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/README.md b/README.md index 557e71c..26c7dd8 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,20 @@ Electrolyte uses to automatically wire together an application. - `@singleton` Indicates that the component returns a singleton object, which should be shared by all components in the application. +#### Register Components Manually + +```javascript +var IoC = require('electrolyte'); + +var fn = function () { + return 'db.instance'; +}; +fn['@singleton'] = true; +IoC.register('database', fn); + +var db = IoC.create('database'); +``` + #### Creating Components Components are created by asking the IoC container to create them: @@ -110,6 +124,14 @@ var IoC = require('electrolyte'); var db = IoC.create('database'); ``` +Or you can pass an array to provide dependencies like below. +```javascript +var IoC = require('electrolyte'); +var db = IoC.create('database', [ + IoC.create('settings') +]); +``` + Electrolyte is smart enough to automatically traverse a component's dependencies (and dependencies of dependencies, and so on), correctly wiring together the complete object structure. diff --git a/lib/container.js b/lib/container.js index 4c76a50..9b2eb74 100644 --- a/lib/container.js +++ b/lib/container.js @@ -180,6 +180,20 @@ Container.prototype._loadSpec = function(id) { } } +Container.prototype.register = function(id, mod) { + var order = this._order + , source, prefix; + for (var i = 0, len = order.length; i < len; ++i) { + source = this._sources[order[i]]; + prefix = source.prefix; + if (id.indexOf(prefix) !== 0) { continue; } + if (mod) { + this._registerSpec(id, mod, order[i]); + break; + } + } +} + Container.prototype._registerSpec = function(id, mod, hsource) { var dependencies = mod['@require'] || [] , pattern = 'literal' From 1079ccf9433eb9011bf81195ea8ee94fe1829631 Mon Sep 17 00:00:00 2001 From: uetkaje Date: Sat, 14 Nov 2015 01:47:33 +0200 Subject: [PATCH 3/3] Bug fixed --- lib/spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/spec.js b/lib/spec.js index 63c984d..34a8888 100644 --- a/lib/spec.js +++ b/lib/spec.js @@ -58,7 +58,7 @@ Spec.prototype.create = function(container, args) { if (sfn) { inst = sfn.call(container, source); } else { - inst = container.create(deps[i], this); + inst = container.create(deps[i], undefined, this); } args.push(inst); }