Comparing version 0.1.0 to 0.1.1
var EventEmitter = require("events").EventEmitter; | ||
var singletonIndex = 0; | ||
/** | ||
@@ -8,6 +10,9 @@ */ | ||
var em = new EventEmitter(), singletonArgs, loading, ret; | ||
var _id = singletonIndex++; | ||
var asyngleton = function() { | ||
var asyng = asyngleton.info.call(this); | ||
var args, cb, callback = arguments[arguments.length - 1]; | ||
@@ -20,23 +25,21 @@ | ||
//result already set? return the value | ||
if(singletonArgs) { | ||
callback.apply(this, singletonArgs); | ||
//return the value if there is one | ||
return ret; | ||
if(asyng.result) { | ||
callback.apply(this, asyng.result); | ||
return this; | ||
} | ||
em.once("singleton", callback); | ||
asyng.em.once("singleton", callback); | ||
//still loading? add listener to event emitter | ||
if(loading) { | ||
return; | ||
if(asyng.loading) { | ||
return this; | ||
} | ||
loading = true; | ||
asyng.loading = true; | ||
args = Array.prototype.slice.call(arguments, 0); | ||
cb = function() { | ||
singletonArgs = Array.prototype.slice.call(arguments, 0); | ||
em.emit.apply(em, ["singleton"].concat(singletonArgs)); | ||
asyng.result = Array.prototype.slice.call(arguments, 0); | ||
asyng.em.emit.apply(asyng.em, ["singleton"].concat(asyng.result)); | ||
}; | ||
@@ -60,12 +63,33 @@ | ||
loading = false; | ||
ret = undefined; | ||
singletonArgs = undefined; | ||
var asyng = asyngleton.info.call(this); | ||
asyng.loading = false; | ||
asyng.result = undefined; | ||
return asyngleton; | ||
}; | ||
asyngleton.info = function() { | ||
if(!this._asyngleton) { | ||
this._asyngleton = { | ||
}; | ||
} | ||
var asyng; | ||
if(!(asyng = this._asyngleton[_id])) { | ||
asyng = this._asyngleton[_id] = { | ||
result: null, | ||
loading: false, | ||
em: new EventEmitter() | ||
} | ||
} | ||
return asyng; | ||
} | ||
return asyngleton; | ||
} | ||
/** | ||
@@ -72,0 +96,0 @@ */ |
@@ -5,3 +5,3 @@ { | ||
"description": "asynchronously generate singletons", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
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
5585
134