refactor: split subscribe into syncToLatestBlock
This commit is contained in:
parent
4836ddb32e
commit
11791ff08b
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue