@@ -13,8 +13,6 @@ const api = require('./api')
1313
1414axiosCookieJarSupport ( axios )
1515
16- let socket
17-
1816const baseURL = getIndexerApiUrl ( )
1917
2018const jar = new CookieJar ( )
@@ -23,33 +21,36 @@ const setTimeoutAsync = timeout => new Promise(function (resolve) {
2321 setTimeout ( resolve , timeout )
2422} )
2523
26- const getCookie = ( ) => axios . get ( baseURL , { jar, withCredentials : true } )
27- . then ( function ( ) {
28- socket = io ( `${ baseURL } /v1` , {
29- autoConnect : false ,
30- extraHeaders : {
31- Cookie : jar . getCookiesSync ( baseURL ) . join ( ';' )
32- }
24+ const getSocket = ( ) =>
25+ axios . get ( baseURL , { jar, withCredentials : true } )
26+ . then ( function ( ) {
27+ return io ( `${ baseURL } /v1` , {
28+ autoConnect : false ,
29+ extraHeaders : {
30+ Cookie : jar . getCookiesSync ( baseURL ) . join ( ';' )
31+ }
32+ } )
3333 } )
34- } )
35- . catch ( function ( err ) {
36- logger . warn ( 'Failed to get subscription cookie' , err . message )
34+ . catch ( function ( err ) {
35+ logger . warn ( 'Failed to get subscription cookie' , err . message )
3736
38- return setTimeoutAsync ( 5000 )
39- . then ( getCookie )
40- } )
37+ return setTimeoutAsync ( 5000 )
38+ . then ( getSocket )
39+ } )
4140
42- const initialized = getCookie ( )
41+ const initialized = getSocket ( )
4342
4443function subscribeBlocks ( eventsBus ) {
45- initialized . then ( function ( ) {
46- socket . emit ( 'subscribe' , { type : 'blocks' } , function ( err ) {
47- if ( err ) {
48- logger . warn ( 'Blocks subscription failed' , err )
49- return
50- }
51-
52- logger . debug ( 'Blocks subscription successfull' )
44+ initialized . then ( function ( socket ) {
45+ socket . on ( 'connect' , function ( ) {
46+ socket . emit ( 'subscribe' , { type : 'blocks' } , function ( err ) {
47+ if ( err ) {
48+ logger . warn ( 'Blocks subscription failed' , err )
49+ return
50+ }
51+
52+ logger . debug ( 'Blocks subscription successfull' )
53+ } )
5354 } )
5455
5556 socket . on ( 'block' , function ( data ) {
@@ -61,7 +62,7 @@ function subscribeBlocks (eventsBus) {
6162}
6263
6364const start = eventsBus => function ( pluginEmitter ) {
64- initialized . then ( function ( ) {
65+ initialized . then ( function ( socket ) {
6566 socket . on ( 'error' , function ( err ) {
6667 logger . warn ( 'Connection error' , err . message )
6768
@@ -72,8 +73,6 @@ const start = eventsBus => function (pluginEmitter) {
7273 logger . debug ( 'Client connected' )
7374
7475 pluginEmitter . emit ( 'connection-state-changed' , 'connected' )
75-
76- subscribeBlocks ( eventsBus )
7776 } )
7877
7978 socket . on ( 'disconnect' , function ( reason ) {
@@ -82,6 +81,8 @@ const start = eventsBus => function (pluginEmitter) {
8281 pluginEmitter . emit ( 'connection-state-changed' , 'disconnected' )
8382 } )
8483
84+ subscribeBlocks ( eventsBus )
85+
8586 socket . open ( )
8687 } )
8788}
@@ -99,45 +100,46 @@ function broadcastConnectivityState (subscriptions, state) {
99100}
100101
101102function stop ( ) {
102- initialized . then ( function ( ) {
103+ initialized . then ( function ( socket ) {
103104 socket . removeAllListeners ( 'error' )
104-
105105 socket . removeAllListeners ( 'connect' )
106-
107106 socket . removeAllListeners ( 'disconnect' )
108107
108+ socket . removeAllListeners ( 'block' )
109+ socket . removeAllListeners ( 'tx' )
110+
109111 socket . close ( )
112+
113+ logger . debug ( 'Block and tx listeners stopped' )
110114 } )
111115}
112116
113- function subscribeAddresses ( { eventsBus, walletId, addresses } ) {
114- initialized . then ( function ( ) {
115- socket . emit ( 'subscribe' , { type : 'txs' , addresses } , function ( err ) {
116- if ( err ) {
117- logger . warn ( 'Subscription failed' , walletId , err )
118- return
119- }
120-
121- logger . debug ( 'Addresses subscription successfull' , addresses , walletId )
122- } )
123-
124- socket . on ( 'tx' , function ( data ) {
125- const { type, txid, status, meta } = data
126-
127- logger . verbose ( 'New transaction received' , { walletId, data } )
117+ function subscribeAddresses ( socket , walletId , addresses ) {
118+ socket . emit ( 'subscribe' , { type : 'txs' , addresses } , function ( err ) {
119+ if ( err ) {
120+ logger . warn ( 'Subscription failed' , walletId , err )
121+ return
122+ }
128123
129- eventsBus . emit ( `${ type } -tx-${ status } ` , { walletId, txid, meta } )
130- } )
124+ logger . debug ( 'Addresses subscription successfull' , addresses , walletId )
131125 } )
132126}
133127
134128function attachToEvents ( eventsBus ) {
135129 eventsBus . on ( 'wallet-opened' , function ( { walletId, addresses } ) {
136- initialized . then ( function ( ) {
137- subscribeAddresses ( { eventsBus, walletId, addresses } )
130+ initialized . then ( function ( socket ) {
131+ socket . on ( 'tx' , function ( data ) {
132+ const { type, txid, status, meta } = data
133+
134+ logger . verbose ( 'New transaction received' , { walletId, data } )
135+
136+ eventsBus . emit ( `${ type } -tx-${ status } ` , { walletId, txid, meta } )
137+ } )
138+
139+ subscribeAddresses ( socket , walletId , addresses )
138140
139141 socket . on ( 'connect' , function ( ) {
140- subscribeAddresses ( { eventsBus , walletId, addresses } )
142+ subscribeAddresses ( socket , walletId , addresses )
141143 } )
142144 } )
143145 } )
0 commit comments