UNPKG

@metamask/polling-controller

Version:

Polling Controller is the base for controllers that polling by networkClientId

73 lines 4.37 kB
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; 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); }; import { BaseController } from "@metamask/base-controller"; import { AbstractPollingControllerBaseMixin, getKey } from "./AbstractPollingController.mjs"; /** * StaticIntervalPollingControllerMixin * A polling controller that polls on a static interval. * * @param Base - The base class to mix onto. * @returns The composed class. */ function StaticIntervalPollingControllerMixin(Base) { var _StaticIntervalPollingController_intervalIds, _StaticIntervalPollingController_intervalLength; class StaticIntervalPollingController extends AbstractPollingControllerBaseMixin(Base) { constructor() { super(...arguments); _StaticIntervalPollingController_intervalIds.set(this, {}); _StaticIntervalPollingController_intervalLength.set(this, 1000); } setIntervalLength(intervalLength) { __classPrivateFieldSet(this, _StaticIntervalPollingController_intervalLength, intervalLength, "f"); } getIntervalLength() { return __classPrivateFieldGet(this, _StaticIntervalPollingController_intervalLength, "f"); } _startPolling(input) { if (!__classPrivateFieldGet(this, _StaticIntervalPollingController_intervalLength, "f")) { throw new Error('intervalLength must be defined and greater than 0'); } const key = getKey(input); const existingInterval = __classPrivateFieldGet(this, _StaticIntervalPollingController_intervalIds, "f")[key]; this._stopPollingByPollingTokenSetId(key); // eslint-disable-next-line no-multi-assign const intervalId = (__classPrivateFieldGet(this, _StaticIntervalPollingController_intervalIds, "f")[key] = setTimeout( // TODO: Either fix this lint violation or explain why it's necessary to ignore. // eslint-disable-next-line @typescript-eslint/no-misused-promises async () => { try { await this._executePoll(input); } catch (error) { console.error(error); } if (intervalId === __classPrivateFieldGet(this, _StaticIntervalPollingController_intervalIds, "f")[key]) { this._startPolling(input); } }, existingInterval ? __classPrivateFieldGet(this, _StaticIntervalPollingController_intervalLength, "f") : 0)); } _stopPollingByPollingTokenSetId(key) { const intervalId = __classPrivateFieldGet(this, _StaticIntervalPollingController_intervalIds, "f")[key]; if (intervalId) { clearTimeout(intervalId); delete __classPrivateFieldGet(this, _StaticIntervalPollingController_intervalIds, "f")[key]; } } } _StaticIntervalPollingController_intervalIds = new WeakMap(), _StaticIntervalPollingController_intervalLength = new WeakMap(); return StaticIntervalPollingController; } class Empty { } export const StaticIntervalPollingControllerOnly = () => StaticIntervalPollingControllerMixin(Empty); export const StaticIntervalPollingController = () => StaticIntervalPollingControllerMixin(BaseController); //# sourceMappingURL=StaticIntervalPollingController.mjs.map