diff --git a/src/lib/nodes/abstract.client.ts b/src/lib/nodes/abstract.client.ts index 1edf8a3a2..536334d91 100644 --- a/src/lib/nodes/abstract.client.ts +++ b/src/lib/nodes/abstract.client.ts @@ -30,18 +30,39 @@ export abstract class Client { */ type: NodeType + /** + * Resolves when at least one node is ready to accept requests + */ + ready: Promise + resolve = () => {} + initialized = false + constructor(type: NodeType, kind: NodeKind = 'node') { this.type = type this.kind = kind this.useFastest = nodesStorage.getUseFastest(type) + + this.ready = new Promise((resolve) => { + this.resolve = () => { + if (this.initialized) return + + this.initialized = true + resolve() + } + }) } protected async watchNodeStatusChange() { for (const node of this.nodes) { - node.onStatusChange((node) => { + node.onStatusChange((nodeStatus) => { this.updateSyncStatuses() - this.statusUpdateCallback?.(node) + this.statusUpdateCallback?.(nodeStatus) + + if (this.isActiveNode(node)) { + // Resolve when at least one node is ready to accept requests + this.resolve() + } }) } } diff --git a/src/lib/nodes/adm/AdmClient.ts b/src/lib/nodes/adm/AdmClient.ts index f4349b53f..a4f635077 100644 --- a/src/lib/nodes/adm/AdmClient.ts +++ b/src/lib/nodes/adm/AdmClient.ts @@ -42,6 +42,8 @@ export class AdmClient extends Client { * @param {RequestConfig} config request config */ async request

(config: RequestConfig

): Promise { + await this.ready + return this.getNode() .request(config) .catch((error) => {