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 });