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

Manually register and create #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
18 changes: 16 additions & 2 deletions lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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'
Expand Down
24 changes: 12 additions & 12 deletions lib/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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], undefined, 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; }
Expand Down