node-fibers-synchronize-helper
Advanced tools
Comparing version 1.0.2 to 1.1.0
81
index.js
@@ -113,3 +113,3 @@ var sync = require('synchronize'); | ||
console.log(params) | ||
//console.log(params) | ||
var res = sync.await(fn.apply(obj, params)); | ||
@@ -120,26 +120,51 @@ return res; | ||
exports.executeFiberFn = function (obj, fn) { | ||
//console.log(fn) | ||
//console.log(arguments) | ||
var params = []; | ||
var argl = arguments.length | ||
for (var i = 2; i < argl; i++) { | ||
params.push(arguments[i]); | ||
//console.log(fn) | ||
//console.log(arguments) | ||
var params = []; | ||
var argl = arguments.length | ||
for (var i = 2; i < argl; i++) { | ||
params.push(arguments[i]); | ||
} | ||
return executeFiberIntFn(obj, fn, params); | ||
} | ||
return executeFiberIntFn(obj, fn, params); | ||
} | ||
/* | ||
exports.executeFiberFnName = function (obj, fnName) { | ||
//console.log(fn) | ||
//console.log(arguments) | ||
var params = []; | ||
var argl = arguments.length | ||
for (var i = 2; i < argl; i++) { | ||
params.push(arguments[i]); | ||
} | ||
var fn = obj[fnName] | ||
return executeFiberIntFn(obj, fn, params); | ||
} | ||
*/ | ||
exports.executeFiberFnMultiParamCb = function (obj, fn, returnParamsNameArr) { | ||
//console.log(fn) | ||
//console.log(arguments) | ||
var params = []; | ||
var argl = arguments.length | ||
for (var i = 3; i < argl; i++) { | ||
params.push(arguments[i]); | ||
//console.log(fn) | ||
//console.log(arguments) | ||
var params = []; | ||
var argl = arguments.length | ||
for (var i = 3; i < argl; i++) { | ||
params.push(arguments[i]); | ||
} | ||
return executeFiberIntFn(obj, fn, params, returnParamsNameArr); | ||
} | ||
return executeFiberIntFn(obj, fn, params, returnParamsNameArr); | ||
} | ||
/* | ||
exports.executeFiberFnNameMultiParamCb = function (obj, fnName, returnParamsNameArr) { | ||
//console.log(fn) | ||
//console.log(arguments) | ||
var params = []; | ||
var argl = arguments.length | ||
for (var i = 3; i < argl; i++) { | ||
params.push(arguments[i]); | ||
} | ||
var fn = obj[fnName] | ||
return executeFiberIntFn(obj, fn, params, returnParamsNameArr); | ||
} | ||
*/ | ||
exports.getSynchPromiseObj = function (that) { | ||
@@ -156,1 +181,21 @@ return new synchPromObj(that) | ||
} | ||
exports.setThreadLocalValue = function (key, value) { | ||
if (sync.Fiber && sync.Fiber.current) | ||
sync.Fiber.current[key] = value; | ||
else { | ||
console.log("ERROR", "NO Fiber Thread") | ||
throw Error("NO Fiber Thread"); | ||
} | ||
} | ||
exports.getThreadLocalValue = function (key) { | ||
if (sync.Fiber && sync.Fiber.current) | ||
return sync.Fiber.current[key]; | ||
else { | ||
// throw Error("NO Fiber Thread"); | ||
console.log("ERROR", "NO Fiber Thread") | ||
return undefined; | ||
} | ||
} |
114
package.json
{ | ||
"_args": [ | ||
[ | ||
{ | ||
"raw": "https://github.com/zakdav/node-fibers-synchronize-helper", | ||
"scope": null, | ||
"escapedName": null, | ||
"name": null, | ||
"rawSpec": "https://github.com/zakdav/node-fibers-synchronize-helper", | ||
"spec": "git+https://github.com/zakdav/node-fibers-synchronize-helper.git", | ||
"type": "hosted", | ||
"hosted": { | ||
"type": "github", | ||
"ssh": "git@github.com:zakdav/node-fibers-synchronize-helper.git", | ||
"sshUrl": "git+ssh://git@github.com/zakdav/node-fibers-synchronize-helper.git", | ||
"httpsUrl": "git+https://github.com/zakdav/node-fibers-synchronize-helper.git", | ||
"gitUrl": "git://github.com/zakdav/node-fibers-synchronize-helper.git", | ||
"shortcut": "github:zakdav/node-fibers-synchronize-helper", | ||
"directUrl": "https://raw.githubusercontent.com/zakdav/node-fibers-synchronize-helper/master/package.json" | ||
} | ||
}, | ||
"/srv/GitHub" | ||
] | ||
], | ||
"_from": "git+https://github.com/zakdav/node-fibers-synchronize-helper.git", | ||
"_id": "node-fibers-synchronize-helper@1.0.2", | ||
"_inCache": true, | ||
"_location": "/node-fibers-synchronize-helper", | ||
"_phantomChildren": {}, | ||
"_requested": { | ||
"raw": "https://github.com/zakdav/node-fibers-synchronize-helper", | ||
"scope": null, | ||
"escapedName": null, | ||
"name": null, | ||
"rawSpec": "https://github.com/zakdav/node-fibers-synchronize-helper", | ||
"spec": "git+https://github.com/zakdav/node-fibers-synchronize-helper.git", | ||
"type": "hosted", | ||
"hosted": { | ||
"type": "github", | ||
"ssh": "git@github.com:zakdav/node-fibers-synchronize-helper.git", | ||
"sshUrl": "git+ssh://git@github.com/zakdav/node-fibers-synchronize-helper.git", | ||
"httpsUrl": "git+https://github.com/zakdav/node-fibers-synchronize-helper.git", | ||
"gitUrl": "git://github.com/zakdav/node-fibers-synchronize-helper.git", | ||
"shortcut": "github:zakdav/node-fibers-synchronize-helper", | ||
"directUrl": "https://raw.githubusercontent.com/zakdav/node-fibers-synchronize-helper/master/package.json" | ||
} | ||
}, | ||
"_requiredBy": [ | ||
"#USER" | ||
], | ||
"_resolved": "git+https://github.com/zakdav/node-fibers-synchronize-helper.git#36c80ec63584e819963d3695dc910c977ae7f707", | ||
"_shasum": "79e7eff828ff1b6dad8484cb37354c3f87058d44", | ||
"_shrinkwrap": null, | ||
"_spec": "https://github.com/zakdav/node-fibers-synchronize-helper", | ||
"_where": "/srv/GitHub", | ||
"author": { | ||
"name": "davide.zaccheo@gmail.com" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/zakdav/node-fibers-synchronize-helper/issues" | ||
}, | ||
"dependencies": { | ||
"synchronize": "2.0.0" | ||
}, | ||
"description": "helper for synchronize promise and callback", | ||
"devDependencies": {}, | ||
"gitHead": "36c80ec63584e819963d3695dc910c977ae7f707", | ||
"homepage": "https://github.com/zakdav/node-fibers-synchronize-helper#readme", | ||
"keywords": [ | ||
"name": "node-fibers-synchronize-helper", | ||
"version": "1.1.0", | ||
"description": "helper for synchronize promise and callback and manage variables on Fiber Thread as ThreadLocal", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"dependencies": { | ||
"synchronize": "2.0.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/zakdav/node-fibers-synchronize-helper.git" | ||
}, | ||
"keywords": [ | ||
"nodejs", | ||
"node", | ||
"fiber", | ||
"synchronize", | ||
"promise", | ||
"callback", | ||
"helper", | ||
"sync", | ||
"avoid callback", | ||
"callback hell" | ||
"callback hell", | ||
"fiber thread", | ||
"Java ThreadLocal", | ||
"Underlying Fiber Thread Objects" | ||
], | ||
"license": "ISC", | ||
"main": "index.js", | ||
"name": "node-fibers-synchronize-helper", | ||
"optionalDependencies": {}, | ||
"readme": "# node-fibers-synchronize-helper\n\nThis helper uses synchronize module (https://www.npmjs.com/package/synchronize) and helps to manage promises and callbacks, you will be able to synch code and catch exceptions \n\n\nPromise management:\n```\nvar synchProm = require('node-fibers-synchronize-helper')\n \nvar testSynch = function () {\n try{\n //promise function call\n var db = synchProm.executePromiseFiberFn(MongoClient, MongoClient.connect, url)\n //get synch object and call promise function\n var dbProm = synchProm.getSynchPromiseObj(db);\n var stats = dbProm.executePromiseFiber(db.stats)\n\n //cursor.toArray promise version\n var cursor = collection_test.find({});\n var resProm = synchProm.executePromiseFiberFn(cursor, cursor.toArray)\n\n\n\n }catch(e){\n console.log(e)\n }\n\n}\n\n\nsynchProm.executeSynch(testSynch, function (err, res) {\n if (err)\n console.log(\"err\", err)\n else\n console.log(\"OK:\", JSON.stringify(res, null, 4))\n})\n\n```\n\n\n\nCallback management:\n```\nvar synchProm = require('node-fibers-synchronize-helper')\n \nvar testSynch = function () {\n try{\n\n //cursor.toArray callback version\n cursor = collection_test.find({});\n var resCb = synchProm.executeFiberFn(cursor, cursor.toArray)\n assert.notEqual(null, resCb);\n\n //callback with param\n resCb = synchProm.executeFiberFn(cursor, cursor.count, true, {\n skip: 1\n })\n\n //multiparam result callback\n var resMultiCb = synchProm.executeFiberFnMultiParamCb(this, multiParamCB, ['res1', 'res2'], \"a\", \"b\", \"c\") \n\n\n }catch(e){\n console.log(e)\n }\n\n}\n\n\nsynchProm.executeSynch(testSynch, function (err, res) {\n if (err)\n console.log(\"err\", err)\n else\n console.log(\"OK:\", JSON.stringify(res, null, 4))\n})\n```\n\n\n\n\n\n\n\n\n", | ||
"readmeFilename": "README.md", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/zakdav/node-fibers-synchronize-helper.git" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"version": "1.0.2" | ||
} | ||
"author": "davide.zaccheo@gmail.com", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/zakdav/node-fibers-synchronize-helper/issues" | ||
}, | ||
"homepage": "https://github.com/zakdav/node-fibers-synchronize-helper#readme" | ||
} |
114
README.md
# node-fibers-synchronize-helper | ||
This helper uses synchronize module (https://www.npmjs.com/package/synchronize) and helps to manage promises and callbacks, you will be able to synch code and catch exceptions | ||
This helper uses synchronize module (https://www.npmjs.com/package/synchronize) and helps to manage promises and callbacks, and manage variables on underlying Fiber Thread like Java ThreadLocal, **you will be able to synchronize callback and promises** and **develop syncronously as your did in Java** returning Objects and catch exceptions, and set-get Objects on your Fiber Thread as you did with Java **ThreadLocal**. | ||
Promise management: | ||
***Create/Start Fiber Thread:*** | ||
var synchProm = require('node-fibers-synchronize-helper') | ||
synchProm.executeSynch(function, callback) | ||
``` | ||
synchProm.executeSynch(function, function (err, res) { | ||
if (err) | ||
console.log("err", err) | ||
else | ||
console.log("OK:", res) | ||
}) | ||
``` | ||
***Promise:*** | ||
Object result: | ||
**var result = executePromiseFiberFn(Object/this, method/function, params ...)** | ||
**var result = executePromiseFiberFnName(Object/this, method/function name, params ...)** | ||
example: | ||
``` | ||
var db = synchProm.executePromiseFiberFn(MongoClient, MongoClient.connect, url) | ||
synchProm.executePromiseFiberFn(collection, collection.insertMany, [{name:"Jim"} | ||
, {name:"Sarah", title:"Princess"}], {w:1}) | ||
var db = synchProm.executePromiseFiberFnName(MongoClient, 'connect', url) | ||
``` | ||
***Callback:*** | ||
Object result: | ||
**var result = executeFiberFn(Object/this, method/function, params ...)** | ||
example: | ||
``` | ||
var db = synchProm.executeFiberFn(MongoClient, MongoClient.connect, url) | ||
synchProm.executeFiberFn(collection, collection.insertMany, [{name:"Jim"} | ||
, {name:"Sarah", title:"Princess"}], {w:1}) | ||
``` | ||
Array results: | ||
**var results = executeFiberFnMultiParamCb(Object/this, method/function, result name array , params ...)** | ||
example: | ||
``` | ||
var resMultiCbs = synchProm.executeFiberFnMultiParamCb(this, multiParamCB, ['res1', 'res2'], "a", "b", "c") | ||
``` | ||
Fiber ThreadLocal: | ||
**synchProm.setThreadLocalValue("key", value)** | ||
**synchProm.getThreadLocalValue("key")** | ||
Promise synchronization: | ||
``` | ||
var synchProm = require('node-fibers-synchronize-helper') | ||
@@ -42,3 +107,3 @@ | ||
Callback management: | ||
Callback synchronization: | ||
``` | ||
@@ -81,6 +146,49 @@ var synchProm = require('node-fibers-synchronize-helper') | ||
Fiber ThreadLocal: | ||
``` | ||
var synchProm = require('node-fibers-synchronize-helper') | ||
synchProm.setThreadLocalValue("key", value) | ||
synchProm.getThreadLocalValue("key") | ||
``` | ||
example ThreadLocal: | ||
``` | ||
var synchProm = require('node-fibers-synchronize-helper') | ||
var timeoutCB = function (mills, cb) { | ||
setTimeout(function () { | ||
cb(null); | ||
}, mills); | ||
} | ||
var testThreadLocal = function () { | ||
for (i = 0; i < 10; i++) { | ||
( | ||
function (count) { | ||
var fiberThread = function () { | ||
synchProm.setThreadLocalValue("count", count) | ||
synchProm.executeFiberFn(this, timeoutCB, 1000) | ||
console.log(count, synchProm.getThreadLocalValue("count")) | ||
return count | ||
} | ||
synchProm.executeSynch(fiberThread, function (err, res) { | ||
if (err) | ||
console.log("err", err) | ||
else | ||
console.log("OK:", res) | ||
}) | ||
})(i) | ||
} | ||
} | ||
testThreadLocal() | ||
``` | ||
@@ -6,6 +6,6 @@ //TEST | ||
var synchProm = require('./index.js') | ||
//var synchProm = require('node-fibers-synchronize-helper') | ||
var synchProm = require('node-fibers-synchronize-helper') | ||
var url = 'mongodb://localhost:27017/local'; | ||
@@ -12,0 +12,0 @@ console.log("CALLBACK") |
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
16258
5
292
193