*make getNetwork async and wait on a new moduleReady promise

This commit is contained in:
Derrick Hammer 2022-08-31 16:40:54 -04:00
parent 4290641f94
commit af08c1f01c
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 18 additions and 9 deletions

View File

@ -24,6 +24,10 @@ function idFactory(start = 1, step = 1, limit = 2 ** 32) {
const nextId = idFactory(1); const nextId = idFactory(1);
let defaultNetwork: RpcNetwork; let defaultNetwork: RpcNetwork;
let moduleReadyResolve: Function;
let moduleReady: Promise<void> = new Promise((resolve) => {
moduleReadyResolve = resolve;
});
const networkInstances = new Map<number, RpcNetwork>(); const networkInstances = new Map<number, RpcNetwork>();
@ -41,6 +45,7 @@ async function handlePresentSeed() {
if (!defaultNetwork) { if (!defaultNetwork) {
defaultNetwork = networkInstances.get(await createNetwork()) as RpcNetwork; defaultNetwork = networkInstances.get(await createNetwork()) as RpcNetwork;
} }
moduleReadyResolve();
} }
async function handleCreateNetwork(aq: ActiveQuery) { async function handleCreateNetwork(aq: ActiveQuery) {
@ -55,7 +60,7 @@ async function handleAddRelay(aq: ActiveQuery) {
return; return;
} }
const network = getNetwork(aq); const network = await getNetwork(aq);
network.addRelay(pubkey); network.addRelay(pubkey);
try { try {
@ -65,7 +70,7 @@ async function handleAddRelay(aq: ActiveQuery) {
aq.respond(); aq.respond();
} }
function handleRemoveRelay(aq: ActiveQuery) { async function handleRemoveRelay(aq: ActiveQuery) {
const { pubkey = null } = aq.callerInput; const { pubkey = null } = aq.callerInput;
if (!pubkey) { if (!pubkey) {
@ -73,11 +78,11 @@ function handleRemoveRelay(aq: ActiveQuery) {
return; return;
} }
aq.respond(getNetwork(aq).removeRelay(pubkey)); aq.respond((await getNetwork(aq)).removeRelay(pubkey));
} }
async function handleClearRelays(aq: ActiveQuery) { async function handleClearRelays(aq: ActiveQuery) {
const network = getNetwork(aq); const network = await getNetwork(aq);
network.clearRelays(); network.clearRelays();
await network.dht.clearRelays(); await network.dht.clearRelays();
@ -105,7 +110,7 @@ async function handleSimpleQuery(aq: ActiveQuery) {
return; return;
} }
const network = getNetwork(aq); const network = await getNetwork(aq);
let resp: RPCResponse | null = null; let resp: RPCResponse | null = null;
@ -157,7 +162,7 @@ async function handleStreamingQuery(aq: ActiveQuery) {
return; return;
} }
const network = getNetwork(aq); const network = await getNetwork(aq);
let resp: RPCResponse | null = null; let resp: RPCResponse | null = null;
@ -194,7 +199,7 @@ async function handleWisdomQuery(aq: ActiveQuery) {
return; return;
} }
const network = getNetwork(aq); const network = await getNetwork(aq);
let resp: RPCResponse | null = null; let resp: RPCResponse | null = null;
@ -220,7 +225,9 @@ async function handleWisdomQuery(aq: ActiveQuery) {
} }
async function handleReady(aq: ActiveQuery) { async function handleReady(aq: ActiveQuery) {
await getNetwork(aq).ready; await (
await getNetwork(aq)
).ready;
aq.respond(); aq.respond();
} }
async function createNetwork(def = true): Promise<number> { async function createNetwork(def = true): Promise<number> {
@ -231,9 +238,11 @@ async function createNetwork(def = true): Promise<number> {
return id; return id;
} }
function getNetwork(aq: ActiveQuery): RpcNetwork { async function getNetwork(aq: ActiveQuery): Promise<RpcNetwork> {
const { network = null } = aq.callerInput; const { network = null } = aq.callerInput;
await moduleReady;
if (!network) { if (!network) {
return defaultNetwork; return defaultNetwork;
} }