-
Notifications
You must be signed in to change notification settings - Fork 477
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 all commits
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,6 @@ | |
'use strict'; | ||
|
||
var defs = require('./defs'); | ||
var Promise = require('bluebird'); | ||
var inherits = require('util').inherits; | ||
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. 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 commentThe reason will be displayed to describe this comment to others. Learn more. I'll open PR for this |
||
var EventEmitter = require('events').EventEmitter; | ||
var BaseChannel = require('./channel').BaseChannel; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ | |
'use strict'; | ||
|
||
var defs = require('./defs'); | ||
var Promise = require('bluebird'); | ||
var Bluebird = require('bluebird'); | ||
const { promisify } = require('util'); | ||
var inherits = require('util').inherits; | ||
var EventEmitter = require('events').EventEmitter; | ||
var BaseChannel = require('./channel').BaseChannel; | ||
|
@@ -29,7 +30,7 @@ module.exports.ChannelModel = ChannelModel; | |
var CM = ChannelModel.prototype; | ||
|
||
CM.close = function() { | ||
return Promise.fromCallback(this.connection.close.bind(this.connection)); | ||
return promisify(this.connection.close.bind(this.connection)); | ||
}; | ||
|
||
// Channels | ||
|
@@ -54,30 +55,28 @@ var C = Channel.prototype; | |
// response's fields; this is intended to be suitable for implementing | ||
// 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 f.fields; | ||
}); | ||
const rpc = promisify(this._rpc.bind(this)); | ||
|
||
return rpc(method, fields, expect).then(f => f.fields); | ||
}; | ||
|
||
// Do the remarkably simple channel open handshake | ||
C.open = function() { | ||
return Promise.try(this.allocate.bind(this)).then( | ||
function(ch) { | ||
return ch.rpc(defs.ChannelOpen, {outOfBand: ""}, | ||
defs.ChannelOpenOk); | ||
}); | ||
C.open = async function () { | ||
const ch = this.allocate.call(this); | ||
|
||
return ch.rpc(defs.ChannelOpen, {outOfBand: ""}, defs.ChannelOpenOk); | ||
}; | ||
|
||
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); | ||
}); | ||
// const closeBecause = promisify(this.closeBecause.bind(this)); | ||
// | ||
// return closeBecause("Goodbye", defs.constants.REPLY_SUCCESS); | ||
}; | ||
|
||
// === Public API, declaring queues and stuff === | ||
|
@@ -167,23 +166,28 @@ 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; | ||
}); | ||
}; | ||
|
||
C.cancel = function(consumerTag) { | ||
var self = this; | ||
return Promise.fromCallback(function(cb) { | ||
self._rpc(defs.BasicCancel, Args.cancel(consumerTag), | ||
defs.BasicCancelOk, | ||
cb); | ||
}) | ||
.then(function(ok) { | ||
const rpc = promisify(this._rpc.bind(this)); | ||
|
||
return rpc(defs.BasicCancel, Args.cancel(consumerTag), defs.BasicCancelOk).then(ok => { | ||
self.unregisterConsumer(consumerTag); | ||
return ok.fields; | ||
}); | ||
|
@@ -192,9 +196,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.
promisify is undefined