*Dont call init in constructor

*Rename init to run
*Make run a chained method and not async
This commit is contained in:
Derrick Hammer 2022-08-28 02:26:40 -04:00
parent b5bb0c1889
commit 00ccbc9536
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 13 additions and 13 deletions

View File

@ -107,7 +107,7 @@ export default class RpcNetwork {
bypassCache: bypassCache || this._bypassCache, bypassCache: bypassCache || this._bypassCache,
}, },
options options
); ).run();
} }
public streamingQuery( public streamingQuery(
@ -123,7 +123,7 @@ export default class RpcNetwork {
relay, relay,
{ method, module, data }, { method, module, data },
{ streamHandler, ...options } { streamHandler, ...options }
); ).run();
} }
public simpleQuery( public simpleQuery(
@ -142,6 +142,6 @@ export default class RpcNetwork {
data, data,
}, },
options options
); ).run();
} }
} }

View File

@ -27,7 +27,6 @@ export default abstract class RpcQueryBase {
this._network = network; this._network = network;
this._query = query; this._query = query;
this._options = options; this._options = options;
this.init();
} }
get result(): Promise<RPCResponse> { get result(): Promise<RPCResponse> {
@ -52,7 +51,7 @@ export default abstract class RpcQueryBase {
this._promiseResolve?.(data); this._promiseResolve?.(data);
} }
protected async init() { public run(): this {
this._promise = this._promise =
this._promise ?? this._promise ??
new Promise<any>((resolve) => { new Promise<any>((resolve) => {
@ -66,16 +65,17 @@ export default abstract class RpcQueryBase {
(this._options.queryTimeout || this._network.queryTimeout) * 1000 (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()) { Promise.allSettled(promises).then(() => this.checkResponses());
promises.push(this.queryRelay(relay)); });
}
await Promise.allSettled(promises); return this;
this.checkResponses();
} }
protected async queryRelay(relay: string | Buffer): Promise<any> { protected async queryRelay(relay: string | Buffer): Promise<any> {

View File

@ -69,7 +69,7 @@ export default class WisdomRpcQuery extends RpcQueryBase {
return; return;
} }
this.init(); this.run();
} }
protected getRelays(): string[] | [] { protected getRelays(): string[] | [] {