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) {
|
protected async subscribe(callback?: (ei: ExecutionInfo) => void) {
|
||||||
setInterval(async () => {
|
setInterval(async () => {
|
||||||
try {
|
await this.syncToLatestBlock(callback);
|
||||||
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);
|
}, 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> {
|
protected async getLatestExecution(): Promise<ExecutionInfo | null> {
|
||||||
await this._sync();
|
await this._sync();
|
||||||
const update = await this.options.optimisticUpdateCallback();
|
const update = await this.options.optimisticUpdateCallback();
|
||||||
|
|
Loading…
Reference in New Issue