Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 3aeb4f1

Browse files
authored
make gRPC call to getBlock to determine if emulator is fully up (#73)
1 parent c8fad25 commit 3aeb4f1

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flow-js-testing",
3-
"version": "0.2.1",
3+
"version": "0.2.2-alpha.3",
44
"description": "This package will expose a set of utility methods, to allow Cadence code testing with libraries like Jest",
55
"repository": {
66
"type": "git",

src/emulator.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
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

1920
const { 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

Comments
 (0)