Giter VIP home page Giter VIP logo

node-serialize's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

foo123 mandrake73

node-serialize's Issues

Ability to rewire prev results to current task args

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.