UNPKG

@metamask/polling-controller

Version:

Polling Controller is the base for controllers that polling by networkClientId

70 lines 4.15 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BlockTrackerPollingController = exports.BlockTrackerPollingControllerOnly = void 0; const base_controller_1 = require("@metamask/base-controller"); const AbstractPollingController_1 = require("./AbstractPollingController.cjs"); /** * BlockTrackerPollingControllerMixin * A polling controller that polls using a block tracker. * * @param Base - The base class to mix onto. * @returns The composed class. */ // TODO: Either fix this lint violation or explain why it's necessary to ignore. // eslint-disable-next-line @typescript-eslint/naming-convention function BlockTrackerPollingControllerMixin(Base) { var _BlockTrackerPollingController_activeListeners; class BlockTrackerPollingController extends (0, AbstractPollingController_1.AbstractPollingControllerBaseMixin)(Base) { constructor() { super(...arguments); _BlockTrackerPollingController_activeListeners.set(this, {}); } _startPolling(input) { const key = (0, AbstractPollingController_1.getKey)(input); if (__classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key]) { return; } const networkClient = this._getNetworkClientById(input.networkClientId); if (networkClient) { const updateOnNewBlock = this._executePoll.bind(this, input); // TODO: Either fix this lint violation or explain why it's necessary to ignore. // eslint-disable-next-line @typescript-eslint/no-misused-promises networkClient.blockTracker.addListener('latest', updateOnNewBlock); __classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key] = updateOnNewBlock; } else { throw new Error( // TODO: Either fix this lint violation or explain why it's necessary to ignore. // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `Unable to retrieve blockTracker for networkClientId ${input.networkClientId}`); } } _stopPollingByPollingTokenSetId(key) { const { networkClientId } = JSON.parse(key); const networkClient = this._getNetworkClientById(networkClientId); if (networkClient && __classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key]) { const listener = __classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key]; if (listener) { // TODO: Either fix this lint violation or explain why it's necessary to ignore. // eslint-disable-next-line @typescript-eslint/no-misused-promises networkClient.blockTracker.removeListener('latest', listener); delete __classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key]; } } } } _BlockTrackerPollingController_activeListeners = new WeakMap(); return BlockTrackerPollingController; } class Empty { } const BlockTrackerPollingControllerOnly = () => BlockTrackerPollingControllerMixin(Empty); exports.BlockTrackerPollingControllerOnly = BlockTrackerPollingControllerOnly; const BlockTrackerPollingController = () => BlockTrackerPollingControllerMixin(base_controller_1.BaseController); exports.BlockTrackerPollingController = BlockTrackerPollingController; //# sourceMappingURL=BlockTrackerPollingController.cjs.map