Comparing version 0.0.1 to 0.0.2
@@ -24,2 +24,13 @@ var asyngleton = require('../'), | ||
}) | ||
}, 500); | ||
}, 500); | ||
var readDirSync = asyngleton(function(callback) { | ||
console.log("READ DIR SYNC") | ||
return fs.readdirSync(__dirname); | ||
}); | ||
console.log(readDirSync()); | ||
console.log(readDirSync()); |
@@ -6,13 +6,20 @@ var EventEmitter = require("events").EventEmitter; | ||
function createAsyngleton(fn) { | ||
function singleton(fn) { | ||
var em = new EventEmitter(), singletonArgs, called; | ||
var em = new EventEmitter(), singletonArgs, loading, ret; | ||
var ret = function() { | ||
var asyngleton = function() { | ||
var callback = arguments[arguments.length - 1]; | ||
var args, cb, callback = arguments[arguments.length - 1]; | ||
if(!(typeof callback == "function")) { | ||
callback = function(){}; | ||
} | ||
//result already set? return the value | ||
if(singletonArgs) { | ||
return callback(null, singletonArgs); | ||
callback.apply(null, singletonArgs); | ||
//return the value if there is one | ||
return ret; | ||
} | ||
@@ -24,11 +31,10 @@ | ||
//still loading? add listener to event emitter | ||
if(called) return; | ||
called = true; | ||
if(loading) { | ||
return; | ||
} | ||
var args = Array.prototype.splice.call([], arguments); | ||
loading = true; | ||
//remove the callback | ||
args.pop(); | ||
args.push(function() { | ||
args = Array.prototype.slice.call(arguments, 0); | ||
cb = function() { | ||
singletonArgs = Array.prototype.slice.call(arguments, 0); | ||
@@ -39,15 +45,31 @@ em.emit.apply(em, ["singleton"].concat(singletonArgs)); | ||
em.removeAllListeners("singleton"); | ||
}); | ||
}; | ||
fn.apply(this, args); | ||
//remove the callback | ||
args.pop(); | ||
//and replace it. | ||
args.push(cb); | ||
//returned a value? Then it's not async... | ||
if (ret = fn.apply(this, args)) { | ||
cb(ret); | ||
return ret; | ||
} | ||
}; | ||
ret.reset = function() { | ||
asyngleton.reset = function() { | ||
loading = false; | ||
ret = undefined; | ||
singletonArgs = undefined; | ||
called = false; | ||
return ret; | ||
} | ||
return ret; | ||
return asyngleton; | ||
}; | ||
return asyngleton; | ||
} | ||
@@ -66,3 +88,3 @@ | ||
var asyngleton = _dict[key] = createAsyngleton(fn); | ||
var asyngleton = _dict[key] = singleton(fn); | ||
@@ -78,3 +100,20 @@ asyngleton.dispose = function() { | ||
module.exports = createAsyngleton; | ||
module.exports.dictionary = createDictionary; | ||
/** | ||
*/ | ||
function structrFactory(that, property, value) { | ||
return singleton(value); | ||
} | ||
/** | ||
*/ | ||
module.exports = singleton; | ||
module.exports.dictionary = createDictionary; | ||
//for structr | ||
module.exports.type = "operator"; | ||
module.exports.factory = structrFactory; |
{ | ||
"author": "Craig Condon", | ||
"name": "asyngleton", | ||
"description": "asynchronously generate singletons", | ||
"version": "0.0.1", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/crcn/asyngleton.js.git" | ||
}, | ||
"main": "./lib/index.js", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
"author": "Craig Condon", | ||
"name": "asyngleton", | ||
"description": "asynchronously generate singletons", | ||
"version": "0.0.2", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/crcn/asyngleton.js.git" | ||
}, | ||
"main": "./lib/index.js", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"structr": "*" | ||
} | ||
} |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
4734
5
122
1
2