1515 * See the License for the specific language governing permissions and
1616 * limitations under the License.
1717 */
18+ import { send , build , getBlock , decode } from "@onflow/fcl" ;
1819
1920const { spawn } = require ( "child_process" ) ;
2021
@@ -87,6 +88,21 @@ export class Emulator {
8788 this . logProcessor = ( item ) => item ;
8889
8990 return new Promise ( ( resolve , reject ) => {
91+ let internalId ;
92+ const checkLiveness = async function ( ) {
93+ try {
94+ await send ( build ( [ getBlock ( false ) ] ) ) . then ( decode ) ;
95+
96+ console . log ( "Flow emulator is ready" )
97+ clearInterval ( internalId ) ;
98+ this . initialized = true ;
99+ resolve ( true ) ;
100+ } catch ( err ) { // eslint-disable-line no-unused-vars
101+ console . log ( "Flow emulator not ready yet" )
102+ }
103+ }
104+ internalId = setInterval ( checkLiveness , 100 ) ;
105+
90106 this . process . stdout . on ( "data" , ( data ) => {
91107 // const buf = this.parseDataBuffer(data);
92108
@@ -106,20 +122,20 @@ export class Emulator {
106122 }
107123 if ( data . includes ( "Starting HTTP server" ) ) {
108124 this . log ( "EMULATOR IS UP! Listening for events!" ) ;
109- this . initialized = true ;
110- resolve ( true ) ;
111125 }
112126 } ) ;
113127
114128 this . process . stderr . on ( "data" , ( data ) => {
115129 this . log ( `ERROR: ${ data } ` , "error" ) ;
116130 this . initialized = false ;
131+ clearInterval ( internalId ) ;
117132 reject ( ) ;
118133 } ) ;
119134
120135 this . process . on ( "close" , ( code ) => {
121136 this . log ( `emulator exited with code ${ code } ` ) ;
122137 this . initialized = false ;
138+ clearInterval ( internalId ) ;
123139 resolve ( false ) ;
124140 } ) ;
125141 } ) ;
0 commit comments