chaoran / node-serialize Goto Github PK
View Code? Open in Web Editor NEWA simple node library to serialize execution of asynchronous functions
A simple node library to serialize execution of asynchronous functions
Hi,
your lib is just what i was looking for for some tools i have.
It is so simple, it is brilliant.
However a small addition is needed:
Scenario:
var fs = require('fs');
var serialize = require('./utils/node-serialize');
fs.writeFile = serialize(fs.writeFile);
fs.readFile = serialize(fs.readFile);
fs.writeFile('./foo1.txt', 'test1', { encoding: 'utf8'});
fs.readFile('./foo1.txt', { encoding: 'utf8'});
// i would like to replace the 1st arg with the data read in previous step
fs.writeFile('./foo2.txt', 'this arg should be replaced with test1', { encoding: 'utf8'});
// foo2.txt content is 'this arg should be replaced with test1' instead of 'test1'
i made a small extension in the lib to be able to rewire previous results to current task args:
var fs = require('fs');
var serialize = require('./utils/node-serialize');
fs.writeFile = serialize(fs.writeFile);
fs.readFile = serialize(fs.readFile);
fs.writeFile('./foo1.txt', 'test1', { encoding: 'utf8'});
// rewire the results[1] to the next task's args[1], multiple rewires can be added in same call
fs.readFile('./foo1.txt', { encoding: 'utf8'}).rewire([1, 1]);
// i would like to replace the 1st arg with the data read in previous step
fs.writeFile('./foo2.txt', 'this arg should be replaced with test1', { encoding: 'utf8'});
// foo2.txt content is now correctly 'test1'
extended code:
var slice = Array.prototype.slice;
var queues = [];
var _prevResult = null;
var _currentRewire = null;
var WorkQueue = function() {
this.running = null;
this.pending = [];
};
WorkQueue.get = function(name) {
var queue = queues[name];
if (!queue) queue = queues[name] = new WorkQueue();
return queue;
};
WorkQueue.prototype.add = function(_this, func, args, noCallback, rewireArgs) {
var that = this;
if (noCallback)
{
args.push(function(err) {
_prevResult = slice.call(arguments);
_currentRewire = rewireArgs.rewire || null;
that.next(err)
});
}
else
{
callback = args.pop();
args.push(function() {
callback.apply(this, arguments);
_prevResult = slice.call(arguments);
_currentRewire = rewireArgs.rewire || null;
that.next(arguments[0]);
});
}
var task = function(err) {
if (err instanceof Error)
{
args[args.length - 1].call(global, err);
}
else
{
console.log(["args before", args]);
console.log(["rewire", _currentRewire]);
if (_currentRewire && _prevResult)
{
// rewire prev results to current task args
for (var i=0; i<_currentRewire.length; i++)
args[_currentRewire[i][0]] = _prevResult[_currentRewire[i][1]];
}
_prevResult = null;
console.log(["args after", args]);
func.apply(_this, args);
}
};
if (!this.running) (this.running = task)();
else this.pending.push(task);
};
WorkQueue.prototype.next = function(err) {
var run = this.running = this.pending.shift();
if (run) run(err);
};
var serialize = function(func, name) {
if (!name) name = 'default';
var queue = WorkQueue.get(name)
, length = func.length;
var rewireArgs = { rewire: null };
var serialized = function() {
queue.add(this, func, slice.call(arguments), arguments.length < length, rewireArgs);
return serialized;
};
serialized.rewire = function() {
var args = slice.call(arguments);
if (args.length) rewireArgs.rewire = args;
return serialized;
};
serialized.free = function() { return func; };
return serialized;
}
module.exports = serialize;
i can make a pull request if everything is ok.
Nikos
Serializes and Unserializes to empty objects for both.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.