refactor: split subscribe into syncToLatestBlock

This commit is contained in:
Derrick Hammer 2023-07-13 03:18:05 -04:00
parent 4836ddb32e
commit 11791ff08b
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 13 additions and 9 deletions

View File

@ -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<ExecutionInfo | null> {
await this._sync();
const update = await this.options.optimisticUpdateCallback();