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

52 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-09-16 05:58:45 +03:00
import { getNumber } from "../utils/index.js";
2022-11-30 23:44:23 +03:00
/**
* @TODO
*
* @_docloc: api/providers/abstract-provider
*/
2022-09-05 23:57:11 +03:00
export class BlockConnectionSubscriber {
#provider;
#blockNumber;
2023-02-04 11:26:34 +03:00
#running;
2022-09-05 23:57:11 +03:00
#filterId;
constructor(provider) {
this.#provider = provider;
this.#blockNumber = -2;
2023-02-04 11:26:34 +03:00
this.#running = false;
2022-09-05 23:57:11 +03:00
this.#filterId = null;
}
start() {
2023-02-04 11:26:34 +03:00
if (this.#running) {
return;
}
this.#running = true;
2022-09-05 23:57:11 +03:00
this.#filterId = this.#provider._subscribe(["newHeads"], (result) => {
2022-09-16 05:58:45 +03:00
const blockNumber = getNumber(result.number);
2022-09-05 23:57:11 +03:00
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() {
2023-02-04 11:26:34 +03:00
if (!this.#running) {
return;
}
this.#running = false;
2022-09-05 23:57:11 +03:00
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