@@ -12,17 +12,15 @@ function RedisPubSub(options) {
12
12
options || ( options = { } ) ;
13
13
14
14
this . client = options . client || redis . createClient ( options ) ;
15
+ this . _clientConnection = null ;
15
16
16
17
// Redis doesn't allow the same connection to both listen to channels and do
17
18
// operations. Make an extra redis connection for subscribing with the same
18
19
// options if not provided
19
20
this . observer = options . observer || redis . createClient ( this . client . options ) ;
21
+ this . _observerConnection = null ;
20
22
21
- var pubsub = this ;
22
- this . observer . on ( 'message' , function ( channel , message ) {
23
- var data = JSON . parse ( message ) ;
24
- pubsub . _emit ( channel , data ) ;
25
- } ) ;
23
+ this . _connect ( ) ;
26
24
}
27
25
module . exports = RedisPubSub ;
28
26
@@ -37,27 +35,59 @@ RedisPubSub.prototype.close = function(callback) {
37
35
var pubsub = this ;
38
36
PubSub . prototype . close . call ( this , function ( err ) {
39
37
if ( err ) return callback ( err ) ;
40
- pubsub . client . quit ( function ( err ) {
41
- if ( err ) return callback ( err ) ;
42
- pubsub . observer . quit ( callback ) ;
43
- } ) ;
38
+ pubsub . _close ( ) . then ( function ( ) {
39
+ callback ( ) ;
40
+ } , callback ) ;
44
41
} ) ;
45
42
} ;
46
43
44
+ RedisPubSub . prototype . _close = function ( ) {
45
+ return this . _closing = this . _closing || this . _connect ( ) . then ( Promise . all ( [
46
+ this . client . quit ( ) ,
47
+ this . observer . quit ( )
48
+ ] ) ) ;
49
+ } ;
50
+
47
51
RedisPubSub . prototype . _subscribe = function ( channel , callback ) {
48
- this . observer . subscribe ( channel , callback ) ;
52
+ var pubsub = this ;
53
+ pubsub . observer
54
+ . subscribe ( channel , function ( message ) {
55
+ var data = JSON . parse ( message ) ;
56
+ pubsub . _emit ( channel , data ) ;
57
+ } )
58
+ . then ( function ( ) {
59
+ callback ( ) ;
60
+ } , callback ) ;
49
61
} ;
50
62
51
63
RedisPubSub . prototype . _unsubscribe = function ( channel , callback ) {
52
- this . observer . unsubscribe ( channel , callback ) ;
64
+ this . observer . unsubscribe ( channel )
65
+ . then ( function ( ) {
66
+ callback ( ) ;
67
+ } , callback ) ;
53
68
} ;
54
69
55
70
RedisPubSub . prototype . _publish = function ( channels , data , callback ) {
56
71
var message = JSON . stringify ( data ) ;
57
- var args = [ PUBLISH_SCRIPT , 0 , message ] . concat ( channels ) ;
58
- this . client . eval ( args , callback ) ;
72
+ var args = [ message ] . concat ( channels ) ;
73
+ this . client . eval ( PUBLISH_SCRIPT , { arguments : args } ) . then ( function ( ) {
74
+ callback ( ) ;
75
+ } , callback ) ;
59
76
} ;
60
77
78
+ RedisPubSub . prototype . _connect = function ( ) {
79
+ this . _clientConnection = this . _clientConnection || connect ( this . client ) ;
80
+ this . _observerConnection = this . _observerConnection || connect ( this . observer ) ;
81
+ return Promise . all ( [
82
+ this . _clientConnection ,
83
+ this . _observerConnection
84
+ ] ) ;
85
+ } ;
86
+
87
+ function connect ( client ) {
88
+ return client . isOpen ? Promise . resolve ( ) : client . connect ( ) ;
89
+ }
90
+
61
91
var PUBLISH_SCRIPT =
62
92
'for i = 2, #ARGV do ' +
63
93
'redis.call("publish", ARGV[i], ARGV[1]) ' +
0 commit comments