-
Notifications
You must be signed in to change notification settings - Fork 476
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
feat: switch to native Promise #624
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
language: node_js | ||
node_js: | ||
- "0.8" | ||
- "0.10" | ||
- "0.12" | ||
- "iojs-v1" | ||
- "iojs-v2" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
'use strict'; | ||
|
||
var defs = require('./defs'); | ||
var Promise = require('bluebird'); | ||
var Bluebird = require('bluebird'); | ||
var inherits = require('util').inherits; | ||
var EventEmitter = require('events').EventEmitter; | ||
var BaseChannel = require('./channel').BaseChannel; | ||
|
@@ -29,7 +29,17 @@ module.exports.ChannelModel = ChannelModel; | |
var CM = ChannelModel.prototype; | ||
|
||
CM.close = function() { | ||
return Promise.fromCallback(this.connection.close.bind(this.connection)); | ||
var close = this.connection.close.bind(this.connection); | ||
|
||
return new Promise(function (resolve, reject) { | ||
close(function (err, result) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
// Channels | ||
|
@@ -55,17 +65,31 @@ var C = Channel.prototype; | |
// API procedures. | ||
C.rpc = function(method, fields, expect) { | ||
var self = this; | ||
return Promise.fromCallback(function(cb) { | ||
return self._rpc(method, fields, expect, cb); | ||
}) | ||
.then(function(f) { | ||
|
||
return new Promise(function (resolve, reject) { | ||
self._rpc(method, fields, expect, function (err, result) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
}).then(function (f) { | ||
return f.fields; | ||
}); | ||
}; | ||
|
||
// Do the remarkably simple channel open handshake | ||
C.open = function() { | ||
return Promise.try(this.allocate.bind(this)).then( | ||
var allocate = this.allocate.bind(this); | ||
|
||
return new Promise(function (resolve, reject) { | ||
try { | ||
resolve(allocate()); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}).then( | ||
function(ch) { | ||
return ch.rpc(defs.ChannelOpen, {outOfBand: ""}, | ||
defs.ChannelOpenOk); | ||
|
@@ -74,10 +98,16 @@ C.open = function() { | |
|
||
C.close = function() { | ||
var self = this; | ||
return Promise.fromCallback(function(cb) { | ||
|
||
return Bluebird.fromCallback(function(cb) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am I misunderstanding something, or promisify could be used here instead of Bluebird? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After dropping old Node version — it could There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only Node 10+ is supported in master, so should be fine to use it now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replaced with util.promisify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw .travis.yml in main still builds library for old Node.js versions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think it was just missed in 267dd4e. Probably Travis needs to be adjusted to only test for Node 10+ as well. |
||
return self.closeBecause("Goodbye", defs.constants.REPLY_SUCCESS, | ||
cb); | ||
}); | ||
// return new Promise(function(resolve, reject) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @squaremo could you help with those three places (lines 106, 203 and 243) If I replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be commented out? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need some help with this. If I replace |
||
// return self.closeBecause("Goodbye", defs.constants.REPLY_SUCCESS, | ||
// (err, result) => err ? reject(err) : resolve(result) | ||
// ); | ||
// }); | ||
}; | ||
|
||
// === Public API, declaring queues and stuff === | ||
|
@@ -167,9 +197,18 @@ C.consume = function(queue, callback, options) { | |
// NB we want the callback to be run synchronously, so that we've | ||
// registered the consumerTag before any messages can arrive. | ||
var fields = Args.consume(queue, options); | ||
return Promise.fromCallback(function(cb) { | ||
return Bluebird.fromCallback(function(cb) { | ||
self._rpc(defs.BasicConsume, fields, defs.BasicConsumeOk, cb); | ||
}) | ||
// return new Promise(function(resolve, reject) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be commented out? |
||
// self._rpc(defs.BasicConsume, fields, defs.BasicConsumeOk, function (err, result) { | ||
// if (err) { | ||
// reject(err); | ||
// } else { | ||
// resolve(result); | ||
// } | ||
// }); | ||
// }) | ||
.then(function(ok) { | ||
self.registerConsumer(ok.fields.consumerTag, callback); | ||
return ok.fields; | ||
|
@@ -178,10 +217,16 @@ C.consume = function(queue, callback, options) { | |
|
||
C.cancel = function(consumerTag) { | ||
var self = this; | ||
return Promise.fromCallback(function(cb) { | ||
return new Promise(function(resolve, reject) { | ||
self._rpc(defs.BasicCancel, Args.cancel(consumerTag), | ||
defs.BasicCancelOk, | ||
cb); | ||
function (err, result) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
}) | ||
.then(function(ok) { | ||
self.unregisterConsumer(consumerTag); | ||
|
@@ -192,9 +237,18 @@ C.cancel = function(consumerTag) { | |
C.get = function(queue, options) { | ||
var self = this; | ||
var fields = Args.get(queue, options); | ||
return Promise.fromCallback(function(cb) { | ||
return Bluebird.fromCallback(function(cb) { | ||
return self.sendOrEnqueue(defs.BasicGet, fields, cb); | ||
}) | ||
// return new Promise(function(resolve, reject) { | ||
// return self.sendOrEnqueue(defs.BasicGet, fields, function (err, result) { | ||
// if (err) { | ||
// reject(err); | ||
// } else { | ||
// resolve(result); | ||
// } | ||
// }); | ||
// }) | ||
.then(function(f) { | ||
if (f.id === defs.BasicGetEmpty) { | ||
return false; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this PR but i just notices inherits, NodeJS docs mention discourage use of inherits and that class extends should be used instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll open PR for this