New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

asyngleton

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

asyngleton - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

examples/structr-mixin.js

13

examples/test1.js

@@ -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());

83

lib/index.js

@@ -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": "*"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc