From 208ca03e807b9515c81f357918678d39c3fdd809 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 11 Jul 2023 02:21:30 -0400 Subject: [PATCH] refactor: expose subscribe on the base client, with optional callback and call on child clients --- src/baseClient.ts | 15 +++++++++++++++ src/client/client.ts | 15 --------------- src/node/client.ts | 5 +++++ 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/baseClient.ts b/src/baseClient.ts index 153c57a..937b2af 100644 --- a/src/baseClient.ts +++ b/src/baseClient.ts @@ -85,6 +85,21 @@ export default abstract class BaseClient { this.syncMutex.release(); } + protected async subscribe(callback?: (ei: ExecutionInfo) => void) { + setInterval(async () => { + try { + await this._sync(); + const ei = await this.getLatestExecution(); + if (ei && ei.blockHash !== this.latestBlockHash) { + this.latestBlockHash = ei.blockHash; + return await callback?.(ei); + } + } catch (e) { + console.error(e); + } + }, POLLING_DELAY); + } + public get store(): IStore { return this.options.store as IStore; } diff --git a/src/client/client.ts b/src/client/client.ts index 2923a8d..73454be 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -101,21 +101,6 @@ export default class Client extends BaseClient { return this.getCommittee(currentPeriod, lastCommitteeHash); } - private async subscribe(callback: (ei: ExecutionInfo) => void) { - setInterval(async () => { - try { - await this._sync(); - const ei = await this.getLatestExecution(); - if (ei && ei.blockHash !== this.latestBlockHash) { - this.latestBlockHash = ei.blockHash; - return await callback(ei); - } - } catch (e) { - console.error(e); - } - }, POLLING_DELAY); - } - private async getCommittee( period: number, expectedCommitteeHash: Uint8Array | null, diff --git a/src/node/client.ts b/src/node/client.ts index d88d4c7..1e92914 100644 --- a/src/node/client.ts +++ b/src/node/client.ts @@ -28,6 +28,11 @@ interface Config extends BaseClientOptions { } export default class Client extends BaseClient { + async sync(): Promise { + await super.sync(); + + this.subscribe(); + } private beaconUrl: string; private blockCache = new NodeCache({ stdTTL: 60 * 60 * 12 }); private blockHashCache = new NodeCache({ stdTTL: 60 * 60 * 12 });