From 6b9c865e12c60f719938308a6f6f4cebe4c51b1f Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 25 Mar 2023 09:42:42 -0400 Subject: [PATCH] *Handle web edge case where .dht returns an async function --- src/network.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/network.ts b/src/network.ts index 3521a3a..d03ff2c 100644 --- a/src/network.ts +++ b/src/network.ts @@ -2,7 +2,7 @@ import Hyperswarm from "hyperswarm"; import RpcNetworkQueryFactory from "./query/index.js"; import b4a from "b4a"; -import { createHash } from "./util.js"; +import { createHash, isPromise } from "./util.js"; export default class RpcNetwork { private _relaysAvailablePromise?: Promise; @@ -70,10 +70,18 @@ export default class RpcNetwork { get ready(): Promise { if (!this._ready) { - this._ready = this._swarm.dht.ready() as Promise; + let dht = this._swarm.dht; + if (typeof dht === "function") { + dht = dht(); + } + if (isPromise(dht)) { + this._ready = dht.then((dht: any) => dht.ready()); + } else { + this._ready = this._swarm.dht.ready() as Promise; + } } - return this._ready; + return this._ready as Promise; } get readyWithRelays(): Promise {