@metamask/polling-controller
Version:
Polling Controller is the base for controllers that polling by networkClientId
80 lines • 4.91 kB
JavaScript
;
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StaticIntervalPollingController = exports.StaticIntervalPollingControllerOnly = void 0;
const base_controller_1 = require("@metamask/base-controller");
const AbstractPollingController_1 = require("./AbstractPollingController.cjs");
/**
* StaticIntervalPollingControllerMixin
* A polling controller that polls on a static interval.
*
* @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 StaticIntervalPollingControllerMixin(Base) {
var _StaticIntervalPollingController_intervalIds, _StaticIntervalPollingController_intervalLength;
class StaticIntervalPollingController extends (0, AbstractPollingController_1.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 = (0, AbstractPollingController_1.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 {
}
const StaticIntervalPollingControllerOnly = () => StaticIntervalPollingControllerMixin(Empty);
exports.StaticIntervalPollingControllerOnly = StaticIntervalPollingControllerOnly;
const StaticIntervalPollingController = () => StaticIntervalPollingControllerMixin(base_controller_1.BaseController);
exports.StaticIntervalPollingController = StaticIntervalPollingController;
//# sourceMappingURL=StaticIntervalPollingController.cjs.map