-
-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathBlockTrackerPollingController.ts
More file actions
116 lines (102 loc) · 4.17 KB
/
Copy pathBlockTrackerPollingController.ts
File metadata and controls
116 lines (102 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { BaseController } from '@metamask/base-controller';
import { BaseDataService } from '@metamask/base-data-service';
import type {
NetworkClientId,
NetworkClient,
} from '@metamask/network-controller';
import type { Json } from '@metamask/utils';
import {
AbstractPollingControllerBaseMixin,
getKey,
} from './AbstractPollingController';
import type { Constructor, PollingTokenSetId } from './types';
/**
* The minimum input required to start polling for a {@link BlockTrackerPollingController}.
* Implementing classes may provide additional properties.
*/
export type BlockTrackerPollingInput = {
networkClientId: NetworkClientId;
};
/**
* BlockTrackerPollingControllerMixin
* A polling controller that polls using a block tracker.
*
* @param Base - The base class to mix onto.
* @returns The composed class.
*/
// This is a function that's used as class, and the return type is inferred from
// the class defined inside the function scope, so this can't be easily typed.
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/naming-convention
function BlockTrackerPollingControllerMixin<
TBase extends Constructor,
PollingInput extends BlockTrackerPollingInput,
>(Base: TBase) {
abstract class BlockTrackerPollingController extends AbstractPollingControllerBaseMixin<
TBase,
PollingInput
>(Base) {
#activeListeners: Record<string, (options: Json) => Promise<void>> = {};
abstract _getNetworkClientById(
networkClientId: NetworkClientId,
): NetworkClient | undefined;
_startPolling(input: PollingInput): void {
const key = getKey(input);
if (this.#activeListeners[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);
this.#activeListeners[key] = updateOnNewBlock;
} else {
throw new Error(
`Unable to retrieve blockTracker for networkClientId ${input.networkClientId}`,
);
}
}
_stopPollingByPollingTokenSetId(key: PollingTokenSetId): void {
const { networkClientId } = JSON.parse(key);
const networkClient = this._getNetworkClientById(
networkClientId as NetworkClientId,
);
if (networkClient && this.#activeListeners[key]) {
const listener = this.#activeListeners[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 this.#activeListeners[key];
}
}
}
}
return BlockTrackerPollingController;
}
class Empty {}
export const BlockTrackerPollingControllerOnly = <
PollingInput extends BlockTrackerPollingInput,
// The return type is inferred from the class defined inside the function
// scope, so this can't be easily typed.
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
>() => BlockTrackerPollingControllerMixin<typeof Empty, PollingInput>(Empty);
export const BlockTrackerPollingController = <
PollingInput extends BlockTrackerPollingInput,
// The return type is inferred from the class defined inside the function
// scope, so this can't be easily typed.
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
>() =>
BlockTrackerPollingControllerMixin<typeof BaseController, PollingInput>(
BaseController,
);
export const BlockTrackerPollingDataService = <
PollingInput extends BlockTrackerPollingInput,
// The return type is inferred from the class defined inside the function
// scope, so this can't be easily typed.
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
>() =>
BlockTrackerPollingControllerMixin<typeof BaseDataService, PollingInput>(
BaseDataService,
);