From e0d01c9b5ed9d200fd173dba7305dbaebda4d8a9 Mon Sep 17 00:00:00 2001 From: Vladimir Alaev Date: Wed, 6 Mar 2013 23:15:49 +0200 Subject: [PATCH 1/2] replace Q with Vow --- lib/arch.js | 6 +++--- lib/coa.js | 2 +- lib/util.js | 16 ++++++++++++++++ lib/workers.js | 20 +++++++++++--------- package.json | 2 +- test/workers-test.js | 26 +++++++++++++------------- 6 files changed, 45 insertions(+), 27 deletions(-) diff --git a/lib/arch.js b/lib/arch.js index ffe17f3..96a4e44 100644 --- a/lib/arch.js +++ b/lib/arch.js @@ -1,5 +1,5 @@ var UTIL = require('util'), - Q = require('q'), + Q = require('vow'), INHERIT = require('inherit'), ASSERTS = require('./asserts'), Plan = require('./plan'), @@ -74,8 +74,8 @@ module.exports = INHERIT(/** @lends Arch.prototype */ { withLock: function(cb, context) { var _this = this; this.lock(); - return Q.fcall(cb.bind(context)) - .fin(function() { + return U.qcall(cb.bind(context)) + .always(function() { _this.unlock(); }); }, diff --git a/lib/coa.js b/lib/coa.js index 775fb04..43f7940 100644 --- a/lib/coa.js +++ b/lib/coa.js @@ -1,4 +1,4 @@ -var Q = require('q'), +var Q = require('vow'), PATH = require('path'), APW = require('./apw'), diff --git a/lib/util.js b/lib/util.js index 93867a5..0c9af1b 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,3 +1,4 @@ +var Q = require('vow'); /** * Wrap object in array if it is not array already. * @@ -8,6 +9,21 @@ var toArray = exports.toArray = function(o) { return Array.isArray(o) ? o : [o]; }; +exports.qcall = function(func, thisp) { + var args = Array.prototype.slice.call(arguments, 2); + + var deferred = Q.promise(); + process.nextTick(function () { + try { + deferred.fulfill(func.call(thisp, args)); + } catch(err) { + deferred.reject(err); + } + }); + + return deferred; +}; + /** * Convert array to object. * diff --git a/lib/workers.js b/lib/workers.js index d2a6240..b0d89c0 100644 --- a/lib/workers.js +++ b/lib/workers.js @@ -1,6 +1,7 @@ -var Q = require('q'), +var Q = require('vow'), INHERIT = require('inherit'), ASSERTS = require('./asserts'), + U = require('./util'), extend = require('./util').extend; module.exports = INHERIT(/** @lends Workers.prototype */ { @@ -65,7 +66,7 @@ module.exports = INHERIT(/** @lends Workers.prototype */ { this.addPlan(plan); this.next(); - return defer.promise; + return defer; }, /** @@ -76,7 +77,7 @@ module.exports = INHERIT(/** @lends Workers.prototype */ { * @returns {Q.Defer} Defer of this plan ID. */ getDefer: function(id, remove) { - var defer = this.defers[id] || (this.defers[id] = Q.defer()); + var defer = this.defers[id] || (this.defers[id] = Q.promise()); remove && delete this.defers[id]; return defer; }, @@ -212,7 +213,8 @@ module.exports = INHERIT(/** @lends Workers.prototype */ { this.activeWorkers++; this.work(job, plan) - .done(onDone, onError); + .then(onDone, onError) + .done(); return this; }, @@ -242,7 +244,7 @@ module.exports = INHERIT(/** @lends Workers.prototype */ { this.removePlanFinishListener(planId); defer = this.getDefer(plan.getId(), true); - err? defer.reject(err) : defer.resolve(); + err? defer.reject(err) : defer.fulfill(); } } @@ -257,7 +259,7 @@ module.exports = INHERIT(/** @lends Workers.prototype */ { * @returns {Promise} The result of Q.fcall. */ work: function(job, plan) { - return Q.fcall(function() { + return Q.when(function() { // Skip job if its node has no run() method if (!job.node.run) return; @@ -267,12 +269,12 @@ module.exports = INHERIT(/** @lends Workers.prototype */ { plan: plan }); job.node.ctx = ctx; - return Q.invoke(job.node, 'run', ctx) - .fin(function() { + return U.qcall(job.node.run, job.node, ctx) + .always(function() { delete job.node.ctx; }); - }.bind(this)); + }.apply(this)); }, /** diff --git a/package.json b/package.json index 5e818d2..5c89dee 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "node": ">= 0.6.0" }, "dependencies": { - "q": "~0.8.10", + "vow": "~0.1.12", "coa": "~0.3.8", "inherit": "1", "node.extend": "1" diff --git a/test/workers-test.js b/test/workers-test.js index 0ba8dc4..702b14d 100644 --- a/test/workers-test.js +++ b/test/workers-test.js @@ -1,4 +1,4 @@ -var Q = require('q'), +var Q = require('vow'), APW = require('..'), ASSERT = require('assert'); @@ -83,34 +83,34 @@ function getAPW(arch) { describe('Run plan: A', function() { it('correct run', function(done) { var state = []; - Q.done(getAPW(getArch(state)).process('0A'), + Q.when(getAPW(getArch(state)).process('0A'), function() { ASSERT.equal(state.length, 1); ASSERT.equal(state[0], '0A'); done(); }, done - ); + ).done(); }); }); describe('Run plan: A -> B', function() { it('correct run order', function(done) { var state = []; - Q.done(getAPW(getArch(state)).process('1A'), + Q.when(getAPW(getArch(state)).process('1A'), function() { ASSERT.equal(state.length, 2); ASSERT.equal(state[0], '1B'); ASSERT.equal(state[1], '1A'); done(); }, done - ); + ).done(); }); }); describe('Run plan without lock (TODO: should we throw error?): A -> B* -> (A -> C, A -> D)', function() { it('correct run order', function(done) { var state = []; - Q.done(getAPW(getArch(state)).process('2A'), + Q.when(getAPW(getArch(state)).process('2A'), function() { ASSERT.equal(state.length, 4); ASSERT.equal(state[0], '2B'); @@ -119,14 +119,14 @@ describe('Run plan without lock (TODO: should we throw error?): A -> B* -> (A -> ASSERT.equal(state[3], '2A'); done(); }, done - ); + ).done(); }); }); describe('Run plan with lock: A -> B* -> (A -> C, A -> D)', function() { it('correct run order', function(done) { var state = []; - Q.done(getAPW(getArch(state)).process('3A'), + Q.when(getAPW(getArch(state)).process('3A'), function() { ASSERT.equal(state.length, 4); ASSERT.equal(state[0], '3B'); @@ -135,7 +135,7 @@ describe('Run plan with lock: A -> B* -> (A -> C, A -> D)', function() { ASSERT.equal(state[3], '3A'); done(); }, done - ); + ).done(); }); }); @@ -147,24 +147,24 @@ describe('Run plans on same node', function() { apw.workers.addPlan(arch.createPlan('4A')); - Q.done(apw.process('4B'), + Q.when(apw.process('4B'), function() { done(); }, done - ); + ).done(); }); }); describe('All done subscribers', function() { it('allDone subscribers fired', function(done) { var state = []; - Q.done(getAPW(getArch(state)).process('5A'), + Q.when(getAPW(getArch(state)).process('5A'), function() { ASSERT.equal(state.length, 2); ASSERT.equal(state[0], '5A'); ASSERT.equal(state[1], '5B'); done(); }, done - ); + ).done(); }); }); From 8f5623d97d84a96e93dfde0f62d8fee8ae6a5cd2 Mon Sep 17 00:00:00 2001 From: Vladimir Alaev Date: Wed, 13 Mar 2013 21:36:15 +0200 Subject: [PATCH 2/2] Update bo vow 0.2.0 --- lib/arch.js | 4 +++- lib/workers.js | 5 +++-- package.json | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/arch.js b/lib/arch.js index 96a4e44..6700cc7 100644 --- a/lib/arch.js +++ b/lib/arch.js @@ -74,9 +74,11 @@ module.exports = INHERIT(/** @lends Arch.prototype */ { withLock: function(cb, context) { var _this = this; this.lock(); + return U.qcall(cb.bind(context)) - .always(function() { + .always(function(p) { _this.unlock(); + if (p.isRejected()) throw p.valueOf(); }); }, diff --git a/lib/workers.js b/lib/workers.js index b0d89c0..420179e 100644 --- a/lib/workers.js +++ b/lib/workers.js @@ -269,9 +269,10 @@ module.exports = INHERIT(/** @lends Workers.prototype */ { plan: plan }); job.node.ctx = ctx; - return U.qcall(job.node.run, job.node, ctx) - .always(function() { + return Q.when(job.node.run.call(job.node, ctx)) + .always(function(p) { delete job.node.ctx; + if (p.isRejected()) throw p.valueOf(); }); }.apply(this)); diff --git a/package.json b/package.json index 5c89dee..8f86bfc 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "node": ">= 0.6.0" }, "dependencies": { - "vow": "~0.1.12", + "vow": "~0.2.0", "coa": "~0.3.8", "inherit": "1", "node.extend": "1"