diff --git a/src/HavenoClient.test.ts b/src/HavenoClient.test.ts index 8e3304fd..1230961a 100644 --- a/src/HavenoClient.test.ts +++ b/src/HavenoClient.test.ts @@ -1255,6 +1255,7 @@ test("Can schedule offers with locked funds (CI)", async () => { const user3Config = {appName: user3.getAppName()}; await releaseHavenoProcess(user3); user3 = await initHaveno(user3Config); + ctx.maker = user3; // has offer offer = await user3.getMyOffer(offer.getId()); diff --git a/src/HavenoClient.ts b/src/HavenoClient.ts index bcdf8d20..3c93d7a4 100644 --- a/src/HavenoClient.ts +++ b/src/HavenoClient.ts @@ -79,7 +79,7 @@ export default class HavenoClient { * @param {string[]} cmd - command to start the process * @param {string} url - Haveno daemon url (must proxy to api port) * @param {boolean} enableLogging - specifies if logging is enabled or disabled at log level 3 - * @return {haveno} a client connected to the newly started Haveno process + * @return {HavenoClient} a client connected to the newly started Haveno process */ static async startProcess(havenoPath: string, cmd: string[], url: string, enableLogging: boolean): Promise { try { @@ -1326,33 +1326,31 @@ export default class HavenoClient { async _updateNotificationListenerRegistration(): Promise { try { const listening = this._notificationListeners.length > 0; - if (listening && this._notificationStream || !listening && !this._notificationStream) return; // no difference + if ((listening && this._notificationStream) || (!listening && !this._notificationStream)) return; // no difference if (listening) { - await new Promise((resolve) => { - - // send request to register client listener - this._notificationStream = this._notificationsClient.registerNotificationListener(new RegisterNotificationListenerRequest(), {password: this._password}) - .on('data', this._onNotification); - - // periodically send keep alive requests // TODO (woodser): better way to keep notification stream alive? - let firstRequest = true; - this._keepAliveLooper = new TaskLooper(async () => { - if (firstRequest) { - firstRequest = false; - return; - } - try { - await this._sendNotification(new NotificationMessage() - .setType(NotificationMessage.NotificationType.KEEP_ALIVE) - .setTimestamp(Date.now())); - } catch (err: any) { - HavenoUtils.log(0, "Error sending keep alive request to Haveno daemon: " + err.message); - } - }); - this._keepAliveLooper.start(this._keepAlivePeriodMs); - - setTimeout(resolve, 1000); // TODO: call returns before listener registered + + // send request to register client listener + this._notificationStream = this._notificationsClient.registerNotificationListener(new RegisterNotificationListenerRequest(), {password: this._password}) + .on('data', this._onNotification); + + // periodically send keep alive requests // TODO (woodser): better way to keep notification stream alive? + let firstRequest = true; + this._keepAliveLooper = new TaskLooper(async () => { + if (firstRequest) { + firstRequest = false; + return; + } + try { + await this._sendNotification(new NotificationMessage() + .setType(NotificationMessage.NotificationType.KEEP_ALIVE) + .setTimestamp(Date.now())); + } catch (err: any) { + HavenoUtils.log(0, "Error sending keep alive request to Haveno daemon " + this.getUrl() + ": " + err.message); + } }); + this._keepAliveLooper.start(this._keepAlivePeriodMs); + + await HavenoUtils.waitFor(1000); // TODO: call returns before listener registered } else { this._notificationStream!.removeListener('data', this._onNotification); this._keepAliveLooper.stop(); diff --git a/src/utils/HavenoUtils.ts b/src/utils/HavenoUtils.ts index 6efd8b05..4c73d174 100644 --- a/src/utils/HavenoUtils.ts +++ b/src/utils/HavenoUtils.ts @@ -130,4 +130,13 @@ export default class HavenoUtils { } throw new Error("PaymentAccountForm does not have field " + fieldId); } + + /** + * Wait for the duration. + * + * @param {number} durationMs - the duration to wait for in milliseconds + */ + static async waitFor(durationMs: number) { + return new Promise(function(resolve) { setTimeout(resolve, durationMs); }); + } }