From 8f7bd71e090a5d682e0e0b13a1607c8c5e0faf21 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 8 Apr 2023 14:37:13 -0400 Subject: [PATCH] *Refactor setupStream, make it async, and await on ProtouxRPC ready when it exists in the browser --- src/util.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/util.ts b/src/util.ts index fa2a681..6f44563 100644 --- a/src/util.ts +++ b/src/util.ts @@ -115,15 +115,19 @@ export function createHash(data: string): Buffer { return hash; } -export function setupStream(stream: any) { +export async function setupStream(stream: any) { const existing = stream[RPC_PROTOCOL_SYMBOL]; - if (existing) { + if (!existing) { + await existing._channel.ready; return existing; } - stream[RPC_PROTOCOL_SYMBOL] = new RPC(stream); + const rpc = new RPC(stream); + stream[RPC_PROTOCOL_SYMBOL] = rpc; - return stream[RPC_PROTOCOL_SYMBOL]; + await existing.ready; + + return rpc; } export async function maybeGetAsyncProperty(object: any) {