-
-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathStaticIntervalPollingController.ts
More file actions
107 lines (93 loc) · 3.68 KB
/
Copy pathStaticIntervalPollingController.ts
File metadata and controls
107 lines (93 loc) · 3.68 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
import { BaseController } from '@metamask/base-controller';
import { BaseDataService } from '@metamask/base-data-service';
import type { Json } from '@metamask/utils';
import {
AbstractPollingControllerBaseMixin,
getKey,
} from './AbstractPollingController';
import type {
Constructor,
IPollingController,
PollingTokenSetId,
} from './types';
/**
* StaticIntervalPollingControllerMixin
* A polling controller that polls on a static interval.
*
* @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 StaticIntervalPollingControllerMixin<
TBase extends Constructor,
PollingInput extends Json,
>(Base: TBase) {
abstract class StaticIntervalPollingController
extends AbstractPollingControllerBaseMixin<TBase, PollingInput>(Base)
implements IPollingController<PollingInput>
{
readonly #intervalIds: Record<PollingTokenSetId, NodeJS.Timeout> = {};
#intervalLength: number | undefined = 1000;
setIntervalLength(intervalLength: number): void {
this.#intervalLength = intervalLength;
}
getIntervalLength(): number | undefined {
return this.#intervalLength;
}
_startPolling(input: PollingInput): void {
if (!this.#intervalLength) {
throw new Error('intervalLength must be defined and greater than 0');
}
const key = getKey(input);
const existingInterval = this.#intervalIds[key];
this._stopPollingByPollingTokenSetId(key);
// eslint-disable-next-line no-multi-assign
const intervalId = (this.#intervalIds[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 === this.#intervalIds[key]) {
this._startPolling(input);
}
},
existingInterval ? this.#intervalLength : 0,
));
}
_stopPollingByPollingTokenSetId(key: PollingTokenSetId): void {
const intervalId = this.#intervalIds[key];
if (intervalId) {
clearTimeout(intervalId);
delete this.#intervalIds[key];
}
}
}
return StaticIntervalPollingController;
}
class Empty {}
export const StaticIntervalPollingControllerOnly = <
PollingInput extends Json,
// 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
>() => StaticIntervalPollingControllerMixin<typeof Empty, PollingInput>(Empty);
// 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
export const StaticIntervalPollingController = <PollingInput extends Json>() =>
StaticIntervalPollingControllerMixin<typeof BaseController, PollingInput>(
BaseController,
);
// 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
export const StaticIntervalPollingDataService = <PollingInput extends Json>() =>
StaticIntervalPollingControllerMixin<typeof BaseDataService, PollingInput>(
BaseDataService,
);