@@ -41,7 +41,7 @@ const wait = (ms: number) =>
4141 setTimeout ( resolve , ms )
4242 ) ;
4343
44- function healthCheckerMock ( ) : MockedHealthChecker {
44+ function healthCheckerMock ( ) : MockedHealthChecker {
4545 let cb : ( health : SmoldotHealth ) => void = ( ) => undefined ;
4646 let sendJsonRpc : ( request : string ) => void = ( ) => undefined ;
4747 let isActive = false ;
@@ -66,7 +66,7 @@ function healthCheckerMock (): MockedHealthChecker {
6666 } ;
6767}
6868
69- function healthCheckerFactory ( ) {
69+ function healthCheckerFactory ( ) {
7070 const _healthCheckers : MockedHealthChecker [ ] = [ ] ;
7171
7272 return {
@@ -82,13 +82,29 @@ function healthCheckerFactory () {
8282 } ;
8383}
8484
85- function getFakeChain ( spec : string , callback : Sc . JsonRpcCallback ) : MockChain {
85+ function getFakeChain ( spec : string ) : MockChain {
8686 const _receivedRequests : string [ ] = [ ] ;
8787 let _isTerminated = false ;
8888
8989 let terminateInterceptor = Function . prototype ;
9090 let sendJsonRpcInterceptor = Function . prototype ;
9191
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+
92108 return {
93109 _getLatestRequest : ( ) => _receivedRequests [ _receivedRequests . length - 1 ] ,
94110 _isTerminated : ( ) => _isTerminated ,
@@ -101,14 +117,15 @@ function getFakeChain (spec: string, callback: Sc.JsonRpcCallback): MockChain {
101117 } ,
102118 _spec : ( ) => spec ,
103119 _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+
109125 } ,
110- addChain : ( chainSpec , jsonRpcCallback ) =>
111- Promise . resolve ( getFakeChain ( chainSpec , jsonRpcCallback ?? noop ) ) ,
126+ nextJsonRpcResponse,
127+ jsonRpcResponses : jsonRpcResponsesGenerator ( ) ,
128+ addChain : ( chainSpec ) => Promise . resolve ( getFakeChain ( chainSpec ) ) ,
112129 remove : ( ) => {
113130 terminateInterceptor ( ) ;
114131 _isTerminated = true ;
@@ -120,11 +137,27 @@ function getFakeChain (spec: string, callback: Sc.JsonRpcCallback): MockChain {
120137 } ;
121138}
122139
123- function getFakeClient ( ) {
140+ function getFakeClient ( ) {
124141 const chains : MockChain [ ] = [ ] ;
125142 let addChainInterceptor : Promise < void > = Promise . resolve ( ) ;
126143 let addWellKnownChainInterceptor : Promise < void > = Promise . resolve ( ) ;
127144
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+
128161 return {
129162 _chains : ( ) => chains ,
130163 _setAddChainInterceptor : ( interceptor : Promise < void > ) => {
@@ -133,29 +166,12 @@ function getFakeClient () {
133166 _setAddWellKnownChainInterceptor : ( interceptor : Promise < void > ) => {
134167 addWellKnownChainInterceptor = interceptor ;
135168 } ,
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
155171 } ;
156172}
157173
158- function connectorFactory ( ) : MockSc {
174+ function connectorFactory ( ) : MockSc {
159175 const clients : ReturnType < typeof getFakeClient > [ ] = [ ] ;
160176 const latestClient = ( ) => clients [ clients . length - 1 ] ;
161177
@@ -175,7 +191,7 @@ function connectorFactory (): MockSc {
175191 } as unknown as MockSc ;
176192}
177193
178- function setChainSyncyingStatus ( isSyncing : boolean ) : void {
194+ function setChainSyncyingStatus ( isSyncing : boolean ) : void {
179195 getCurrentHealthChecker ( ) . _triggerHealthUpdate ( {
180196 isSyncing,
181197 peers : 1 ,
0 commit comments