From 11791ff08bf5570495d4d8125d5e0dfa1c0db058 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 13 Jul 2023 03:18:05 -0400 Subject: [PATCH] refactor: split subscribe into syncToLatestBlock --- src/baseClient.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/baseClient.ts b/src/baseClient.ts index a9c0449..3a82f0d 100644 --- a/src/baseClient.ts +++ b/src/baseClient.ts @@ -154,18 +154,22 @@ export default abstract class BaseClient { protected async subscribe(callback?: (ei: ExecutionInfo) => void) { setInterval(async () => { - try { - const ei = await this.getLatestExecution(); - if (ei && ei.blockHash !== this.latestBlockHash) { - this.latestBlockHash = ei.blockHash; - return await callback?.(ei); - } - } catch (e) { - console.error(e); - } + await this.syncToLatestBlock(callback); }, POLLING_DELAY); } + public async syncToLatestBlock(callback?: (ei: ExecutionInfo) => void) { + try { + const ei = await this.getLatestExecution(); + if (ei && ei.blockHash !== this.latestBlockHash) { + this.latestBlockHash = ei.blockHash; + return await callback?.(ei); + } + } catch (e) { + console.error(e); + } + } + protected async getLatestExecution(): Promise { await this._sync(); const update = await this.options.optimisticUpdateCallback();