5
5
'use strict' ;
6
6
7
7
const EventEmitter = require ( 'events' ) ;
8
- const Promise = require ( 'bluebird' ) ;
8
+ const promisify = require ( 'util' ) . promisify ;
9
9
const defs = require ( './defs' ) ;
10
10
const { BaseChannel} = require ( './channel' ) ;
11
11
const { acceptMessage} = require ( './channel' ) ;
@@ -23,7 +23,7 @@ class ChannelModel extends EventEmitter {
23
23
}
24
24
25
25
close ( ) {
26
- return Promise . fromCallback ( this . connection . close . bind ( this . connection ) ) ;
26
+ return promisify ( this . connection . close . bind ( this . connection ) ) ( ) ;
27
27
}
28
28
29
29
async createChannel ( ) {
@@ -53,27 +53,25 @@ class Channel extends BaseChannel {
53
53
// response's fields; this is intended to be suitable for implementing
54
54
// API procedures.
55
55
async rpc ( method , fields , expect ) {
56
- const f = await Promise . fromCallback ( cb => {
56
+ const f = await promisify ( cb => {
57
57
return this . _rpc ( method , fields , expect , cb ) ;
58
- } )
58
+ } ) ( ) ;
59
59
60
60
return f . fields ;
61
61
}
62
62
63
63
// Do the remarkably simple channel open handshake
64
- open ( ) {
65
- return Promise . try ( this . allocate . bind ( this ) ) . then (
66
- ch => {
67
- return ch . rpc ( defs . ChannelOpen , { outOfBand : "" } ,
68
- defs . ChannelOpenOk ) ;
69
- } ) ;
64
+ async open ( ) {
65
+ const ch = await this . allocate . bind ( this ) ( ) ;
66
+ return ch . rpc ( defs . ChannelOpen , { outOfBand : "" } ,
67
+ defs . ChannelOpenOk ) ;
70
68
}
71
69
72
70
close ( ) {
73
- return Promise . fromCallback ( cb => {
71
+ return promisify ( cb => {
74
72
return this . closeBecause ( "Goodbye" , defs . constants . REPLY_SUCCESS ,
75
73
cb ) ;
76
- } ) ;
74
+ } ) ( ) ;
77
75
}
78
76
79
77
// === Public API, declaring queues and stuff ===
@@ -162,21 +160,21 @@ class Channel extends BaseChannel {
162
160
// NB we want the callback to be run synchronously, so that we've
163
161
// registered the consumerTag before any messages can arrive.
164
162
const fields = Args . consume ( queue , options ) ;
165
- return Promise . fromCallback ( cb => {
166
- this . _rpc ( defs . BasicConsume , fields , defs . BasicConsumeOk , cb ) ;
167
- } )
168
- . then ( ok => {
169
- this . registerConsumer ( ok . fields . consumerTag , callback ) ;
170
- return ok . fields ;
163
+ return new Promise ( ( resolve , reject ) => {
164
+ this . _rpc ( defs . BasicConsume , fields , defs . BasicConsumeOk , ( err , ok ) => {
165
+ if ( err ) return reject ( err ) ;
166
+ this . registerConsumer ( ok . fields . consumerTag , callback ) ;
167
+ resolve ( ok . fields ) ;
168
+ } ) ;
171
169
} ) ;
172
170
}
173
171
174
172
async cancel ( consumerTag ) {
175
- const ok = await Promise . fromCallback ( cb => {
173
+ const ok = await promisify ( cb => {
176
174
this . _rpc ( defs . BasicCancel , Args . cancel ( consumerTag ) ,
177
175
defs . BasicCancelOk ,
178
176
cb ) ;
179
- } )
177
+ } ) ( )
180
178
. then ( ok => {
181
179
this . unregisterConsumer ( consumerTag ) ;
182
180
return ok . fields ;
@@ -185,25 +183,23 @@ class Channel extends BaseChannel {
185
183
186
184
get ( queue , options ) {
187
185
const fields = Args . get ( queue , options ) ;
188
- return Promise . fromCallback ( cb => {
189
- return this . sendOrEnqueue ( defs . BasicGet , fields , cb ) ;
190
- } )
191
- . then ( f => {
192
- if ( f . id === defs . BasicGetEmpty ) {
193
- return false ;
194
- }
195
- else if ( f . id === defs . BasicGetOk ) {
196
- const fields = f . fields ;
197
- return new Promise ( resolve => {
186
+ return new Promise ( ( resolve , reject ) => {
187
+ this . sendOrEnqueue ( defs . BasicGet , fields , ( err , f ) => {
188
+ if ( err ) return reject ( err ) ;
189
+ if ( f . id === defs . BasicGetEmpty ) {
190
+ return resolve ( false ) ;
191
+ }
192
+ else if ( f . id === defs . BasicGetOk ) {
193
+ const fields = f . fields ;
198
194
this . handleMessage = acceptMessage ( m => {
199
195
m . fields = fields ;
200
196
resolve ( m ) ;
201
197
} ) ;
202
- } ) ;
203
- }
204
- else {
205
- throw new Error ( `Unexpected response to BasicGet: ${ inspect ( f ) } ` ) ;
206
- }
198
+ }
199
+ else {
200
+ reject ( new Error ( `Unexpected response to BasicGet: ${ inspect ( f ) } ` ) ) ;
201
+ }
202
+ } ) ;
207
203
} ) ;
208
204
}
209
205
@@ -290,6 +286,13 @@ class ConfirmChannel extends Channel {
290
286
awaiting . push ( confirmed ) ;
291
287
}
292
288
} ) ;
289
+ // Channel closed
290
+ if ( ! this . pending ) {
291
+ var cb ;
292
+ while ( cb = this . unconfirmed . shift ( ) ) {
293
+ if ( cb ) cb ( new Error ( 'channel closed' ) ) ;
294
+ }
295
+ }
293
296
return Promise . all ( awaiting ) ;
294
297
}
295
298
}
0 commit comments