diff --git a/packages/wasm/lib/services/relay-service/service.js b/packages/wasm/lib/services/relay-service/service.js index 4c749958..e5e13397 100644 --- a/packages/wasm/lib/services/relay-service/service.js +++ b/packages/wasm/lib/services/relay-service/service.js @@ -6,9 +6,29 @@ import { serviceName, validation, } from './configs'; - +import {dockService} from '../dock/service'; import {RelayService as relayServiceClient} from '@docknetwork/wallet-sdk-relay-service/lib'; +export function waitFor(condition, timeout) { + return new Promise((resolve, reject) => { + const interval = setInterval(async () => { + if (await Promise.resolve(condition())) { + clearInterval(interval); + resolve(true); + } + }, 400); + + setTimeout(() => { + clearInterval(interval); + reject(new Error('Timed out')); + }, timeout); + }); +} + +function ensureDockNetwork() { + return waitFor(() => dockService.isApiConnected(), 8000); +} + /** * RelayService */ @@ -24,18 +44,27 @@ export class RelayService { this.name = serviceName; } - sendMessage(params: SendMessageParams) { + async sendMessage(params: SendMessageParams) { validation.sendMessage(params); + + await ensureDockNetwork(); + return relayServiceClient.sendMessage(params); } - resolveDidcommMessage(params: ResolveDidcommMessageParams) { + async resolveDidcommMessage(params: ResolveDidcommMessageParams) { validation.resolveDidcommMessage(params); + + await ensureDockNetwork(); + return relayServiceClient.resolveDidcommMessage(params); } - getMessages(params: GetMessagesParams) { + async getMessages(params: GetMessagesParams) { validation.getMessages(params); + + await ensureDockNetwork(); + return relayServiceClient.getMessages(params); }