async-executor
Advanced tools
+16
-16
| /** | ||
| * NodeJS Executor | ||
| * @version 0.1 | ||
| * @version 0.0.2 | ||
| * @author Maksym Pomazan | ||
@@ -11,4 +11,3 @@ */ | ||
| function Executor() { | ||
| this.success_data = null; | ||
| this.error_data = null; | ||
| this.data = null; | ||
| this.success_callbacks = []; | ||
@@ -21,3 +20,3 @@ this.error_callbacks = []; | ||
| * Main result method. | ||
| * | ||
| * | ||
| * @param {Object} err error data or null | ||
@@ -31,17 +30,18 @@ * @param {Object} data result data or null | ||
| this.ready = "error"; | ||
| this.error_data = err; | ||
| for (var i = 0; i < this.error_callbacks.length; i++) { | ||
| this.error_callbacks[i](this.error_data); | ||
| this.data = err; | ||
| while (callback = this.error_callbacks.pop()) { | ||
| callback(this.data); | ||
| } | ||
| if(this.defaultError){ | ||
| this.defaultError(this.error_data); | ||
| if (this.defaultError) { | ||
| this.defaultError(this.data); | ||
| } | ||
| } else { | ||
| this.ready = "success"; | ||
| this.success_data = data; | ||
| for (var j = 0; j < this.success_callbacks.length; j++) { | ||
| this.success_callbacks[j](this.success_data); | ||
| this.data = data; | ||
| while (callback = this.success_callbacks.pop()) { | ||
| callback(this.data); | ||
| } | ||
| if(this.defaultSuccess){ | ||
| this.defaultSuccess(this.success_data); | ||
| if (this.defaultSuccess) { | ||
| this.defaultSuccess(this.data); | ||
| } | ||
@@ -60,3 +60,3 @@ } | ||
| if (this.ready == "success") { | ||
| callback(this.success_data); | ||
| callback(this.data); | ||
| } | ||
@@ -77,3 +77,3 @@ } else { | ||
| if (this.ready == "error") { | ||
| callback(this.error_data); | ||
| callback(this.data); | ||
| } | ||
@@ -80,0 +80,0 @@ } else { |
+6
-3
| { | ||
| "name": "async-executor", | ||
| "version": "0.0.1", | ||
| "version": "0.0.2", | ||
| "description": "Async method executor", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "mocha" | ||
| "test": "mocha --expose-gc" | ||
| }, | ||
@@ -13,3 +13,6 @@ "keywords": [ | ||
| ], | ||
| "git": "https://github.com/maxazan/async-executor", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/maxazan/async-executor.git" | ||
| }, | ||
| "author": "Pomazan Maksym", | ||
@@ -16,0 +19,0 @@ "license": "MIT", |
+11
-9
@@ -1,9 +0,10 @@ | ||
| #Async Executor | ||
| How to install | ||
| #How to install | ||
| ``` | ||
| node install executor | ||
| node install async-executor | ||
| ``` | ||
| How to initialize | ||
| #How to initialize | ||
| ```javascript | ||
| var Executor = require('async-executor'); | ||
| function MyClass(){ | ||
@@ -14,12 +15,13 @@ } | ||
| var executor=new Executor(); | ||
| dosomethingasync(executor.result); | ||
| executor.result(null, {success: true}); // call if success | ||
| //executor.result({error: true}, null); // call if error | ||
| return executor; | ||
| } | ||
| }; | ||
| ``` | ||
| How to use | ||
| #How to use | ||
| ```javascript | ||
| var obj=MyClass(); | ||
| obj.doSomething().success(function(data){}).error(function(){}); | ||
| var obj=new MyClass(); | ||
| obj.doSomething().success(function(data){}).error(function(error){}); | ||
| ``` | ||
+43
-4
@@ -5,2 +5,3 @@ var assert = require("assert"); | ||
| describe('Executor', function() { | ||
| this.timeout(5000); | ||
@@ -89,7 +90,12 @@ it('before success test', function(done) { | ||
| assert.equal(data.foo, "bar"); | ||
| if (counter == 2) { | ||
| done(); | ||
| } | ||
| }).success(function(data) { | ||
| counter++; | ||
| assert.equal(data.foo, "bar"); | ||
| assert.equal(counter, 2); | ||
| done(); | ||
| if (counter == 2) { | ||
| done(); | ||
| } | ||
| }).result(null, { | ||
@@ -107,7 +113,12 @@ foo: "bar" | ||
| assert.equal(data.foo, "bar"); | ||
| if (counter == 2) { | ||
| done(); | ||
| } | ||
| }).error(function(data) { | ||
| counter++; | ||
| assert.equal(data.foo, "bar"); | ||
| assert.equal(counter, 2); | ||
| done(); | ||
| if (counter == 2) { | ||
| done(); | ||
| } | ||
| }).result({ | ||
@@ -155,2 +166,30 @@ foo: "bar" | ||
| }); | ||
| it('memory usage test', function(done) { | ||
| Executor.prototype.defaultSuccess = null; | ||
| var s = []; | ||
| var func = function() {}; | ||
| global.gc(); | ||
| var before = process.memoryUsage().heapUsed; | ||
| for (var i = 0; i < 1000000; i++) { | ||
| var executor = new Executor(); | ||
| executor.success(function(data) {}); | ||
| executor.success(function(data) {}); | ||
| executor.result(null, { | ||
| success: true | ||
| }); | ||
| s.push(executor); | ||
| } | ||
| global.gc(); | ||
| var after = process.memoryUsage().heapUsed; | ||
| var diff_mb = (after - before) / 1024 / 1024; | ||
| assert.ok(diff_mb < 300, "A lot memmory usage >100Mb for 1M instances"); | ||
| done(); | ||
| }); | ||
| }); |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
9243
12.93%229
14.5%27
8%0
-100%