electrolyte
Advanced tools
Comparing version 0.6.1 to 0.7.0
@@ -79,3 +79,5 @@ // Load modules. | ||
for (i = 0, len = deps.length; i < len; ++i) { | ||
if (deps[i][0] == ':') { | ||
if (options[deps[i]]) { | ||
promises.push(Promise.resolve(options[deps[i]])); | ||
} else if (deps[i][0] == ':') { | ||
// TODO: test case for this. | ||
@@ -96,4 +98,19 @@ // inject named parameters | ||
var i = this.instantiate.apply(this, args); | ||
container.emit('create', i, this); | ||
// TODO: Make sure option is respected on cached singletons, etc | ||
// TODO: Probably remove this entirely | ||
return options.meta ? [ i, { implements: this.implements, a: this.a } ] : i; | ||
}.bind(this)) | ||
.then(function(i) { | ||
//console.log('GOT INSTANCE!'); | ||
// WIP: wrapping the object. TODO: Handle implements array. | ||
var impl = this.implements[0]; | ||
var wrap = container._wrappers[impl]; | ||
if (wrap) { | ||
i = wrap.call(container, i); | ||
} | ||
// Once the promise has been resolved, cache the object instance if the | ||
@@ -103,4 +120,5 @@ // spec was annotated as being a singleton. | ||
// TODO: Make sure option is respected on cached singletons, etc | ||
return options.meta ? [ i, { implements: this.implements, a: this.a } ] : i; | ||
container.emit('create', i, this); | ||
return i; | ||
}.bind(this)); | ||
@@ -107,0 +125,0 @@ |
@@ -14,2 +14,3 @@ // Load modules. | ||
, ComponentCreateError = require('./errors/componentcreate') | ||
, pMultiFilter = require('./p-multifilter') | ||
, debug = require('debug')('electrolyte'); | ||
@@ -50,2 +51,6 @@ | ||
this._resolvers = []; | ||
this._filters = []; | ||
this._sorters = []; | ||
this._variables = {}; | ||
this._wrappers = {}; | ||
@@ -81,2 +86,11 @@ this.resolver(require('./resolvers/id')()); | ||
//console.log('USE'); | ||
//console.log(asm); | ||
// TODO: Clean this up | ||
if (asm.use) { | ||
//console.log(''); | ||
asm.use(this); | ||
} | ||
// accept either object or loader function | ||
@@ -96,2 +110,3 @@ if (typeof asm == 'function') { | ||
var ids = asm.components | ||
@@ -136,2 +151,34 @@ , comp, i, len; | ||
Container.prototype.create = function(id, parent, ecomp, options) { | ||
//if (id == 'http://i.bixbyjs.org/http/middleware/session') { | ||
if (id == 'http://i.bixbyjs.org/http/middleware/authenticate') { | ||
console.log('!!! ATTEMPTED TO CREATE AUTH MIDDLEWARE: ' + parent.id); | ||
} | ||
// TODO: Implement feature to create from function??? | ||
/* | ||
if (typeof id == 'function') { | ||
var fc = new FactoryComponent('__anonymous__', id); | ||
var c = fc.create(this); | ||
c.then(function(i) { | ||
console.log('*******________ RESOLVED') | ||
console.log(i); | ||
}) | ||
return c; | ||
} | ||
*/ | ||
if (id[id.length - 1] == '?') { | ||
console.log('**** IT IS OPTIONAL ***'); | ||
console.log(id); | ||
} | ||
var optional = false; | ||
if (id[id.length - 1] == '?') { | ||
optional = true; | ||
id = id.slice(0, id.length - 1); | ||
} | ||
// built-ins | ||
@@ -141,4 +188,17 @@ switch (id) { | ||
return Promise.resolve(new InjectedContainer(this, parent)); | ||
//case '$location': | ||
//console.log('*** CREATE LOCATION ****'); | ||
// TODO: Factor this better | ||
//return require('./resolvers/location')(this, parent); | ||
} | ||
if (this._variables[id]) { | ||
console.log('IT IS A VARIABLE ****'); | ||
return this._variables[id](this, parent); | ||
} | ||
// FIXME: self should be defined here, not in the promise | ||
return new Promise(function(resolve, reject) { | ||
@@ -182,4 +242,33 @@ var self = this; | ||
} | ||
id = this.resolve(id, parent); | ||
id = this.resolve(id, parent, optional); | ||
if (!id) { | ||
// optional | ||
return resolve(undefined); | ||
} | ||
if (Array.isArray(id)) { | ||
pMultiFilter(id, self._filters) | ||
.then(function(candidates) { | ||
// TODO: handle 0-length array | ||
if (candidates.length > 1) { | ||
// TODO: Make this error string to candidate ids | ||
// FIXME: this is giving [object Object],[object Object] in string where the map is | ||
reject(new Error("Multiple components provide interface '" + id + "' required by '" + (parent || 'unknown') + "'. Configure one of: " + candidates.map(function(c) { return c.id }).join(', '))); | ||
} | ||
var c = candidates[0]; | ||
return create(c); | ||
}); | ||
return; | ||
} | ||
if (typeof id == 'object') { | ||
// TODO: In what cases do we fall below and need to regiser components? | ||
//console.log('**** RETURNED A COMPONENT, OPTIMIZE FAST PATH'); | ||
return create(id); | ||
} | ||
// TODO: Eliminate this below here by always returning a component from a resolver. | ||
var comp = this._components[id]; | ||
@@ -215,3 +304,3 @@ if (comp) { | ||
Container.prototype.resolve = function(id, parent) { | ||
Container.prototype.resolve = function(id, parent, optional) { | ||
var resolvers = this._resolvers | ||
@@ -222,4 +311,15 @@ , fn, rid, i, len; | ||
rid = fn(id, parent && parent.id); | ||
/* | ||
if (rid) { | ||
if (Array.isArray(rid)) { | ||
console.log('NEED TO FILTER THIS...'); | ||
return; | ||
} | ||
} | ||
*/ | ||
if (rid) { return rid; } | ||
} | ||
if (optional) { return undefined; } | ||
throw new ImplNotFoundError("Cannot find implementation of '" + id + "' required by '" + (parent && parent.id || 'unknown') + "'", id); | ||
@@ -233,2 +333,18 @@ } | ||
Container.prototype.filter = function(fn) { | ||
this._filters.push(fn); | ||
} | ||
Container.prototype.sorter = function(fn) { | ||
this._sorters.push(fn); | ||
} | ||
Container.prototype.variable = function(name, fn) { | ||
this._variables['$' + name] = fn; | ||
} | ||
Container.prototype.wrap = function(name, fn) { | ||
this._wrappers[name] = fn; | ||
} | ||
/** | ||
@@ -235,0 +351,0 @@ * Load component specification. |
@@ -97,2 +97,10 @@ // Load modules. | ||
} | ||
var sorters = this._c._sorters | ||
, sorter | ||
for (i = 0, len = sorters.length; i < len; ++i) { | ||
sorter = sorters[i]; | ||
exposed = sorter(exposed); | ||
} | ||
return exposed; | ||
@@ -99,0 +107,0 @@ } |
{ | ||
"name": "electrolyte", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"description": "Elegant dependency injection for Node.js.", | ||
@@ -44,2 +44,3 @@ "keywords": [ | ||
"depd": "^1.0.1", | ||
"p-filter": "2.1", | ||
"promise": "^7.1.1", | ||
@@ -46,0 +47,0 @@ "scripts": "0.1.x" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55360
22
956
6
+ Addedp-filter@2.1
+ Addedp-filter@2.1.0(transitive)
+ Addedp-map@2.1.0(transitive)