refactor: use defers and add clientInitDefer
This commit is contained in:
parent
f45bf1c43a
commit
4a7feb9a98
|
@ -11,11 +11,12 @@
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lumeweb/bigint-crypto-utils": "^1.0.0",
|
"@lumeweb/bigint-crypto-utils": "^1.0.0",
|
||||||
"@lumeweb/kernel-network-registry-client": "^0.1.0-develop.4",
|
"@lumeweb/kernel-network-registry-client": "^0.1.0-develop.5",
|
||||||
"@lumeweb/kernel-rpc-client": "0.0.2-develop.10",
|
"@lumeweb/kernel-rpc-client": "0.0.2-develop.10",
|
||||||
"@lumeweb/libethsync": "0.1.0-develop.42",
|
"@lumeweb/libethsync": "0.1.0-develop.45",
|
||||||
"@lumeweb/libkernel": "0.1.0-develop.16",
|
"@lumeweb/libkernel": "0.1.0-develop.16",
|
||||||
"@lumeweb/libweb": "0.2.0-develop.22"
|
"@lumeweb/libweb": "0.2.0-develop.22",
|
||||||
|
"p-defer": "^4.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@lumeweb/presetter-kernel-module-preset": "0.1.0-develop.42",
|
"@lumeweb/presetter-kernel-module-preset": "0.1.0-develop.42",
|
||||||
|
@ -3098,9 +3099,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@lumeweb/kernel-network-registry-client": {
|
"node_modules/@lumeweb/kernel-network-registry-client": {
|
||||||
"version": "0.1.0-develop.4",
|
"version": "0.1.0-develop.5",
|
||||||
"resolved": "https://registry.npmjs.org/@lumeweb/kernel-network-registry-client/-/kernel-network-registry-client-0.1.0-develop.4.tgz",
|
"resolved": "https://registry.npmjs.org/@lumeweb/kernel-network-registry-client/-/kernel-network-registry-client-0.1.0-develop.5.tgz",
|
||||||
"integrity": "sha512-41VKVCJ71evA+uUdoVxsxLx2hP029Hdv0jmasvVBh/WuSyYiDUm47pdXa+4ct8fIwsLHLCzpFAR/CkpL2F/kog==",
|
"integrity": "sha512-4ijBHvJKtiOrGrzJkytmNBNa0FpRy8y9w2OmzxP1yA4aCjVq5hxqKR0NSupXeBjhMNCD8Va3aM0a6I7tGFnF1w==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lumeweb/libkernel": "^0.1.0-develop.20"
|
"@lumeweb/libkernel": "^0.1.0-develop.20"
|
||||||
}
|
}
|
||||||
|
@ -3145,9 +3146,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@lumeweb/libethsync": {
|
"node_modules/@lumeweb/libethsync": {
|
||||||
"version": "0.1.0-develop.42",
|
"version": "0.1.0-develop.45",
|
||||||
"resolved": "https://registry.npmjs.org/@lumeweb/libethsync/-/libethsync-0.1.0-develop.42.tgz",
|
"resolved": "https://registry.npmjs.org/@lumeweb/libethsync/-/libethsync-0.1.0-develop.45.tgz",
|
||||||
"integrity": "sha512-pdKi+iVj82L4lKYeFzdLimZOgkDRRYlRr0zlfjS+Sk6xj5rLnf8q9jgYJtjnLCMfgkwldsdY22jCpvq17v3wWA==",
|
"integrity": "sha512-ijyWt1onyYB0/lSfts8EaZ76DACKjc+xj1ZGOV2/VecETw66gJAr6uyySnTK7VLztzBv0FQA4yvNwUSj8uUIeQ==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@chainsafe/as-sha256": "^0.3.1",
|
"@chainsafe/as-sha256": "^0.3.1",
|
||||||
"@chainsafe/bls": "7.1.1",
|
"@chainsafe/bls": "7.1.1",
|
||||||
|
@ -3169,6 +3170,7 @@
|
||||||
"axios-retry": "^3.5.1",
|
"axios-retry": "^3.5.1",
|
||||||
"detect-node": "^2.1.0",
|
"detect-node": "^2.1.0",
|
||||||
"ethereum-cryptography": "^2.0.0",
|
"ethereum-cryptography": "^2.0.0",
|
||||||
|
"events": "^3.3.0",
|
||||||
"node-cache": "^5.1.2",
|
"node-cache": "^5.1.2",
|
||||||
"rlp": "^3.0.0",
|
"rlp": "^3.0.0",
|
||||||
"web3-types": "^1.0.1"
|
"web3-types": "^1.0.1"
|
||||||
|
|
|
@ -23,11 +23,12 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lumeweb/bigint-crypto-utils": "^1.0.0",
|
"@lumeweb/bigint-crypto-utils": "^1.0.0",
|
||||||
"@lumeweb/kernel-network-registry-client": "^0.1.0-develop.4",
|
"@lumeweb/kernel-network-registry-client": "^0.1.0-develop.5",
|
||||||
"@lumeweb/kernel-rpc-client": "0.0.2-develop.10",
|
"@lumeweb/kernel-rpc-client": "0.0.2-develop.10",
|
||||||
"@lumeweb/libethsync": "0.1.0-develop.42",
|
"@lumeweb/libethsync": "0.1.0-develop.45",
|
||||||
"@lumeweb/libkernel": "0.1.0-develop.16",
|
"@lumeweb/libkernel": "0.1.0-develop.16",
|
||||||
"@lumeweb/libweb": "0.2.0-develop.22"
|
"@lumeweb/libweb": "0.2.0-develop.22",
|
||||||
|
"p-defer": "^4.0.0"
|
||||||
},
|
},
|
||||||
"overrides": {
|
"overrides": {
|
||||||
"bigint-crypto-utils": "@lumeweb/bigint-crypto-utils"
|
"bigint-crypto-utils": "@lumeweb/bigint-crypto-utils"
|
||||||
|
|
15
src/index.ts
15
src/index.ts
|
@ -16,16 +16,15 @@ import {
|
||||||
createDefaultClient as createEthClient,
|
createDefaultClient as createEthClient,
|
||||||
} from "@lumeweb/libethsync/client";
|
} from "@lumeweb/libethsync/client";
|
||||||
import * as capella from "@lodestar/types/capella";
|
import * as capella from "@lodestar/types/capella";
|
||||||
|
import defer from "p-defer";
|
||||||
|
|
||||||
onmessage = handleMessage;
|
onmessage = handleMessage;
|
||||||
|
|
||||||
const TYPES = ["blockchain"];
|
const TYPES = ["blockchain"];
|
||||||
const networkRegistry = createNetworkRegistryClient();
|
const networkRegistry = createNetworkRegistryClient();
|
||||||
|
|
||||||
let moduleReadyResolve: Function;
|
const moduleReadyDefer = defer();
|
||||||
let moduleReady: Promise<void> = new Promise((resolve) => {
|
const clientInitDefer = defer();
|
||||||
moduleReadyResolve = resolve;
|
|
||||||
});
|
|
||||||
|
|
||||||
let client: EthClient;
|
let client: EthClient;
|
||||||
let rpc: RpcNetwork;
|
let rpc: RpcNetwork;
|
||||||
|
@ -70,11 +69,11 @@ addHandler("ready", handleReady);
|
||||||
|
|
||||||
async function handlePresentKey() {
|
async function handlePresentKey() {
|
||||||
await setup();
|
await setup();
|
||||||
moduleReadyResolve();
|
moduleReadyDefer.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleRpcMethod(aq: ActiveQuery) {
|
async function handleRpcMethod(aq: ActiveQuery) {
|
||||||
await moduleReady;
|
await moduleReadyDefer.promise;
|
||||||
if (!client.isSynced) {
|
if (!client.isSynced) {
|
||||||
await client.sync();
|
await client.sync();
|
||||||
}
|
}
|
||||||
|
@ -154,6 +153,8 @@ async function setup() {
|
||||||
500,
|
500,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
clientInitDefer.resolve();
|
||||||
|
|
||||||
let synced = false;
|
let synced = false;
|
||||||
|
|
||||||
while (!synced) {
|
while (!synced) {
|
||||||
|
@ -167,7 +168,7 @@ async function setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleReady(aq: ActiveQuery) {
|
async function handleReady(aq: ActiveQuery) {
|
||||||
await moduleReady;
|
await moduleReadyDefer.promise;
|
||||||
|
|
||||||
aq.respond();
|
aq.respond();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue