Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace Q with Vow #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/arch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var UTIL = require('util'),
Q = require('q'),
Q = require('vow'),
INHERIT = require('inherit'),
ASSERTS = require('./asserts'),
Plan = require('./plan'),
Expand Down Expand Up @@ -74,9 +74,11 @@ 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(p) {
_this.unlock();
if (p.isRejected()) throw p.valueOf();
});
},

Expand Down
2 changes: 1 addition & 1 deletion lib/coa.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Q = require('q'),
var Q = require('vow'),
PATH = require('path'),
APW = require('./apw'),

Expand Down
16 changes: 16 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var Q = require('vow');
/**
* Wrap object in array if it is not array already.
*
Expand All @@ -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.
*
Expand Down
21 changes: 12 additions & 9 deletions lib/workers.js
Original file line number Diff line number Diff line change
@@ -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 */ {
Expand Down Expand Up @@ -65,7 +66,7 @@ module.exports = INHERIT(/** @lends Workers.prototype */ {
this.addPlan(plan);
this.next();

return defer.promise;
return defer;
},

/**
Expand All @@ -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;
},
Expand Down Expand Up @@ -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;
},
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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;
Expand All @@ -267,12 +269,13 @@ module.exports = INHERIT(/** @lends Workers.prototype */ {
plan: plan
});
job.node.ctx = ctx;
return Q.invoke(job.node, 'run', ctx)
.fin(function() {
return Q.when(job.node.run.call(job.node, ctx))
.always(function(p) {
delete job.node.ctx;
if (p.isRejected()) throw p.valueOf();
});

}.bind(this));
}.apply(this));
},

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"node": ">= 0.6.0"
},
"dependencies": {
"q": "~0.8.10",
"vow": "~0.2.0",
"coa": "~0.3.8",
"inherit": "1",
"node.extend": "1"
Expand Down
26 changes: 13 additions & 13 deletions test/workers-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Q = require('q'),
var Q = require('vow'),
APW = require('..'),
ASSERT = require('assert');

Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -135,7 +135,7 @@ describe('Run plan with lock: A -> B* -> (A -> C, A -> D)', function() {
ASSERT.equal(state[3], '3A');
done();
}, done
);
).done();
});
});

Expand All @@ -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();
});
});