@@ -41,7 +41,7 @@ const wait = (ms: number) =>
41
41
setTimeout ( resolve , ms )
42
42
) ;
43
43
44
- function healthCheckerMock ( ) : MockedHealthChecker {
44
+ function healthCheckerMock ( ) : MockedHealthChecker {
45
45
let cb : ( health : SmoldotHealth ) => void = ( ) => undefined ;
46
46
let sendJsonRpc : ( request : string ) => void = ( ) => undefined ;
47
47
let isActive = false ;
@@ -66,7 +66,7 @@ function healthCheckerMock (): MockedHealthChecker {
66
66
} ;
67
67
}
68
68
69
- function healthCheckerFactory ( ) {
69
+ function healthCheckerFactory ( ) {
70
70
const _healthCheckers : MockedHealthChecker [ ] = [ ] ;
71
71
72
72
return {
@@ -82,13 +82,29 @@ function healthCheckerFactory () {
82
82
} ;
83
83
}
84
84
85
- function getFakeChain ( spec : string , callback : Sc . JsonRpcCallback ) : MockChain {
85
+ function getFakeChain ( spec : string ) : MockChain {
86
86
const _receivedRequests : string [ ] = [ ] ;
87
87
let _isTerminated = false ;
88
88
89
89
let terminateInterceptor = Function . prototype ;
90
90
let sendJsonRpcInterceptor = Function . prototype ;
91
91
92
+ const responseQueue : string [ ] = [ ]
93
+
94
+ const nextJsonRpcResponse = async ( ) => {
95
+ while ( responseQueue . length === 0 ) {
96
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
97
+ }
98
+ return responseQueue . shift ( ) ! ;
99
+ }
100
+
101
+ async function * jsonRpcResponsesGenerator ( ) : AsyncIterableIterator < string > {
102
+ while ( true ) {
103
+ const response = await nextJsonRpcResponse ( ) ;
104
+ yield response ;
105
+ }
106
+ }
107
+
92
108
return {
93
109
_getLatestRequest : ( ) => _receivedRequests [ _receivedRequests . length - 1 ] ,
94
110
_isTerminated : ( ) => _isTerminated ,
@@ -101,14 +117,15 @@ function getFakeChain (spec: string, callback: Sc.JsonRpcCallback): MockChain {
101
117
} ,
102
118
_spec : ( ) => spec ,
103
119
_triggerCallback : ( response ) => {
104
- callback (
105
- typeof response === 'string'
106
- ? response
107
- : stringify ( response )
108
- ) ;
120
+ const message = typeof response === 'string'
121
+ ? response
122
+ : stringify ( response )
123
+ responseQueue . push ( message )
124
+
109
125
} ,
110
- addChain : ( chainSpec , jsonRpcCallback ) =>
111
- Promise . resolve ( getFakeChain ( chainSpec , jsonRpcCallback ?? noop ) ) ,
126
+ nextJsonRpcResponse,
127
+ jsonRpcResponses : jsonRpcResponsesGenerator ( ) ,
128
+ addChain : ( chainSpec ) => Promise . resolve ( getFakeChain ( chainSpec ) ) ,
112
129
remove : ( ) => {
113
130
terminateInterceptor ( ) ;
114
131
_isTerminated = true ;
@@ -120,11 +137,27 @@ function getFakeChain (spec: string, callback: Sc.JsonRpcCallback): MockChain {
120
137
} ;
121
138
}
122
139
123
- function getFakeClient ( ) {
140
+ function getFakeClient ( ) {
124
141
const chains : MockChain [ ] = [ ] ;
125
142
let addChainInterceptor : Promise < void > = Promise . resolve ( ) ;
126
143
let addWellKnownChainInterceptor : Promise < void > = Promise . resolve ( ) ;
127
144
145
+ const addChain : Sc . AddChain = async ( chainSpec ) => addChainInterceptor . then ( ( ) => {
146
+ const result = getFakeChain ( chainSpec ) ;
147
+
148
+ chains . push ( result ) ;
149
+
150
+ return result ;
151
+ } )
152
+
153
+ const addWellKnownChain : Sc . AddWellKnownChain = async ( wellKnownChain ) => addWellKnownChainInterceptor . then ( ( ) => {
154
+ const result = getFakeChain ( wellKnownChain ) ;
155
+
156
+ chains . push ( result ) ;
157
+
158
+ return result ;
159
+ } )
160
+
128
161
return {
129
162
_chains : ( ) => chains ,
130
163
_setAddChainInterceptor : ( interceptor : Promise < void > ) => {
@@ -133,29 +166,12 @@ function getFakeClient () {
133
166
_setAddWellKnownChainInterceptor : ( interceptor : Promise < void > ) => {
134
167
addWellKnownChainInterceptor = interceptor ;
135
168
} ,
136
- addChain : ( chainSpec : string , cb : Sc . JsonRpcCallback ) : Promise < MockChain > =>
137
- addChainInterceptor . then ( ( ) => {
138
- const result = getFakeChain ( chainSpec , cb ) ;
139
-
140
- chains . push ( result ) ;
141
-
142
- return result ;
143
- } ) ,
144
- addWellKnownChain : (
145
- wellKnownChain : string ,
146
- cb : Sc . JsonRpcCallback
147
- ) : Promise < MockChain > =>
148
- addWellKnownChainInterceptor . then ( ( ) => {
149
- const result = getFakeChain ( wellKnownChain , cb ) ;
150
-
151
- chains . push ( result ) ;
152
-
153
- return result ;
154
- } )
169
+ addChain,
170
+ addWellKnownChain
155
171
} ;
156
172
}
157
173
158
- function connectorFactory ( ) : MockSc {
174
+ function connectorFactory ( ) : MockSc {
159
175
const clients : ReturnType < typeof getFakeClient > [ ] = [ ] ;
160
176
const latestClient = ( ) => clients [ clients . length - 1 ] ;
161
177
@@ -175,7 +191,7 @@ function connectorFactory (): MockSc {
175
191
} as unknown as MockSc ;
176
192
}
177
193
178
- function setChainSyncyingStatus ( isSyncing : boolean ) : void {
194
+ function setChainSyncyingStatus ( isSyncing : boolean ) : void {
179
195
getCurrentHealthChecker ( ) . _triggerHealthUpdate ( {
180
196
isSyncing,
181
197
peers : 1 ,
0 commit comments