@metamask/polling-controller 10.0.0 → 10.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/CHANGELOG.md +20 -1
  2. package/dist/AbstractPollingController.cjs +87 -0
  3. package/dist/AbstractPollingController.cjs.map +1 -0
  4. package/dist/{types/AbstractPollingController.d.ts → AbstractPollingController.d.cts} +6 -6
  5. package/dist/AbstractPollingController.d.cts.map +1 -0
  6. package/dist/AbstractPollingController.d.mts +22 -0
  7. package/dist/AbstractPollingController.d.mts.map +1 -0
  8. package/dist/AbstractPollingController.mjs +84 -7
  9. package/dist/AbstractPollingController.mjs.map +1 -1
  10. package/dist/BlockTrackerPollingController.cjs +69 -0
  11. package/dist/BlockTrackerPollingController.cjs.map +1 -0
  12. package/dist/{types/BlockTrackerPollingController.d.ts → BlockTrackerPollingController.d.cts} +14 -14
  13. package/dist/BlockTrackerPollingController.d.cts.map +1 -0
  14. package/dist/BlockTrackerPollingController.d.mts +47 -0
  15. package/dist/BlockTrackerPollingController.d.mts.map +1 -0
  16. package/dist/BlockTrackerPollingController.mjs +64 -10
  17. package/dist/BlockTrackerPollingController.mjs.map +1 -1
  18. package/dist/StaticIntervalPollingController.cjs +79 -0
  19. package/dist/StaticIntervalPollingController.cjs.map +1 -0
  20. package/dist/{types/StaticIntervalPollingController.d.ts → StaticIntervalPollingController.d.cts} +17 -22
  21. package/dist/StaticIntervalPollingController.d.cts.map +1 -0
  22. package/dist/StaticIntervalPollingController.d.mts +53 -0
  23. package/dist/StaticIntervalPollingController.d.mts.map +1 -0
  24. package/dist/StaticIntervalPollingController.mjs +74 -10
  25. package/dist/StaticIntervalPollingController.mjs.map +1 -1
  26. package/dist/index.cjs +12 -0
  27. package/dist/index.cjs.map +1 -0
  28. package/dist/index.d.cts +4 -0
  29. package/dist/index.d.cts.map +1 -0
  30. package/dist/index.d.mts +4 -0
  31. package/dist/index.d.mts.map +1 -0
  32. package/dist/index.mjs +2 -19
  33. package/dist/index.mjs.map +1 -1
  34. package/dist/types.cjs +3 -0
  35. package/dist/types.cjs.map +1 -0
  36. package/dist/{types/types.d.ts → types.d.cts} +3 -3
  37. package/dist/types.d.cts.map +1 -0
  38. package/dist/types.d.mts +22 -0
  39. package/dist/types.d.mts.map +1 -0
  40. package/dist/types.mjs +1 -0
  41. package/dist/types.mjs.map +1 -1
  42. package/package.json +15 -10
  43. package/dist/AbstractPollingController.js +0 -9
  44. package/dist/AbstractPollingController.js.map +0 -1
  45. package/dist/BlockTrackerPollingController.js +0 -12
  46. package/dist/BlockTrackerPollingController.js.map +0 -1
  47. package/dist/StaticIntervalPollingController.js +0 -12
  48. package/dist/StaticIntervalPollingController.js.map +0 -1
  49. package/dist/chunk-E2WT3D73.mjs +0 -94
  50. package/dist/chunk-E2WT3D73.mjs.map +0 -1
  51. package/dist/chunk-JCXUPRTT.js +0 -94
  52. package/dist/chunk-JCXUPRTT.js.map +0 -1
  53. package/dist/chunk-JVLLYBTK.js +0 -69
  54. package/dist/chunk-JVLLYBTK.js.map +0 -1
  55. package/dist/chunk-L32FFEZO.mjs +0 -71
  56. package/dist/chunk-L32FFEZO.mjs.map +0 -1
  57. package/dist/chunk-RD6EX3RW.js +0 -71
  58. package/dist/chunk-RD6EX3RW.js.map +0 -1
  59. package/dist/chunk-YETJAOVF.mjs +0 -69
  60. package/dist/chunk-YETJAOVF.mjs.map +0 -1
  61. package/dist/index.js +0 -20
  62. package/dist/index.js.map +0 -1
  63. package/dist/tsconfig.build.tsbuildinfo +0 -1
  64. package/dist/types/AbstractPollingController.d.ts.map +0 -1
  65. package/dist/types/BlockTrackerPollingController.d.ts.map +0 -1
  66. package/dist/types/StaticIntervalPollingController.d.ts.map +0 -1
  67. package/dist/types/index.d.ts +0 -4
  68. package/dist/types/index.d.ts.map +0 -1
  69. package/dist/types/types.d.ts.map +0 -1
  70. package/dist/types.js +0 -1
  71. package/dist/types.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -7,6 +7,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [10.0.1]
11
+
12
+ ### Fixed
13
+
14
+ - Produce and export ESM-compatible TypeScript type declaration files in addition to CommonJS-compatible declaration files ([#4648](https://github.com/MetaMask/core/pull/4648))
15
+ - Previously, this package shipped with only one variant of type declaration
16
+ files, and these files were only CommonJS-compatible, and the `exports`
17
+ field in `package.json` linked to these files. This is an anti-pattern and
18
+ was rightfully flagged by the
19
+ ["Are the Types Wrong?"](https://arethetypeswrong.github.io/) tool as
20
+ ["masquerading as CJS"](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseCJS.md).
21
+ All of the ATTW checks now pass.
22
+ - Remove chunk files ([#4648](https://github.com/MetaMask/core/pull/4648)).
23
+ - Previously, the build tool we used to generate JavaScript files extracted
24
+ common code to "chunk" files. While this was intended to make this package
25
+ more tree-shakeable, it also made debugging more difficult for our
26
+ development teams. These chunk files are no longer present.
27
+
10
28
  ## [10.0.0]
11
29
 
12
30
  ### Changed
@@ -169,7 +187,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
169
187
 
170
188
  - Initial release
171
189
 
172
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/polling-controller@10.0.0...HEAD
190
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/polling-controller@10.0.1...HEAD
191
+ [10.0.1]: https://github.com/MetaMask/core/compare/@metamask/polling-controller@10.0.0...@metamask/polling-controller@10.0.1
173
192
  [10.0.0]: https://github.com/MetaMask/core/compare/@metamask/polling-controller@9.0.1...@metamask/polling-controller@10.0.0
174
193
  [9.0.1]: https://github.com/MetaMask/core/compare/@metamask/polling-controller@9.0.0...@metamask/polling-controller@9.0.1
175
194
  [9.0.0]: https://github.com/MetaMask/core/compare/@metamask/polling-controller@8.0.0...@metamask/polling-controller@9.0.0
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ 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");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ var __importDefault = (this && this.__importDefault) || function (mod) {
8
+ return (mod && mod.__esModule) ? mod : { "default": mod };
9
+ };
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.AbstractPollingControllerBaseMixin = exports.getKey = void 0;
12
+ const fast_json_stable_stringify_1 = __importDefault(require("fast-json-stable-stringify"));
13
+ const uuid_1 = require("uuid");
14
+ const getKey = (networkClientId, options) => `${networkClientId}:${(0, fast_json_stable_stringify_1.default)(options)}`;
15
+ exports.getKey = getKey;
16
+ /**
17
+ * AbstractPollingControllerBaseMixin
18
+ *
19
+ * @param Base - The base class to mix onto.
20
+ * @returns The composed class.
21
+ */
22
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
23
+ // eslint-disable-next-line @typescript-eslint/naming-convention
24
+ function AbstractPollingControllerBaseMixin(Base) {
25
+ var _AbstractPollingControllerBase_pollingTokenSets, _AbstractPollingControllerBase_callbacks;
26
+ class AbstractPollingControllerBase extends Base {
27
+ constructor() {
28
+ super(...arguments);
29
+ _AbstractPollingControllerBase_pollingTokenSets.set(this, new Map());
30
+ _AbstractPollingControllerBase_callbacks.set(this, new Map());
31
+ }
32
+ startPollingByNetworkClientId(networkClientId, options = {}) {
33
+ const pollToken = (0, uuid_1.v4)();
34
+ const key = (0, exports.getKey)(networkClientId, options);
35
+ const pollingTokenSet = __classPrivateFieldGet(this, _AbstractPollingControllerBase_pollingTokenSets, "f").get(key) ?? new Set();
36
+ pollingTokenSet.add(pollToken);
37
+ __classPrivateFieldGet(this, _AbstractPollingControllerBase_pollingTokenSets, "f").set(key, pollingTokenSet);
38
+ if (pollingTokenSet.size === 1) {
39
+ this._startPollingByNetworkClientId(networkClientId, options);
40
+ }
41
+ return pollToken;
42
+ }
43
+ stopAllPolling() {
44
+ __classPrivateFieldGet(this, _AbstractPollingControllerBase_pollingTokenSets, "f").forEach((tokenSet, _key) => {
45
+ tokenSet.forEach((token) => {
46
+ this.stopPollingByPollingToken(token);
47
+ });
48
+ });
49
+ }
50
+ stopPollingByPollingToken(pollingToken) {
51
+ if (!pollingToken) {
52
+ throw new Error('pollingToken required');
53
+ }
54
+ let keyToDelete = null;
55
+ for (const [key, tokenSet] of __classPrivateFieldGet(this, _AbstractPollingControllerBase_pollingTokenSets, "f")) {
56
+ if (tokenSet.delete(pollingToken)) {
57
+ if (tokenSet.size === 0) {
58
+ keyToDelete = key;
59
+ }
60
+ break;
61
+ }
62
+ }
63
+ if (keyToDelete) {
64
+ this._stopPollingByPollingTokenSetId(keyToDelete);
65
+ __classPrivateFieldGet(this, _AbstractPollingControllerBase_pollingTokenSets, "f").delete(keyToDelete);
66
+ const callbacks = __classPrivateFieldGet(this, _AbstractPollingControllerBase_callbacks, "f").get(keyToDelete);
67
+ if (callbacks) {
68
+ for (const callback of callbacks) {
69
+ // eslint-disable-next-line n/callback-return
70
+ callback(keyToDelete);
71
+ }
72
+ callbacks.clear();
73
+ }
74
+ }
75
+ }
76
+ onPollingCompleteByNetworkClientId(networkClientId, callback, options = {}) {
77
+ const key = (0, exports.getKey)(networkClientId, options);
78
+ const callbacks = __classPrivateFieldGet(this, _AbstractPollingControllerBase_callbacks, "f").get(key) ?? new Set();
79
+ callbacks.add(callback);
80
+ __classPrivateFieldGet(this, _AbstractPollingControllerBase_callbacks, "f").set(key, callbacks);
81
+ }
82
+ }
83
+ _AbstractPollingControllerBase_pollingTokenSets = new WeakMap(), _AbstractPollingControllerBase_callbacks = new WeakMap();
84
+ return AbstractPollingControllerBase;
85
+ }
86
+ exports.AbstractPollingControllerBaseMixin = AbstractPollingControllerBaseMixin;
87
+ //# sourceMappingURL=AbstractPollingController.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AbstractPollingController.cjs","sourceRoot":"","sources":["../src/AbstractPollingController.ts"],"names":[],"mappings":"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","sourcesContent":["import type { NetworkClientId } from '@metamask/network-controller';\nimport type { Json } from '@metamask/utils';\nimport stringify from 'fast-json-stable-stringify';\nimport { v4 as random } from 'uuid';\n\nimport type {\n Constructor,\n PollingTokenSetId,\n IPollingController,\n} from './types';\n\nexport const getKey = (\n networkClientId: NetworkClientId,\n options: Json,\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n): PollingTokenSetId => `${networkClientId}:${stringify(options)}`;\n\n/**\n * AbstractPollingControllerBaseMixin\n *\n * @param Base - The base class to mix onto.\n * @returns The composed class.\n */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport function AbstractPollingControllerBaseMixin<TBase extends Constructor>(\n Base: TBase,\n) {\n abstract class AbstractPollingControllerBase\n extends Base\n implements IPollingController\n {\n readonly #pollingTokenSets: Map<PollingTokenSetId, Set<string>> = new Map();\n\n #callbacks: Map<\n PollingTokenSetId,\n Set<(PollingTokenSetId: PollingTokenSetId) => void>\n > = new Map();\n\n abstract _executePoll(\n networkClientId: NetworkClientId,\n options: Json,\n ): Promise<void>;\n\n abstract _startPollingByNetworkClientId(\n networkClientId: NetworkClientId,\n options: Json,\n ): void;\n\n abstract _stopPollingByPollingTokenSetId(key: PollingTokenSetId): void;\n\n startPollingByNetworkClientId(\n networkClientId: NetworkClientId,\n options: Json = {},\n ): string {\n const pollToken = random();\n const key = getKey(networkClientId, options);\n const pollingTokenSet =\n this.#pollingTokenSets.get(key) ?? new Set<string>();\n pollingTokenSet.add(pollToken);\n this.#pollingTokenSets.set(key, pollingTokenSet);\n\n if (pollingTokenSet.size === 1) {\n this._startPollingByNetworkClientId(networkClientId, options);\n }\n\n return pollToken;\n }\n\n stopAllPolling() {\n this.#pollingTokenSets.forEach((tokenSet, _key) => {\n tokenSet.forEach((token) => {\n this.stopPollingByPollingToken(token);\n });\n });\n }\n\n stopPollingByPollingToken(pollingToken: string) {\n if (!pollingToken) {\n throw new Error('pollingToken required');\n }\n\n let keyToDelete: PollingTokenSetId | null = null;\n for (const [key, tokenSet] of this.#pollingTokenSets) {\n if (tokenSet.delete(pollingToken)) {\n if (tokenSet.size === 0) {\n keyToDelete = key;\n }\n break;\n }\n }\n\n if (keyToDelete) {\n this._stopPollingByPollingTokenSetId(keyToDelete);\n this.#pollingTokenSets.delete(keyToDelete);\n const callbacks = this.#callbacks.get(keyToDelete);\n if (callbacks) {\n for (const callback of callbacks) {\n // eslint-disable-next-line n/callback-return\n callback(keyToDelete);\n }\n callbacks.clear();\n }\n }\n }\n\n onPollingCompleteByNetworkClientId(\n networkClientId: NetworkClientId,\n callback: (networkClientId: NetworkClientId) => void,\n options: Json = {},\n ) {\n const key = getKey(networkClientId, options);\n const callbacks = this.#callbacks.get(key) ?? new Set<typeof callback>();\n callbacks.add(callback);\n this.#callbacks.set(key, callbacks);\n }\n }\n return AbstractPollingControllerBase;\n}\n"]}
@@ -1,6 +1,6 @@
1
- import type { NetworkClientId } from '@metamask/network-controller';
2
- import type { Json } from '@metamask/utils';
3
- import type { Constructor, PollingTokenSetId } from './types';
1
+ import type { NetworkClientId } from "@metamask/network-controller";
2
+ import type { Json } from "@metamask/utils";
3
+ import type { Constructor, PollingTokenSetId } from "./types.cjs";
4
4
  export declare const getKey: (networkClientId: NetworkClientId, options: Json) => PollingTokenSetId;
5
5
  /**
6
6
  * AbstractPollingControllerBaseMixin
@@ -9,8 +9,8 @@ export declare const getKey: (networkClientId: NetworkClientId, options: Json) =
9
9
  * @returns The composed class.
10
10
  */
11
11
  export declare function AbstractPollingControllerBaseMixin<TBase extends Constructor>(Base: TBase): (abstract new (...args: any[]) => {
12
- readonly "__#103046@#pollingTokenSets": Map<PollingTokenSetId, Set<string>>;
13
- "__#103046@#callbacks": Map<`${string}:${string}`, Set<(PollingTokenSetId: `${string}:${string}`) => void>>;
12
+ readonly "__#786017@#pollingTokenSets": Map<PollingTokenSetId, Set<string>>;
13
+ "__#786017@#callbacks": Map<`${string}:${string}`, Set<(PollingTokenSetId: `${string}:${string}`) => void>>;
14
14
  _executePoll(networkClientId: NetworkClientId, options: Json): Promise<void>;
15
15
  _startPollingByNetworkClientId(networkClientId: NetworkClientId, options: Json): void;
16
16
  _stopPollingByPollingTokenSetId(key: PollingTokenSetId): void;
@@ -19,4 +19,4 @@ export declare function AbstractPollingControllerBaseMixin<TBase extends Constru
19
19
  stopPollingByPollingToken(pollingToken: string): void;
20
20
  onPollingCompleteByNetworkClientId(networkClientId: NetworkClientId, callback: (networkClientId: NetworkClientId) => void, options?: Json): void;
21
21
  }) & TBase;
22
- //# sourceMappingURL=AbstractPollingController.d.ts.map
22
+ //# sourceMappingURL=AbstractPollingController.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AbstractPollingController.d.cts","sourceRoot":"","sources":["../src/AbstractPollingController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,qCAAqC;AACpE,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAI5C,OAAO,KAAK,EACV,WAAW,EACX,iBAAiB,EAElB,oBAAgB;AAEjB,eAAO,MAAM,MAAM,oBACA,eAAe,WACvB,IAAI,KAGZ,iBAA+D,CAAC;AAEnE;;;;;GAKG;AAGH,wBAAgB,kCAAkC,CAAC,KAAK,SAAS,WAAW,EAC1E,IAAI,EAAE,KAAK;4CAMmB,IAAI,iBAAiB,EAAE,IAAI,MAAM,CAAC,CAAC;yGAIf,IAAI;kCAIjC,eAAe,WACvB,IAAI,GACZ,QAAQ,IAAI,CAAC;oDAGG,eAAe,WACvB,IAAI,GACZ,IAAI;yCAEuC,iBAAiB,GAAG,IAAI;mDAGnD,eAAe,YACvB,IAAI,GACZ,MAAM;;4CAuB+B,MAAM;wDA8B3B,eAAe,8BACJ,eAAe,KAAK,IAAI,YAC3C,IAAI;WASlB"}
@@ -0,0 +1,22 @@
1
+ import type { NetworkClientId } from "@metamask/network-controller";
2
+ import type { Json } from "@metamask/utils";
3
+ import type { Constructor, PollingTokenSetId } from "./types.mjs";
4
+ export declare const getKey: (networkClientId: NetworkClientId, options: Json) => PollingTokenSetId;
5
+ /**
6
+ * AbstractPollingControllerBaseMixin
7
+ *
8
+ * @param Base - The base class to mix onto.
9
+ * @returns The composed class.
10
+ */
11
+ export declare function AbstractPollingControllerBaseMixin<TBase extends Constructor>(Base: TBase): (abstract new (...args: any[]) => {
12
+ readonly "__#786017@#pollingTokenSets": Map<PollingTokenSetId, Set<string>>;
13
+ "__#786017@#callbacks": Map<`${string}:${string}`, Set<(PollingTokenSetId: `${string}:${string}`) => void>>;
14
+ _executePoll(networkClientId: NetworkClientId, options: Json): Promise<void>;
15
+ _startPollingByNetworkClientId(networkClientId: NetworkClientId, options: Json): void;
16
+ _stopPollingByPollingTokenSetId(key: PollingTokenSetId): void;
17
+ startPollingByNetworkClientId(networkClientId: NetworkClientId, options?: Json): string;
18
+ stopAllPolling(): void;
19
+ stopPollingByPollingToken(pollingToken: string): void;
20
+ onPollingCompleteByNetworkClientId(networkClientId: NetworkClientId, callback: (networkClientId: NetworkClientId) => void, options?: Json): void;
21
+ }) & TBase;
22
+ //# sourceMappingURL=AbstractPollingController.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AbstractPollingController.d.mts","sourceRoot":"","sources":["../src/AbstractPollingController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,qCAAqC;AACpE,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAI5C,OAAO,KAAK,EACV,WAAW,EACX,iBAAiB,EAElB,oBAAgB;AAEjB,eAAO,MAAM,MAAM,oBACA,eAAe,WACvB,IAAI,KAGZ,iBAA+D,CAAC;AAEnE;;;;;GAKG;AAGH,wBAAgB,kCAAkC,CAAC,KAAK,SAAS,WAAW,EAC1E,IAAI,EAAE,KAAK;4CAMmB,IAAI,iBAAiB,EAAE,IAAI,MAAM,CAAC,CAAC;yGAIf,IAAI;kCAIjC,eAAe,WACvB,IAAI,GACZ,QAAQ,IAAI,CAAC;oDAGG,eAAe,WACvB,IAAI,GACZ,IAAI;yCAEuC,iBAAiB,GAAG,IAAI;mDAGnD,eAAe,YACvB,IAAI,GACZ,MAAM;;4CAuB+B,MAAM;wDA8B3B,eAAe,8BACJ,eAAe,KAAK,IAAI,YAC3C,IAAI;WASlB"}
@@ -1,9 +1,86 @@
1
- import {
2
- AbstractPollingControllerBaseMixin,
3
- getKey
4
- } from "./chunk-E2WT3D73.mjs";
5
- export {
6
- AbstractPollingControllerBaseMixin,
7
- getKey
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ 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");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
8
5
  };
6
+ function $importDefault(module) {
7
+ if (module?.__esModule) {
8
+ return module.default;
9
+ }
10
+ return module;
11
+ }
12
+ import $stringify from "fast-json-stable-stringify";
13
+ const stringify = $importDefault($stringify);
14
+ import { v4 as random } from "uuid";
15
+ export const getKey = (networkClientId, options) => `${networkClientId}:${stringify(options)}`;
16
+ /**
17
+ * AbstractPollingControllerBaseMixin
18
+ *
19
+ * @param Base - The base class to mix onto.
20
+ * @returns The composed class.
21
+ */
22
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
23
+ // eslint-disable-next-line @typescript-eslint/naming-convention
24
+ export function AbstractPollingControllerBaseMixin(Base) {
25
+ var _AbstractPollingControllerBase_pollingTokenSets, _AbstractPollingControllerBase_callbacks;
26
+ class AbstractPollingControllerBase extends Base {
27
+ constructor() {
28
+ super(...arguments);
29
+ _AbstractPollingControllerBase_pollingTokenSets.set(this, new Map());
30
+ _AbstractPollingControllerBase_callbacks.set(this, new Map());
31
+ }
32
+ startPollingByNetworkClientId(networkClientId, options = {}) {
33
+ const pollToken = random();
34
+ const key = getKey(networkClientId, options);
35
+ const pollingTokenSet = __classPrivateFieldGet(this, _AbstractPollingControllerBase_pollingTokenSets, "f").get(key) ?? new Set();
36
+ pollingTokenSet.add(pollToken);
37
+ __classPrivateFieldGet(this, _AbstractPollingControllerBase_pollingTokenSets, "f").set(key, pollingTokenSet);
38
+ if (pollingTokenSet.size === 1) {
39
+ this._startPollingByNetworkClientId(networkClientId, options);
40
+ }
41
+ return pollToken;
42
+ }
43
+ stopAllPolling() {
44
+ __classPrivateFieldGet(this, _AbstractPollingControllerBase_pollingTokenSets, "f").forEach((tokenSet, _key) => {
45
+ tokenSet.forEach((token) => {
46
+ this.stopPollingByPollingToken(token);
47
+ });
48
+ });
49
+ }
50
+ stopPollingByPollingToken(pollingToken) {
51
+ if (!pollingToken) {
52
+ throw new Error('pollingToken required');
53
+ }
54
+ let keyToDelete = null;
55
+ for (const [key, tokenSet] of __classPrivateFieldGet(this, _AbstractPollingControllerBase_pollingTokenSets, "f")) {
56
+ if (tokenSet.delete(pollingToken)) {
57
+ if (tokenSet.size === 0) {
58
+ keyToDelete = key;
59
+ }
60
+ break;
61
+ }
62
+ }
63
+ if (keyToDelete) {
64
+ this._stopPollingByPollingTokenSetId(keyToDelete);
65
+ __classPrivateFieldGet(this, _AbstractPollingControllerBase_pollingTokenSets, "f").delete(keyToDelete);
66
+ const callbacks = __classPrivateFieldGet(this, _AbstractPollingControllerBase_callbacks, "f").get(keyToDelete);
67
+ if (callbacks) {
68
+ for (const callback of callbacks) {
69
+ // eslint-disable-next-line n/callback-return
70
+ callback(keyToDelete);
71
+ }
72
+ callbacks.clear();
73
+ }
74
+ }
75
+ }
76
+ onPollingCompleteByNetworkClientId(networkClientId, callback, options = {}) {
77
+ const key = getKey(networkClientId, options);
78
+ const callbacks = __classPrivateFieldGet(this, _AbstractPollingControllerBase_callbacks, "f").get(key) ?? new Set();
79
+ callbacks.add(callback);
80
+ __classPrivateFieldGet(this, _AbstractPollingControllerBase_callbacks, "f").set(key, callbacks);
81
+ }
82
+ }
83
+ _AbstractPollingControllerBase_pollingTokenSets = new WeakMap(), _AbstractPollingControllerBase_callbacks = new WeakMap();
84
+ return AbstractPollingControllerBase;
85
+ }
9
86
  //# sourceMappingURL=AbstractPollingController.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"file":"AbstractPollingController.mjs","sourceRoot":"","sources":["../src/AbstractPollingController.ts"],"names":[],"mappings":"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","sourcesContent":["import type { NetworkClientId } from '@metamask/network-controller';\nimport type { Json } from '@metamask/utils';\nimport stringify from 'fast-json-stable-stringify';\nimport { v4 as random } from 'uuid';\n\nimport type {\n Constructor,\n PollingTokenSetId,\n IPollingController,\n} from './types';\n\nexport const getKey = (\n networkClientId: NetworkClientId,\n options: Json,\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n): PollingTokenSetId => `${networkClientId}:${stringify(options)}`;\n\n/**\n * AbstractPollingControllerBaseMixin\n *\n * @param Base - The base class to mix onto.\n * @returns The composed class.\n */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport function AbstractPollingControllerBaseMixin<TBase extends Constructor>(\n Base: TBase,\n) {\n abstract class AbstractPollingControllerBase\n extends Base\n implements IPollingController\n {\n readonly #pollingTokenSets: Map<PollingTokenSetId, Set<string>> = new Map();\n\n #callbacks: Map<\n PollingTokenSetId,\n Set<(PollingTokenSetId: PollingTokenSetId) => void>\n > = new Map();\n\n abstract _executePoll(\n networkClientId: NetworkClientId,\n options: Json,\n ): Promise<void>;\n\n abstract _startPollingByNetworkClientId(\n networkClientId: NetworkClientId,\n options: Json,\n ): void;\n\n abstract _stopPollingByPollingTokenSetId(key: PollingTokenSetId): void;\n\n startPollingByNetworkClientId(\n networkClientId: NetworkClientId,\n options: Json = {},\n ): string {\n const pollToken = random();\n const key = getKey(networkClientId, options);\n const pollingTokenSet =\n this.#pollingTokenSets.get(key) ?? new Set<string>();\n pollingTokenSet.add(pollToken);\n this.#pollingTokenSets.set(key, pollingTokenSet);\n\n if (pollingTokenSet.size === 1) {\n this._startPollingByNetworkClientId(networkClientId, options);\n }\n\n return pollToken;\n }\n\n stopAllPolling() {\n this.#pollingTokenSets.forEach((tokenSet, _key) => {\n tokenSet.forEach((token) => {\n this.stopPollingByPollingToken(token);\n });\n });\n }\n\n stopPollingByPollingToken(pollingToken: string) {\n if (!pollingToken) {\n throw new Error('pollingToken required');\n }\n\n let keyToDelete: PollingTokenSetId | null = null;\n for (const [key, tokenSet] of this.#pollingTokenSets) {\n if (tokenSet.delete(pollingToken)) {\n if (tokenSet.size === 0) {\n keyToDelete = key;\n }\n break;\n }\n }\n\n if (keyToDelete) {\n this._stopPollingByPollingTokenSetId(keyToDelete);\n this.#pollingTokenSets.delete(keyToDelete);\n const callbacks = this.#callbacks.get(keyToDelete);\n if (callbacks) {\n for (const callback of callbacks) {\n // eslint-disable-next-line n/callback-return\n callback(keyToDelete);\n }\n callbacks.clear();\n }\n }\n }\n\n onPollingCompleteByNetworkClientId(\n networkClientId: NetworkClientId,\n callback: (networkClientId: NetworkClientId) => void,\n options: Json = {},\n ) {\n const key = getKey(networkClientId, options);\n const callbacks = this.#callbacks.get(key) ?? new Set<typeof callback>();\n callbacks.add(callback);\n this.#callbacks.set(key, callbacks);\n }\n }\n return AbstractPollingControllerBase;\n}\n"]}
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ 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");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.BlockTrackerPollingControllerV1 = exports.BlockTrackerPollingController = exports.BlockTrackerPollingControllerOnly = void 0;
9
+ const base_controller_1 = require("@metamask/base-controller");
10
+ const AbstractPollingController_1 = require("./AbstractPollingController.cjs");
11
+ /**
12
+ * BlockTrackerPollingControllerMixin
13
+ * A polling controller that polls using a block tracker.
14
+ *
15
+ * @param Base - The base class to mix onto.
16
+ * @returns The composed class.
17
+ */
18
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
19
+ // eslint-disable-next-line @typescript-eslint/naming-convention
20
+ function BlockTrackerPollingControllerMixin(Base) {
21
+ var _BlockTrackerPollingController_activeListeners;
22
+ class BlockTrackerPollingController extends (0, AbstractPollingController_1.AbstractPollingControllerBaseMixin)(Base) {
23
+ constructor() {
24
+ super(...arguments);
25
+ _BlockTrackerPollingController_activeListeners.set(this, {});
26
+ }
27
+ _startPollingByNetworkClientId(networkClientId, options) {
28
+ const key = (0, AbstractPollingController_1.getKey)(networkClientId, options);
29
+ if (__classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key]) {
30
+ return;
31
+ }
32
+ const networkClient = this._getNetworkClientById(networkClientId);
33
+ if (networkClient) {
34
+ const updateOnNewBlock = this._executePoll.bind(this, networkClientId, options);
35
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
36
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
37
+ networkClient.blockTracker.addListener('latest', updateOnNewBlock);
38
+ __classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key] = updateOnNewBlock;
39
+ }
40
+ else {
41
+ throw new Error(
42
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
43
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
44
+ `Unable to retrieve blockTracker for networkClientId ${networkClientId}`);
45
+ }
46
+ }
47
+ _stopPollingByPollingTokenSetId(key) {
48
+ const [networkClientId] = key.split(':');
49
+ const networkClient = this._getNetworkClientById(networkClientId);
50
+ if (networkClient && __classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key]) {
51
+ const listener = __classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key];
52
+ if (listener) {
53
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
54
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
55
+ networkClient.blockTracker.removeListener('latest', listener);
56
+ delete __classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key];
57
+ }
58
+ }
59
+ }
60
+ }
61
+ _BlockTrackerPollingController_activeListeners = new WeakMap();
62
+ return BlockTrackerPollingController;
63
+ }
64
+ class Empty {
65
+ }
66
+ exports.BlockTrackerPollingControllerOnly = BlockTrackerPollingControllerMixin(Empty);
67
+ exports.BlockTrackerPollingController = BlockTrackerPollingControllerMixin(base_controller_1.BaseController);
68
+ exports.BlockTrackerPollingControllerV1 = BlockTrackerPollingControllerMixin(base_controller_1.BaseControllerV1);
69
+ //# sourceMappingURL=BlockTrackerPollingController.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BlockTrackerPollingController.cjs","sourceRoot":"","sources":["../src/BlockTrackerPollingController.ts"],"names":[],"mappings":";;;;;;;;AAAA,+DAA6E;AAO7E,+EAGqC;AAGrC;;;;;;GAMG;AACH,gFAAgF;AAChF,gEAAgE;AAChE,SAAS,kCAAkC,CACzC,IAAW;;IAEX,MAAe,6BAA8B,SAAQ,IAAA,8DAAkC,EACrF,IAAI,CACL;QAFD;;YAGE,yDAAqE,EAAE,EAAC;QAoD1E,CAAC;QA9CC,8BAA8B,CAC5B,eAAgC,EAChC,OAAa;YAEb,MAAM,GAAG,GAAG,IAAA,kCAAM,EAAC,eAAe,EAAE,OAAO,CAAC,CAAC;YAE7C,IAAI,uBAAA,IAAI,sDAAiB,CAAC,GAAG,CAAC,EAAE;gBAC9B,OAAO;aACR;YAED,MAAM,aAAa,GAAG,IAAI,CAAC,qBAAqB,CAAC,eAAe,CAAC,CAAC;YAClE,IAAI,aAAa,EAAE;gBACjB,MAAM,gBAAgB,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAC7C,IAAI,EACJ,eAAe,EACf,OAAO,CACR,CAAC;gBACF,gFAAgF;gBAChF,kEAAkE;gBAClE,aAAa,CAAC,YAAY,CAAC,WAAW,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC;gBACnE,uBAAA,IAAI,sDAAiB,CAAC,GAAG,CAAC,GAAG,gBAAgB,CAAC;aAC/C;iBAAM;gBACL,MAAM,IAAI,KAAK;gBACb,gFAAgF;gBAChF,4EAA4E;gBAC5E,uDAAuD,eAAe,EAAE,CACzE,CAAC;aACH;QACH,CAAC;QAED,+BAA+B,CAAC,GAAsB;YACpD,MAAM,CAAC,eAAe,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACzC,MAAM,aAAa,GAAG,IAAI,CAAC,qBAAqB,CAC9C,eAAkC,CACnC,CAAC;YAEF,IAAI,aAAa,IAAI,uBAAA,IAAI,sDAAiB,CAAC,GAAG,CAAC,EAAE;gBAC/C,MAAM,QAAQ,GAAG,uBAAA,IAAI,sDAAiB,CAAC,GAAG,CAAC,CAAC;gBAC5C,IAAI,QAAQ,EAAE;oBACZ,gFAAgF;oBAChF,kEAAkE;oBAClE,aAAa,CAAC,YAAY,CAAC,cAAc,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;oBAC9D,OAAO,uBAAA,IAAI,sDAAiB,CAAC,GAAG,CAAC,CAAC;iBACnC;aACF;QACH,CAAC;KACF;;IAED,OAAO,6BAA6B,CAAC;AACvC,CAAC;AAED,MAAM,KAAK;CAAG;AAED,QAAA,iCAAiC,GAC5C,kCAAkC,CAAC,KAAK,CAAC,CAAC;AAC/B,QAAA,6BAA6B,GACxC,kCAAkC,CAAC,gCAAc,CAAC,CAAC;AACxC,QAAA,+BAA+B,GAC1C,kCAAkC,CAAC,kCAAgB,CAAC,CAAC","sourcesContent":["import { BaseController, BaseControllerV1 } from '@metamask/base-controller';\nimport type {\n NetworkClientId,\n NetworkClient,\n} from '@metamask/network-controller';\nimport type { Json } from '@metamask/utils';\n\nimport {\n AbstractPollingControllerBaseMixin,\n getKey,\n} from './AbstractPollingController';\nimport type { Constructor, PollingTokenSetId } from './types';\n\n/**\n * BlockTrackerPollingControllerMixin\n * A polling controller that polls using a block tracker.\n *\n * @param Base - The base class to mix onto.\n * @returns The composed class.\n */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nfunction BlockTrackerPollingControllerMixin<TBase extends Constructor>(\n Base: TBase,\n) {\n abstract class BlockTrackerPollingController extends AbstractPollingControllerBaseMixin(\n Base,\n ) {\n #activeListeners: Record<string, (options: Json) => Promise<void>> = {};\n\n abstract _getNetworkClientById(\n networkClientId: NetworkClientId,\n ): NetworkClient | undefined;\n\n _startPollingByNetworkClientId(\n networkClientId: NetworkClientId,\n options: Json,\n ) {\n const key = getKey(networkClientId, options);\n\n if (this.#activeListeners[key]) {\n return;\n }\n\n const networkClient = this._getNetworkClientById(networkClientId);\n if (networkClient) {\n const updateOnNewBlock = this._executePoll.bind(\n this,\n networkClientId,\n options,\n );\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n networkClient.blockTracker.addListener('latest', updateOnNewBlock);\n this.#activeListeners[key] = updateOnNewBlock;\n } else {\n throw new Error(\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `Unable to retrieve blockTracker for networkClientId ${networkClientId}`,\n );\n }\n }\n\n _stopPollingByPollingTokenSetId(key: PollingTokenSetId) {\n const [networkClientId] = key.split(':');\n const networkClient = this._getNetworkClientById(\n networkClientId as NetworkClientId,\n );\n\n if (networkClient && this.#activeListeners[key]) {\n const listener = this.#activeListeners[key];\n if (listener) {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n networkClient.blockTracker.removeListener('latest', listener);\n delete this.#activeListeners[key];\n }\n }\n }\n }\n\n return BlockTrackerPollingController;\n}\n\nclass Empty {}\n\nexport const BlockTrackerPollingControllerOnly =\n BlockTrackerPollingControllerMixin(Empty);\nexport const BlockTrackerPollingController =\n BlockTrackerPollingControllerMixin(BaseController);\nexport const BlockTrackerPollingControllerV1 =\n BlockTrackerPollingControllerMixin(BaseControllerV1);\n"]}
@@ -1,16 +1,16 @@
1
- import { BaseController, BaseControllerV1 } from '@metamask/base-controller';
2
- import type { NetworkClientId, NetworkClient } from '@metamask/network-controller';
3
- import type { Json } from '@metamask/utils';
4
- import type { PollingTokenSetId } from './types';
1
+ import { BaseController, BaseControllerV1 } from "@metamask/base-controller";
2
+ import type { NetworkClientId, NetworkClient } from "@metamask/network-controller";
3
+ import type { Json } from "@metamask/utils";
4
+ import type { PollingTokenSetId } from "./types.cjs";
5
5
  declare class Empty {
6
6
  }
7
7
  export declare const BlockTrackerPollingControllerOnly: (abstract new (...args: any[]) => {
8
- "__#103047@#activeListeners": Record<string, (options: Json) => Promise<void>>;
8
+ "__#786018@#activeListeners": Record<string, (options: Json) => Promise<void>>;
9
9
  _getNetworkClientById(networkClientId: NetworkClientId): NetworkClient | undefined;
10
10
  _startPollingByNetworkClientId(networkClientId: NetworkClientId, options: Json): void;
11
11
  _stopPollingByPollingTokenSetId(key: PollingTokenSetId): void;
12
- readonly "__#103046@#pollingTokenSets": Map<`${string}:${string}`, Set<string>>;
13
- "__#103046@#callbacks": Map<`${string}:${string}`, Set<(PollingTokenSetId: `${string}:${string}`) => void>>;
12
+ readonly "__#786017@#pollingTokenSets": Map<`${string}:${string}`, Set<string>>;
13
+ "__#786017@#callbacks": Map<`${string}:${string}`, Set<(PollingTokenSetId: `${string}:${string}`) => void>>;
14
14
  _executePoll(networkClientId: string, options: Json): Promise<void>;
15
15
  startPollingByNetworkClientId(networkClientId: string, options?: Json): string;
16
16
  stopAllPolling(): void;
@@ -18,12 +18,12 @@ export declare const BlockTrackerPollingControllerOnly: (abstract new (...args:
18
18
  onPollingCompleteByNetworkClientId(networkClientId: string, callback: (networkClientId: string) => void, options?: Json): void;
19
19
  }) & typeof Empty;
20
20
  export declare const BlockTrackerPollingController: (abstract new (...args: any[]) => {
21
- "__#103047@#activeListeners": Record<string, (options: Json) => Promise<void>>;
21
+ "__#786018@#activeListeners": Record<string, (options: Json) => Promise<void>>;
22
22
  _getNetworkClientById(networkClientId: NetworkClientId): NetworkClient | undefined;
23
23
  _startPollingByNetworkClientId(networkClientId: NetworkClientId, options: Json): void;
24
24
  _stopPollingByPollingTokenSetId(key: PollingTokenSetId): void;
25
- readonly "__#103046@#pollingTokenSets": Map<`${string}:${string}`, Set<string>>;
26
- "__#103046@#callbacks": Map<`${string}:${string}`, Set<(PollingTokenSetId: `${string}:${string}`) => void>>;
25
+ readonly "__#786017@#pollingTokenSets": Map<`${string}:${string}`, Set<string>>;
26
+ "__#786017@#callbacks": Map<`${string}:${string}`, Set<(PollingTokenSetId: `${string}:${string}`) => void>>;
27
27
  _executePoll(networkClientId: string, options: Json): Promise<void>;
28
28
  startPollingByNetworkClientId(networkClientId: string, options?: Json): string;
29
29
  stopAllPolling(): void;
@@ -31,12 +31,12 @@ export declare const BlockTrackerPollingController: (abstract new (...args: any[
31
31
  onPollingCompleteByNetworkClientId(networkClientId: string, callback: (networkClientId: string) => void, options?: Json): void;
32
32
  }) & typeof BaseController;
33
33
  export declare const BlockTrackerPollingControllerV1: (abstract new (...args: any[]) => {
34
- "__#103047@#activeListeners": Record<string, (options: Json) => Promise<void>>;
34
+ "__#786018@#activeListeners": Record<string, (options: Json) => Promise<void>>;
35
35
  _getNetworkClientById(networkClientId: NetworkClientId): NetworkClient | undefined;
36
36
  _startPollingByNetworkClientId(networkClientId: NetworkClientId, options: Json): void;
37
37
  _stopPollingByPollingTokenSetId(key: PollingTokenSetId): void;
38
- readonly "__#103046@#pollingTokenSets": Map<`${string}:${string}`, Set<string>>;
39
- "__#103046@#callbacks": Map<`${string}:${string}`, Set<(PollingTokenSetId: `${string}:${string}`) => void>>;
38
+ readonly "__#786017@#pollingTokenSets": Map<`${string}:${string}`, Set<string>>;
39
+ "__#786017@#callbacks": Map<`${string}:${string}`, Set<(PollingTokenSetId: `${string}:${string}`) => void>>;
40
40
  _executePoll(networkClientId: string, options: Json): Promise<void>;
41
41
  startPollingByNetworkClientId(networkClientId: string, options?: Json): string;
42
42
  stopAllPolling(): void;
@@ -44,4 +44,4 @@ export declare const BlockTrackerPollingControllerV1: (abstract new (...args: an
44
44
  onPollingCompleteByNetworkClientId(networkClientId: string, callback: (networkClientId: string) => void, options?: Json): void;
45
45
  }) & typeof BaseControllerV1;
46
46
  export {};
47
- //# sourceMappingURL=BlockTrackerPollingController.d.ts.map
47
+ //# sourceMappingURL=BlockTrackerPollingController.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BlockTrackerPollingController.d.cts","sourceRoot":"","sources":["../src/BlockTrackerPollingController.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,kCAAkC;AAC7E,OAAO,KAAK,EACV,eAAe,EACf,aAAa,EACd,qCAAqC;AACtC,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAM5C,OAAO,KAAK,EAAe,iBAAiB,EAAE,oBAAgB;AA0E9D,cAAM,KAAK;CAAG;AAEd,eAAO,MAAM,iCAAiC;2DA3DC,IAAI,KAAK,QAAQ,IAAI,CAAC;2CAG9C,eAAe,GAC/B,aAAa,GAAG,SAAS;oDAGT,eAAe,WACvB,IAAI;yCA4BsB,iBAAiB;;;;;;;;iBAwBf,CAAC;AAC5C,eAAO,MAAM,6BAA6B;2DA7DK,IAAI,KAAK,QAAQ,IAAI,CAAC;2CAG9C,eAAe,GAC/B,aAAa,GAAG,SAAS;oDAGT,eAAe,WACvB,IAAI;yCA4BsB,iBAAiB;;;;;;;;0BA0BN,CAAC;AACrD,eAAO,MAAM,+BAA+B;2DA/DG,IAAI,KAAK,QAAQ,IAAI,CAAC;2CAG9C,eAAe,GAC/B,aAAa,GAAG,SAAS;oDAGT,eAAe,WACvB,IAAI;yCA4BsB,iBAAiB;;;;;;;;4BA4BJ,CAAC"}
@@ -0,0 +1,47 @@
1
+ import { BaseController, BaseControllerV1 } from "@metamask/base-controller";
2
+ import type { NetworkClientId, NetworkClient } from "@metamask/network-controller";
3
+ import type { Json } from "@metamask/utils";
4
+ import type { PollingTokenSetId } from "./types.mjs";
5
+ declare class Empty {
6
+ }
7
+ export declare const BlockTrackerPollingControllerOnly: (abstract new (...args: any[]) => {
8
+ "__#786018@#activeListeners": Record<string, (options: Json) => Promise<void>>;
9
+ _getNetworkClientById(networkClientId: NetworkClientId): NetworkClient | undefined;
10
+ _startPollingByNetworkClientId(networkClientId: NetworkClientId, options: Json): void;
11
+ _stopPollingByPollingTokenSetId(key: PollingTokenSetId): void;
12
+ readonly "__#786017@#pollingTokenSets": Map<`${string}:${string}`, Set<string>>;
13
+ "__#786017@#callbacks": Map<`${string}:${string}`, Set<(PollingTokenSetId: `${string}:${string}`) => void>>;
14
+ _executePoll(networkClientId: string, options: Json): Promise<void>;
15
+ startPollingByNetworkClientId(networkClientId: string, options?: Json): string;
16
+ stopAllPolling(): void;
17
+ stopPollingByPollingToken(pollingToken: string): void;
18
+ onPollingCompleteByNetworkClientId(networkClientId: string, callback: (networkClientId: string) => void, options?: Json): void;
19
+ }) & typeof Empty;
20
+ export declare const BlockTrackerPollingController: (abstract new (...args: any[]) => {
21
+ "__#786018@#activeListeners": Record<string, (options: Json) => Promise<void>>;
22
+ _getNetworkClientById(networkClientId: NetworkClientId): NetworkClient | undefined;
23
+ _startPollingByNetworkClientId(networkClientId: NetworkClientId, options: Json): void;
24
+ _stopPollingByPollingTokenSetId(key: PollingTokenSetId): void;
25
+ readonly "__#786017@#pollingTokenSets": Map<`${string}:${string}`, Set<string>>;
26
+ "__#786017@#callbacks": Map<`${string}:${string}`, Set<(PollingTokenSetId: `${string}:${string}`) => void>>;
27
+ _executePoll(networkClientId: string, options: Json): Promise<void>;
28
+ startPollingByNetworkClientId(networkClientId: string, options?: Json): string;
29
+ stopAllPolling(): void;
30
+ stopPollingByPollingToken(pollingToken: string): void;
31
+ onPollingCompleteByNetworkClientId(networkClientId: string, callback: (networkClientId: string) => void, options?: Json): void;
32
+ }) & typeof BaseController;
33
+ export declare const BlockTrackerPollingControllerV1: (abstract new (...args: any[]) => {
34
+ "__#786018@#activeListeners": Record<string, (options: Json) => Promise<void>>;
35
+ _getNetworkClientById(networkClientId: NetworkClientId): NetworkClient | undefined;
36
+ _startPollingByNetworkClientId(networkClientId: NetworkClientId, options: Json): void;
37
+ _stopPollingByPollingTokenSetId(key: PollingTokenSetId): void;
38
+ readonly "__#786017@#pollingTokenSets": Map<`${string}:${string}`, Set<string>>;
39
+ "__#786017@#callbacks": Map<`${string}:${string}`, Set<(PollingTokenSetId: `${string}:${string}`) => void>>;
40
+ _executePoll(networkClientId: string, options: Json): Promise<void>;
41
+ startPollingByNetworkClientId(networkClientId: string, options?: Json): string;
42
+ stopAllPolling(): void;
43
+ stopPollingByPollingToken(pollingToken: string): void;
44
+ onPollingCompleteByNetworkClientId(networkClientId: string, callback: (networkClientId: string) => void, options?: Json): void;
45
+ }) & typeof BaseControllerV1;
46
+ export {};
47
+ //# sourceMappingURL=BlockTrackerPollingController.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BlockTrackerPollingController.d.mts","sourceRoot":"","sources":["../src/BlockTrackerPollingController.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,kCAAkC;AAC7E,OAAO,KAAK,EACV,eAAe,EACf,aAAa,EACd,qCAAqC;AACtC,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAM5C,OAAO,KAAK,EAAe,iBAAiB,EAAE,oBAAgB;AA0E9D,cAAM,KAAK;CAAG;AAEd,eAAO,MAAM,iCAAiC;2DA3DC,IAAI,KAAK,QAAQ,IAAI,CAAC;2CAG9C,eAAe,GAC/B,aAAa,GAAG,SAAS;oDAGT,eAAe,WACvB,IAAI;yCA4BsB,iBAAiB;;;;;;;;iBAwBf,CAAC;AAC5C,eAAO,MAAM,6BAA6B;2DA7DK,IAAI,KAAK,QAAQ,IAAI,CAAC;2CAG9C,eAAe,GAC/B,aAAa,GAAG,SAAS;oDAGT,eAAe,WACvB,IAAI;yCA4BsB,iBAAiB;;;;;;;;0BA0BN,CAAC;AACrD,eAAO,MAAM,+BAA+B;2DA/DG,IAAI,KAAK,QAAQ,IAAI,CAAC;2CAG9C,eAAe,GAC/B,aAAa,GAAG,SAAS;oDAGT,eAAe,WACvB,IAAI;yCA4BsB,iBAAiB;;;;;;;;4BA4BJ,CAAC"}
@@ -1,12 +1,66 @@
1
- import {
2
- BlockTrackerPollingController,
3
- BlockTrackerPollingControllerOnly,
4
- BlockTrackerPollingControllerV1
5
- } from "./chunk-YETJAOVF.mjs";
6
- import "./chunk-E2WT3D73.mjs";
7
- export {
8
- BlockTrackerPollingController,
9
- BlockTrackerPollingControllerOnly,
10
- BlockTrackerPollingControllerV1
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ 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");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
5
  };
6
+ import { BaseController, BaseControllerV1 } from "@metamask/base-controller";
7
+ import { AbstractPollingControllerBaseMixin, getKey } from "./AbstractPollingController.mjs";
8
+ /**
9
+ * BlockTrackerPollingControllerMixin
10
+ * A polling controller that polls using a block tracker.
11
+ *
12
+ * @param Base - The base class to mix onto.
13
+ * @returns The composed class.
14
+ */
15
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
16
+ // eslint-disable-next-line @typescript-eslint/naming-convention
17
+ function BlockTrackerPollingControllerMixin(Base) {
18
+ var _BlockTrackerPollingController_activeListeners;
19
+ class BlockTrackerPollingController extends AbstractPollingControllerBaseMixin(Base) {
20
+ constructor() {
21
+ super(...arguments);
22
+ _BlockTrackerPollingController_activeListeners.set(this, {});
23
+ }
24
+ _startPollingByNetworkClientId(networkClientId, options) {
25
+ const key = getKey(networkClientId, options);
26
+ if (__classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key]) {
27
+ return;
28
+ }
29
+ const networkClient = this._getNetworkClientById(networkClientId);
30
+ if (networkClient) {
31
+ const updateOnNewBlock = this._executePoll.bind(this, networkClientId, options);
32
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
33
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
34
+ networkClient.blockTracker.addListener('latest', updateOnNewBlock);
35
+ __classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key] = updateOnNewBlock;
36
+ }
37
+ else {
38
+ throw new Error(
39
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
40
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
41
+ `Unable to retrieve blockTracker for networkClientId ${networkClientId}`);
42
+ }
43
+ }
44
+ _stopPollingByPollingTokenSetId(key) {
45
+ const [networkClientId] = key.split(':');
46
+ const networkClient = this._getNetworkClientById(networkClientId);
47
+ if (networkClient && __classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key]) {
48
+ const listener = __classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key];
49
+ if (listener) {
50
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
51
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
52
+ networkClient.blockTracker.removeListener('latest', listener);
53
+ delete __classPrivateFieldGet(this, _BlockTrackerPollingController_activeListeners, "f")[key];
54
+ }
55
+ }
56
+ }
57
+ }
58
+ _BlockTrackerPollingController_activeListeners = new WeakMap();
59
+ return BlockTrackerPollingController;
60
+ }
61
+ class Empty {
62
+ }
63
+ export const BlockTrackerPollingControllerOnly = BlockTrackerPollingControllerMixin(Empty);
64
+ export const BlockTrackerPollingController = BlockTrackerPollingControllerMixin(BaseController);
65
+ export const BlockTrackerPollingControllerV1 = BlockTrackerPollingControllerMixin(BaseControllerV1);
12
66
  //# sourceMappingURL=BlockTrackerPollingController.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"file":"BlockTrackerPollingController.mjs","sourceRoot":"","sources":["../src/BlockTrackerPollingController.ts"],"names":[],"mappings":";;;;;AAAA,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,kCAAkC;AAO7E,OAAO,EACL,kCAAkC,EAClC,MAAM,EACP,wCAAoC;AAGrC;;;;;;GAMG;AACH,gFAAgF;AAChF,gEAAgE;AAChE,SAAS,kCAAkC,CACzC,IAAW;;IAEX,MAAe,6BAA8B,SAAQ,kCAAkC,CACrF,IAAI,CACL;QAFD;;YAGE,yDAAqE,EAAE,EAAC;QAoD1E,CAAC;QA9CC,8BAA8B,CAC5B,eAAgC,EAChC,OAAa;YAEb,MAAM,GAAG,GAAG,MAAM,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC;YAE7C,IAAI,uBAAA,IAAI,sDAAiB,CAAC,GAAG,CAAC,EAAE;gBAC9B,OAAO;aACR;YAED,MAAM,aAAa,GAAG,IAAI,CAAC,qBAAqB,CAAC,eAAe,CAAC,CAAC;YAClE,IAAI,aAAa,EAAE;gBACjB,MAAM,gBAAgB,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAC7C,IAAI,EACJ,eAAe,EACf,OAAO,CACR,CAAC;gBACF,gFAAgF;gBAChF,kEAAkE;gBAClE,aAAa,CAAC,YAAY,CAAC,WAAW,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC;gBACnE,uBAAA,IAAI,sDAAiB,CAAC,GAAG,CAAC,GAAG,gBAAgB,CAAC;aAC/C;iBAAM;gBACL,MAAM,IAAI,KAAK;gBACb,gFAAgF;gBAChF,4EAA4E;gBAC5E,uDAAuD,eAAe,EAAE,CACzE,CAAC;aACH;QACH,CAAC;QAED,+BAA+B,CAAC,GAAsB;YACpD,MAAM,CAAC,eAAe,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACzC,MAAM,aAAa,GAAG,IAAI,CAAC,qBAAqB,CAC9C,eAAkC,CACnC,CAAC;YAEF,IAAI,aAAa,IAAI,uBAAA,IAAI,sDAAiB,CAAC,GAAG,CAAC,EAAE;gBAC/C,MAAM,QAAQ,GAAG,uBAAA,IAAI,sDAAiB,CAAC,GAAG,CAAC,CAAC;gBAC5C,IAAI,QAAQ,EAAE;oBACZ,gFAAgF;oBAChF,kEAAkE;oBAClE,aAAa,CAAC,YAAY,CAAC,cAAc,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;oBAC9D,OAAO,uBAAA,IAAI,sDAAiB,CAAC,GAAG,CAAC,CAAC;iBACnC;aACF;QACH,CAAC;KACF;;IAED,OAAO,6BAA6B,CAAC;AACvC,CAAC;AAED,MAAM,KAAK;CAAG;AAEd,MAAM,CAAC,MAAM,iCAAiC,GAC5C,kCAAkC,CAAC,KAAK,CAAC,CAAC;AAC5C,MAAM,CAAC,MAAM,6BAA6B,GACxC,kCAAkC,CAAC,cAAc,CAAC,CAAC;AACrD,MAAM,CAAC,MAAM,+BAA+B,GAC1C,kCAAkC,CAAC,gBAAgB,CAAC,CAAC","sourcesContent":["import { BaseController, BaseControllerV1 } from '@metamask/base-controller';\nimport type {\n NetworkClientId,\n NetworkClient,\n} from '@metamask/network-controller';\nimport type { Json } from '@metamask/utils';\n\nimport {\n AbstractPollingControllerBaseMixin,\n getKey,\n} from './AbstractPollingController';\nimport type { Constructor, PollingTokenSetId } from './types';\n\n/**\n * BlockTrackerPollingControllerMixin\n * A polling controller that polls using a block tracker.\n *\n * @param Base - The base class to mix onto.\n * @returns The composed class.\n */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nfunction BlockTrackerPollingControllerMixin<TBase extends Constructor>(\n Base: TBase,\n) {\n abstract class BlockTrackerPollingController extends AbstractPollingControllerBaseMixin(\n Base,\n ) {\n #activeListeners: Record<string, (options: Json) => Promise<void>> = {};\n\n abstract _getNetworkClientById(\n networkClientId: NetworkClientId,\n ): NetworkClient | undefined;\n\n _startPollingByNetworkClientId(\n networkClientId: NetworkClientId,\n options: Json,\n ) {\n const key = getKey(networkClientId, options);\n\n if (this.#activeListeners[key]) {\n return;\n }\n\n const networkClient = this._getNetworkClientById(networkClientId);\n if (networkClient) {\n const updateOnNewBlock = this._executePoll.bind(\n this,\n networkClientId,\n options,\n );\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n networkClient.blockTracker.addListener('latest', updateOnNewBlock);\n this.#activeListeners[key] = updateOnNewBlock;\n } else {\n throw new Error(\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `Unable to retrieve blockTracker for networkClientId ${networkClientId}`,\n );\n }\n }\n\n _stopPollingByPollingTokenSetId(key: PollingTokenSetId) {\n const [networkClientId] = key.split(':');\n const networkClient = this._getNetworkClientById(\n networkClientId as NetworkClientId,\n );\n\n if (networkClient && this.#activeListeners[key]) {\n const listener = this.#activeListeners[key];\n if (listener) {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n networkClient.blockTracker.removeListener('latest', listener);\n delete this.#activeListeners[key];\n }\n }\n }\n }\n\n return BlockTrackerPollingController;\n}\n\nclass Empty {}\n\nexport const BlockTrackerPollingControllerOnly =\n BlockTrackerPollingControllerMixin(Empty);\nexport const BlockTrackerPollingController =\n BlockTrackerPollingControllerMixin(BaseController);\nexport const BlockTrackerPollingControllerV1 =\n BlockTrackerPollingControllerMixin(BaseControllerV1);\n"]}