@@ -25,13 +25,43 @@ const address = _config.default.api.address || 'localhost';
25
25
status . start ( ) ;
26
26
txPool . start ( ) ;
27
27
28
+ let userEvents ;
29
+
30
+ const delayedResult = ( res , payload , socket ) => {
31
+ const { params, result, delayed } = res ;
32
+ if ( delayed && userEvents ) {
33
+ const registry = delayed . registry || ! result . data && delayed . runIfEmpty ;
34
+ if ( payload . getDelayed ) {
35
+ const lastBlock = api . getLastBlock ( ) ;
36
+ const block = lastBlock ? lastBlock . number : null ;
37
+
38
+ userEvents . send ( {
39
+ action : delayed . action ,
40
+ module : delayed . module ,
41
+ params,
42
+ socketId : socket ? socket . id : undefined ,
43
+ payload,
44
+ block,
45
+ result } ) ;
46
+
47
+ }
48
+ res . result . delayed = { fields : delayed . fields , registry } ;
49
+ }
50
+ return res ;
51
+ } ;
52
+
53
+ const send = ( { res, response, payload } ) => {
54
+ const { result } = delayedResult ( response , payload ) ;
55
+ res . send ( result ) ;
56
+ } ;
57
+
28
58
// http server
29
- const httpServer = ( 0 , _HttpServer . HttpServer ) ( { api, status, log : _log . default } ) ;
59
+ const { httpServer } = ( 0 , _HttpServer . HttpServer ) ( { api, status, log : _log . default } , send ) ;
30
60
httpServer . listen ( port , address ) ;
31
61
const io = new _socket . default ( httpServer ) ;
32
62
33
63
// start userEvents api
34
- const userEvents = ( 0 , _UserEventsApi . default ) ( io , api , { log : _log . default } ) ;
64
+ userEvents = ( 0 , _UserEventsApi . default ) ( io , api , { log : _log . default } ) ;
35
65
36
66
io . httpServer . on ( 'listening' , ( ) => {
37
67
_log . default . info ( `Server listening on: ${ address || '0.0.0.0' } :${ port } ` ) ;
@@ -127,25 +157,9 @@ const address = _config.default.api.address || 'localhost';
127
157
socket . on ( 'data' , async payload => {
128
158
try {
129
159
const res = await api . run ( payload ) ;
130
- const { module, action, params, result, delayed } = res ;
131
- if ( delayed && userEvents ) {
132
- const registry = delayed . registry || ! result . data && delayed . runIfEmpty ;
133
- if ( payload . getDelayed ) {
134
- const lastBlock = api . getLastBlock ( ) ;
135
- const block = lastBlock ? lastBlock . number : null ;
136
- userEvents . send ( {
137
- action : delayed . action ,
138
- module : delayed . module ,
139
- params,
140
- socketId : socket . id ,
141
- payload,
142
- block,
143
- result } ) ;
144
-
145
- }
146
- result . delayed = { fields : delayed . fields , registry } ;
147
- }
148
- socket . emit ( 'data' , ( 0 , _apiTools . formatRes ) ( { module, action, result, req : payload } ) ) ;
160
+ const req = payload ;
161
+ const { module, action, result } = delayedResult ( res , payload , socket ) ;
162
+ socket . emit ( 'data' , ( 0 , _apiTools . formatRes ) ( { module, action, result, req } ) ) ;
149
163
} catch ( err ) {
150
164
_log . default . debug ( `Action: ${ payload . action } : ERROR: ${ err } ` ) ;
151
165
_log . default . trace ( err ) ;
0 commit comments