ethers.js/lib.esm/providers/subscriber-connection.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-09-05 23:57:11 +03:00
import { logger } from "../utils/logger.js";
export class BlockConnectionSubscriber {
#provider;
#blockNumber;
#filterId;
constructor(provider) {
this.#provider = provider;
this.#blockNumber = -2;
this.#filterId = null;
}
start() {
this.#filterId = this.#provider._subscribe(["newHeads"], (result) => {
const blockNumber = logger.getNumber(result.number);
const initial = (this.#blockNumber === -2) ? blockNumber : (this.#blockNumber + 1);
for (let b = initial; b <= blockNumber; b++) {
this.#provider.emit("block", b);
}
this.#blockNumber = blockNumber;
});
}
stop() {
if (this.#filterId != null) {
this.#provider._unsubscribe(this.#filterId);
this.#filterId = null;
}
}
pause(dropWhilePaused) {
if (dropWhilePaused) {
this.#blockNumber = -2;
}
this.stop();
}
resume() {
this.start();
}
}
//# sourceMappingURL=subscriber-connection.js.map