@metamask/polling-controller
Version:
Polling Controller is the base for controllers that polling by networkClientId
65 lines • 3.77 kB
JavaScript
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";
/**
* 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 AbstractPollingControllerBaseMixin(Base) {
constructor() {
super(...arguments);
_BlockTrackerPollingController_activeListeners.set(this, {});
}
_startPolling(input) {
const key = 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 {
}
export const BlockTrackerPollingControllerOnly = () => BlockTrackerPollingControllerMixin(Empty);
export const BlockTrackerPollingController = () => BlockTrackerPollingControllerMixin(BaseController);
//# sourceMappingURL=BlockTrackerPollingController.mjs.map