subscriber-connection.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.BlockConnectionSubscriber = void 0;
  4. const index_js_1 = require("../utils/index.js");
  5. /**
  6. * @TODO
  7. *
  8. * @_docloc: api/providers/abstract-provider
  9. */
  10. class BlockConnectionSubscriber {
  11. #provider;
  12. #blockNumber;
  13. #running;
  14. #filterId;
  15. constructor(provider) {
  16. this.#provider = provider;
  17. this.#blockNumber = -2;
  18. this.#running = false;
  19. this.#filterId = null;
  20. }
  21. start() {
  22. if (this.#running) {
  23. return;
  24. }
  25. this.#running = true;
  26. this.#filterId = this.#provider._subscribe(["newHeads"], (result) => {
  27. const blockNumber = (0, index_js_1.getNumber)(result.number);
  28. const initial = (this.#blockNumber === -2) ? blockNumber : (this.#blockNumber + 1);
  29. for (let b = initial; b <= blockNumber; b++) {
  30. this.#provider.emit("block", b);
  31. }
  32. this.#blockNumber = blockNumber;
  33. });
  34. }
  35. stop() {
  36. if (!this.#running) {
  37. return;
  38. }
  39. this.#running = false;
  40. if (this.#filterId != null) {
  41. this.#provider._unsubscribe(this.#filterId);
  42. this.#filterId = null;
  43. }
  44. }
  45. pause(dropWhilePaused) {
  46. if (dropWhilePaused) {
  47. this.#blockNumber = -2;
  48. }
  49. this.stop();
  50. }
  51. resume() {
  52. this.start();
  53. }
  54. }
  55. exports.BlockConnectionSubscriber = BlockConnectionSubscriber;
  56. //# sourceMappingURL=subscriber-connection.js.map