From 00ccbc95361bdc353a2dddd3aac5d96578514b59 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 28 Aug 2022 02:26:40 -0400 Subject: [PATCH] *Dont call init in constructor *Rename init to run *Make run a chained method and not async --- src/network.ts | 6 +++--- src/query/base.ts | 18 +++++++++--------- src/query/wisdom.ts | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/network.ts b/src/network.ts index bc59632..10ce00f 100644 --- a/src/network.ts +++ b/src/network.ts @@ -107,7 +107,7 @@ export default class RpcNetwork { bypassCache: bypassCache || this._bypassCache, }, options - ); + ).run(); } public streamingQuery( @@ -123,7 +123,7 @@ export default class RpcNetwork { relay, { method, module, data }, { streamHandler, ...options } - ); + ).run(); } public simpleQuery( @@ -142,6 +142,6 @@ export default class RpcNetwork { data, }, options - ); + ).run(); } } diff --git a/src/query/base.ts b/src/query/base.ts index 57c0f34..217289e 100644 --- a/src/query/base.ts +++ b/src/query/base.ts @@ -27,7 +27,6 @@ export default abstract class RpcQueryBase { this._network = network; this._query = query; this._options = options; - this.init(); } get result(): Promise { @@ -52,7 +51,7 @@ export default abstract class RpcQueryBase { this._promiseResolve?.(data); } - protected async init() { + public run(): this { this._promise = this._promise ?? new Promise((resolve) => { @@ -66,16 +65,17 @@ export default abstract class RpcQueryBase { (this._options.queryTimeout || this._network.queryTimeout) * 1000 ); - await this._network.ready; + this._network.ready.then(() => { + const promises = []; - const promises = []; + for (const relay of this.getRelays()) { + promises.push(this.queryRelay(relay)); + } - for (const relay of this.getRelays()) { - promises.push(this.queryRelay(relay)); - } + Promise.allSettled(promises).then(() => this.checkResponses()); + }); - await Promise.allSettled(promises); - this.checkResponses(); + return this; } protected async queryRelay(relay: string | Buffer): Promise { diff --git a/src/query/wisdom.ts b/src/query/wisdom.ts index a1ceed1..7d6083a 100644 --- a/src/query/wisdom.ts +++ b/src/query/wisdom.ts @@ -69,7 +69,7 @@ export default class WisdomRpcQuery extends RpcQueryBase { return; } - this.init(); + this.run(); } protected getRelays(): string[] | [] {