From af08c1f01ce2f91e699e0c44a6df458feef8aeae Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 31 Aug 2022 16:40:54 -0400 Subject: [PATCH] *make getNetwork async and wait on a new moduleReady promise --- src/index.ts | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 31aae2a..5138c3b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,6 +24,10 @@ function idFactory(start = 1, step = 1, limit = 2 ** 32) { const nextId = idFactory(1); let defaultNetwork: RpcNetwork; +let moduleReadyResolve: Function; +let moduleReady: Promise = new Promise((resolve) => { + moduleReadyResolve = resolve; +}); const networkInstances = new Map(); @@ -41,6 +45,7 @@ async function handlePresentSeed() { if (!defaultNetwork) { defaultNetwork = networkInstances.get(await createNetwork()) as RpcNetwork; } + moduleReadyResolve(); } async function handleCreateNetwork(aq: ActiveQuery) { @@ -55,7 +60,7 @@ async function handleAddRelay(aq: ActiveQuery) { return; } - const network = getNetwork(aq); + const network = await getNetwork(aq); network.addRelay(pubkey); try { @@ -65,7 +70,7 @@ async function handleAddRelay(aq: ActiveQuery) { aq.respond(); } -function handleRemoveRelay(aq: ActiveQuery) { +async function handleRemoveRelay(aq: ActiveQuery) { const { pubkey = null } = aq.callerInput; if (!pubkey) { @@ -73,11 +78,11 @@ function handleRemoveRelay(aq: ActiveQuery) { return; } - aq.respond(getNetwork(aq).removeRelay(pubkey)); + aq.respond((await getNetwork(aq)).removeRelay(pubkey)); } async function handleClearRelays(aq: ActiveQuery) { - const network = getNetwork(aq); + const network = await getNetwork(aq); network.clearRelays(); await network.dht.clearRelays(); @@ -105,7 +110,7 @@ async function handleSimpleQuery(aq: ActiveQuery) { return; } - const network = getNetwork(aq); + const network = await getNetwork(aq); let resp: RPCResponse | null = null; @@ -157,7 +162,7 @@ async function handleStreamingQuery(aq: ActiveQuery) { return; } - const network = getNetwork(aq); + const network = await getNetwork(aq); let resp: RPCResponse | null = null; @@ -194,7 +199,7 @@ async function handleWisdomQuery(aq: ActiveQuery) { return; } - const network = getNetwork(aq); + const network = await getNetwork(aq); let resp: RPCResponse | null = null; @@ -220,7 +225,9 @@ async function handleWisdomQuery(aq: ActiveQuery) { } async function handleReady(aq: ActiveQuery) { - await getNetwork(aq).ready; + await ( + await getNetwork(aq) + ).ready; aq.respond(); } async function createNetwork(def = true): Promise { @@ -231,9 +238,11 @@ async function createNetwork(def = true): Promise { return id; } -function getNetwork(aq: ActiveQuery): RpcNetwork { +async function getNetwork(aq: ActiveQuery): Promise { const { network = null } = aq.callerInput; + await moduleReady; + if (!network) { return defaultNetwork; }