@khanacademy/wonder-blocks-timing 2.1.1 → 2.1.2
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.
- package/CHANGELOG.md +13 -0
- package/dist/components/action-scheduler-provider.d.ts +26 -0
- package/dist/components/action-scheduler-provider.js.flow +35 -0
- package/dist/components/with-action-scheduler.d.ts +14 -0
- package/dist/components/with-action-scheduler.js.flow +26 -0
- package/dist/es/index.js +157 -119
- package/dist/hooks/internal/use-updating-ref.d.ts +13 -0
- package/dist/hooks/internal/use-updating-ref.js.flow +22 -0
- package/dist/hooks/use-interval.d.ts +8 -0
- package/dist/hooks/use-interval.js.flow +18 -0
- package/dist/hooks/use-scheduled-interval.d.ts +2 -0
- package/dist/hooks/use-scheduled-interval.js.flow +13 -0
- package/dist/hooks/use-scheduled-timeout.d.ts +2 -0
- package/dist/hooks/use-scheduled-timeout.js.flow +13 -0
- package/dist/hooks/use-timeout.d.ts +8 -0
- package/dist/hooks/use-timeout.js.flow +18 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +158 -123
- package/dist/index.js.flow +31 -2
- package/dist/util/action-scheduler.d.ts +21 -0
- package/dist/util/action-scheduler.js.flow +39 -0
- package/dist/util/animation-frame.d.ts +62 -0
- package/dist/util/animation-frame.js.flow +71 -0
- package/dist/util/interval.d.ts +60 -0
- package/dist/util/interval.js.flow +70 -0
- package/dist/util/policies.d.ts +8 -0
- package/dist/util/policies.js.flow +17 -0
- package/dist/util/timeout.d.ts +62 -0
- package/dist/util/timeout.js.flow +72 -0
- package/dist/util/types.d.ts +228 -0
- package/dist/util/types.js.flow +235 -0
- package/dist/util/types.typestest.d.ts +1 -0
- package/dist/util/types.typestest.js.flow +6 -0
- package/package.json +3 -3
- package/src/components/__tests__/{action-scheduler-provider.test.js → action-scheduler-provider.test.tsx} +0 -2
- package/src/components/__tests__/{with-action-scheduler.test.js → with-action-scheduler.test.tsx} +6 -6
- package/src/components/{action-scheduler-provider.js → action-scheduler-provider.ts} +4 -5
- package/src/components/{with-action-scheduler.js → with-action-scheduler.tsx} +10 -18
- package/src/hooks/__tests__/{use-interval.test.js → use-interval.test.ts} +5 -6
- package/src/hooks/__tests__/{use-scheduled-interval.test.js → use-scheduled-interval.test.ts} +5 -6
- package/src/hooks/__tests__/{use-scheduled-timeout.test.js → use-scheduled-timeout.test.ts} +9 -10
- package/src/hooks/__tests__/{use-timeout.test.js → use-timeout.test.ts} +5 -6
- package/src/hooks/internal/{use-updating-ref.js → use-updating-ref.ts} +5 -2
- package/src/hooks/{use-interval.js → use-interval.ts} +1 -2
- package/src/hooks/{use-scheduled-interval.js → use-scheduled-interval.ts} +2 -2
- package/src/hooks/{use-scheduled-timeout.js → use-scheduled-timeout.ts} +2 -2
- package/src/hooks/{use-timeout.js → use-timeout.ts} +1 -2
- package/src/{index.js → index.ts} +0 -1
- package/src/util/__tests__/{action-scheduler.test.js → action-scheduler.test.ts} +6 -16
- package/src/util/__tests__/{animation-frame.test.js → animation-frame.test.ts} +8 -7
- package/src/util/__tests__/{interval.test.js → interval.test.ts} +1 -2
- package/src/util/__tests__/{timeout.test.js → timeout.test.ts} +1 -2
- package/src/util/{action-scheduler.js → action-scheduler.ts} +13 -6
- package/src/util/{animation-frame.js → animation-frame.ts} +4 -4
- package/src/util/{interval.js → interval.ts} +5 -4
- package/src/util/{policies.js → policies.ts} +2 -3
- package/src/util/{timeout.js → timeout.ts} +5 -4
- package/src/util/{types.js → types.ts} +21 -28
- package/src/util/{types.flowtest.js → types.typestest.tsx} +20 -18
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/src/components/__docs__/migration.stories.mdx +0 -112
- package/src/components/__docs__/types.ischedule-actions.stories.mdx +0 -157
- package/src/components/__docs__/with-action-scheduler-examples.js +0 -80
- package/src/components/__docs__/with-action-scheduler.stories.mdx +0 -218
- package/src/hooks/__docs__/use-interval.stories.mdx +0 -80
- package/src/hooks/__docs__/use-scheduled-interval.stories.mdx +0 -147
- package/src/hooks/__docs__/use-scheduled-timeout.stories.mdx +0 -148
- package/src/hooks/__docs__/use-timeout.stories.mdx +0 -80
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import {useEffect, useState, useCallback} from "react";
|
|
3
2
|
|
|
4
3
|
import {
|
|
@@ -11,7 +10,7 @@ import {useUpdatingRef} from "./internal/use-updating-ref";
|
|
|
11
10
|
import {useTimeout} from "./use-timeout";
|
|
12
11
|
|
|
13
12
|
export function useScheduledTimeout(
|
|
14
|
-
action: () =>
|
|
13
|
+
action: () => unknown,
|
|
15
14
|
timeoutMs: number,
|
|
16
15
|
options?: Options,
|
|
17
16
|
): ITimeout {
|
|
@@ -76,5 +75,6 @@ export function useScheduledTimeout(
|
|
|
76
75
|
|
|
77
76
|
useTimeout(wrappedAction, timeoutMs, isSet);
|
|
78
77
|
|
|
78
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'boolean' is not assignable to type '() => boolean'.
|
|
79
79
|
return {isSet, set, clear};
|
|
80
80
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import {useEffect} from "react";
|
|
3
2
|
|
|
4
3
|
import {useUpdatingRef} from "./internal/use-updating-ref";
|
|
@@ -11,7 +10,7 @@ import {useUpdatingRef} from "./internal/use-updating-ref";
|
|
|
11
10
|
* @param active whether or not the interval is active
|
|
12
11
|
*/
|
|
13
12
|
export function useTimeout(
|
|
14
|
-
action: () =>
|
|
13
|
+
action: () => unknown,
|
|
15
14
|
timeoutMs: number,
|
|
16
15
|
active: boolean,
|
|
17
16
|
) {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import ActionScheduler from "../action-scheduler";
|
|
3
2
|
import Timeout from "../timeout";
|
|
4
3
|
import Interval from "../interval";
|
|
@@ -45,7 +44,7 @@ describe("ActionScheduler", () => {
|
|
|
45
44
|
const action = jest.fn();
|
|
46
45
|
const options = {
|
|
47
46
|
schedulePolicy: SchedulePolicy.Immediately,
|
|
48
|
-
};
|
|
47
|
+
} as const;
|
|
49
48
|
|
|
50
49
|
// Act
|
|
51
50
|
actionScheduler.timeout(action, 42, options);
|
|
@@ -64,14 +63,13 @@ describe("ActionScheduler", () => {
|
|
|
64
63
|
const action = jest.fn();
|
|
65
64
|
const options = {
|
|
66
65
|
clearPolicy: ClearPolicy.Resolve,
|
|
67
|
-
};
|
|
66
|
+
} as const;
|
|
68
67
|
const testTimeout = actionScheduler.timeout(action, 42, options);
|
|
69
68
|
|
|
70
69
|
// Act
|
|
71
70
|
actionScheduler.clearAll();
|
|
72
71
|
|
|
73
72
|
// Assert
|
|
74
|
-
// $FlowIgnore[method-unbinding]
|
|
75
73
|
expect(testTimeout.clear).toHaveBeenCalledWith(ClearPolicy.Resolve);
|
|
76
74
|
});
|
|
77
75
|
|
|
@@ -110,7 +108,7 @@ describe("ActionScheduler", () => {
|
|
|
110
108
|
const action = jest.fn();
|
|
111
109
|
const options = {
|
|
112
110
|
schedulePolicy: SchedulePolicy.Immediately,
|
|
113
|
-
};
|
|
111
|
+
} as const;
|
|
114
112
|
|
|
115
113
|
// Act
|
|
116
114
|
actionScheduler.interval(action, 42, options);
|
|
@@ -129,14 +127,13 @@ describe("ActionScheduler", () => {
|
|
|
129
127
|
const action = jest.fn();
|
|
130
128
|
const options = {
|
|
131
129
|
clearPolicy: ClearPolicy.Resolve,
|
|
132
|
-
};
|
|
130
|
+
} as const;
|
|
133
131
|
const testInterval = actionScheduler.interval(action, 42, options);
|
|
134
132
|
|
|
135
133
|
// Act
|
|
136
134
|
actionScheduler.clearAll();
|
|
137
135
|
|
|
138
136
|
// Assert
|
|
139
|
-
// $FlowIgnore[method-unbinding]
|
|
140
137
|
expect(testInterval.clear).toHaveBeenCalledWith(
|
|
141
138
|
ClearPolicy.Resolve,
|
|
142
139
|
);
|
|
@@ -177,7 +174,7 @@ describe("ActionScheduler", () => {
|
|
|
177
174
|
const action = jest.fn();
|
|
178
175
|
const options = {
|
|
179
176
|
schedulePolicy: SchedulePolicy.Immediately,
|
|
180
|
-
};
|
|
177
|
+
} as const;
|
|
181
178
|
|
|
182
179
|
// Act
|
|
183
180
|
actionScheduler.animationFrame(action, options);
|
|
@@ -195,14 +192,13 @@ describe("ActionScheduler", () => {
|
|
|
195
192
|
const action = jest.fn();
|
|
196
193
|
const options = {
|
|
197
194
|
clearPolicy: ClearPolicy.Resolve,
|
|
198
|
-
};
|
|
195
|
+
} as const;
|
|
199
196
|
const testFrame = actionScheduler.animationFrame(action, options);
|
|
200
197
|
|
|
201
198
|
// Act
|
|
202
199
|
actionScheduler.clearAll();
|
|
203
200
|
|
|
204
201
|
// Assert
|
|
205
|
-
// $FlowIgnore[method-unbinding]
|
|
206
202
|
expect(testFrame.clear).toHaveBeenCalledWith(ClearPolicy.Resolve);
|
|
207
203
|
});
|
|
208
204
|
|
|
@@ -235,11 +231,8 @@ describe("ActionScheduler", () => {
|
|
|
235
231
|
actionScheduler.clearAll();
|
|
236
232
|
|
|
237
233
|
// Assert
|
|
238
|
-
// $FlowIgnore[method-unbinding]
|
|
239
234
|
expect(timeout.clear).toHaveBeenCalledTimes(1);
|
|
240
|
-
// $FlowIgnore[method-unbinding]
|
|
241
235
|
expect(interval.clear).toHaveBeenCalledTimes(1);
|
|
242
|
-
// $FlowIgnore[method-unbinding]
|
|
243
236
|
expect(animationFrame.clear).toHaveBeenCalledTimes(1);
|
|
244
237
|
});
|
|
245
238
|
|
|
@@ -256,11 +249,8 @@ describe("ActionScheduler", () => {
|
|
|
256
249
|
actionScheduler.clearAll();
|
|
257
250
|
|
|
258
251
|
// Assert
|
|
259
|
-
// $FlowIgnore[method-unbinding]
|
|
260
252
|
expect(timeout.clear).toHaveBeenCalledTimes(1);
|
|
261
|
-
// $FlowIgnore[method-unbinding]
|
|
262
253
|
expect(interval.clear).toHaveBeenCalledTimes(1);
|
|
263
|
-
// $FlowIgnore[method-unbinding]
|
|
264
254
|
expect(animationFrame.clear).toHaveBeenCalledTimes(1);
|
|
265
255
|
});
|
|
266
256
|
});
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import AnimationFrame from "../animation-frame";
|
|
3
2
|
import {SchedulePolicy, ClearPolicy} from "../policies";
|
|
4
3
|
|
|
@@ -10,10 +9,11 @@ describe("AnimationFrame", () => {
|
|
|
10
9
|
// it here and map it to timeouts, that way we can use the fake timer
|
|
11
10
|
// API to test our animation frame things.
|
|
12
11
|
jest.spyOn(global, "requestAnimationFrame").mockImplementation(
|
|
13
|
-
(fn, ...args) =>
|
|
12
|
+
// @ts-expect-error [FEI-5019] - TS2345 - Argument of type '(fn: any, ...args: any) => NodeJS.Timeout' is not assignable to parameter of type '(callback: FrameRequestCallback) => number'.
|
|
13
|
+
(fn: any, ...args: any) => setTimeout(fn, 0),
|
|
14
14
|
);
|
|
15
15
|
jest.spyOn(global, "cancelAnimationFrame").mockImplementation(
|
|
16
|
-
(id, ...args) => clearTimeout(id),
|
|
16
|
+
(id: any, ...args: any) => clearTimeout(id),
|
|
17
17
|
);
|
|
18
18
|
});
|
|
19
19
|
|
|
@@ -36,7 +36,7 @@ describe("AnimationFrame", () => {
|
|
|
36
36
|
// Arrange
|
|
37
37
|
|
|
38
38
|
// Act
|
|
39
|
-
const underTest = () => new AnimationFrame(
|
|
39
|
+
const underTest = () => new AnimationFrame(null as any);
|
|
40
40
|
|
|
41
41
|
// Assert
|
|
42
42
|
expect(underTest).toThrowErrorMatchingInlineSnapshot(
|
|
@@ -113,10 +113,11 @@ describe("AnimationFrame", () => {
|
|
|
113
113
|
it("should invoke requestAnimationFrame to call the given action", () => {
|
|
114
114
|
// Arrange
|
|
115
115
|
const action = jest.fn();
|
|
116
|
-
const animationFrame = new AnimationFrame((time) =>
|
|
116
|
+
const animationFrame = new AnimationFrame((time: any) =>
|
|
117
|
+
action(time),
|
|
118
|
+
);
|
|
117
119
|
animationFrame.set();
|
|
118
|
-
//
|
|
119
|
-
// $FlowFixMe[prop-missing]
|
|
120
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'mock' does not exist on type '(callback: FrameRequestCallback) => number'.
|
|
120
121
|
const scheduledAction = requestAnimationFrame.mock.calls[0][0];
|
|
121
122
|
|
|
122
123
|
// Act
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import Interval from "../interval";
|
|
3
2
|
import {SchedulePolicy, ClearPolicy} from "../policies";
|
|
4
3
|
|
|
@@ -26,7 +25,7 @@ describe("Interval", () => {
|
|
|
26
25
|
// Arrange
|
|
27
26
|
|
|
28
27
|
// Act
|
|
29
|
-
const underTest = () => new Interval(
|
|
28
|
+
const underTest = () => new Interval(null as any, 1);
|
|
30
29
|
|
|
31
30
|
// Assert
|
|
32
31
|
expect(underTest).toThrowErrorMatchingInlineSnapshot(
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import Timeout from "../timeout";
|
|
3
2
|
import {SchedulePolicy, ClearPolicy} from "../policies";
|
|
4
3
|
|
|
@@ -26,7 +25,7 @@ describe("Timeout", () => {
|
|
|
26
25
|
// Arrange
|
|
27
26
|
|
|
28
27
|
// Act
|
|
29
|
-
const underTest = () => new Timeout(
|
|
28
|
+
const underTest = () => new Timeout(null as any, 0);
|
|
30
29
|
|
|
31
30
|
// Assert
|
|
32
31
|
expect(underTest).toThrowErrorMatchingInlineSnapshot(
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import Timeout from "./timeout";
|
|
3
2
|
import Interval from "./interval";
|
|
4
3
|
import AnimationFrame from "./animation-frame";
|
|
@@ -18,27 +17,33 @@ import type {
|
|
|
18
17
|
* `IScheduleActions` instance.
|
|
19
18
|
*/
|
|
20
19
|
export default class ActionScheduler implements IScheduleActions {
|
|
21
|
-
_disabled
|
|
20
|
+
_disabled = false;
|
|
22
21
|
_registeredActions: Array<() => void> = [];
|
|
23
|
-
static
|
|
22
|
+
static readonly NoopAction: ITimeout & IAnimationFrame & IInterval = {
|
|
24
23
|
set: () => {},
|
|
24
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'boolean' is not assignable to type '(() => boolean) & (() => boolean) & (() => boolean)'.
|
|
25
25
|
get isSet() {
|
|
26
26
|
return false;
|
|
27
27
|
},
|
|
28
28
|
clear: () => {},
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
-
timeout(
|
|
31
|
+
timeout(
|
|
32
|
+
action: () => unknown,
|
|
33
|
+
period: number,
|
|
34
|
+
options?: Options,
|
|
35
|
+
): ITimeout {
|
|
32
36
|
if (this._disabled) {
|
|
33
37
|
return ActionScheduler.NoopAction;
|
|
34
38
|
}
|
|
35
39
|
const timeout = new Timeout(action, period, options?.schedulePolicy);
|
|
36
40
|
this._registeredActions.push(() => timeout.clear(options?.clearPolicy));
|
|
41
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'Timeout' is not assignable to type 'ITimeout'.
|
|
37
42
|
return timeout;
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
interval(
|
|
41
|
-
action: () =>
|
|
46
|
+
action: () => unknown,
|
|
42
47
|
period: number,
|
|
43
48
|
options?: Options,
|
|
44
49
|
): IInterval {
|
|
@@ -49,11 +54,12 @@ export default class ActionScheduler implements IScheduleActions {
|
|
|
49
54
|
this._registeredActions.push(() =>
|
|
50
55
|
interval.clear(options?.clearPolicy),
|
|
51
56
|
);
|
|
57
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'Interval' is not assignable to type 'IInterval'.
|
|
52
58
|
return interval;
|
|
53
59
|
}
|
|
54
60
|
|
|
55
61
|
animationFrame(
|
|
56
|
-
action: (DOMHighResTimeStamp) => void,
|
|
62
|
+
action: (arg1: DOMHighResTimeStamp) => void,
|
|
57
63
|
options?: Options,
|
|
58
64
|
): IAnimationFrame {
|
|
59
65
|
if (this._disabled) {
|
|
@@ -66,6 +72,7 @@ export default class ActionScheduler implements IScheduleActions {
|
|
|
66
72
|
this._registeredActions.push(() =>
|
|
67
73
|
animationFrame.clear(options?.clearPolicy),
|
|
68
74
|
);
|
|
75
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'AnimationFrame' is not assignable to type 'IAnimationFrame'.
|
|
69
76
|
return animationFrame;
|
|
70
77
|
}
|
|
71
78
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import {
|
|
3
2
|
SchedulePolicy as SchedulePolicies,
|
|
4
3
|
ClearPolicy as ClearPolicies,
|
|
@@ -16,8 +15,8 @@ import type {IAnimationFrame, SchedulePolicy, ClearPolicy} from "./types";
|
|
|
16
15
|
* @implements {IAnimationFrame}
|
|
17
16
|
*/
|
|
18
17
|
export default class AnimationFrame implements IAnimationFrame {
|
|
19
|
-
_animationFrameId:
|
|
20
|
-
_action: (DOMHighResTimeStamp) =>
|
|
18
|
+
_animationFrameId: number | null | undefined;
|
|
19
|
+
_action: (arg1: DOMHighResTimeStamp) => unknown;
|
|
21
20
|
|
|
22
21
|
/**
|
|
23
22
|
* Creates an animation frame request that will invoke the given action.
|
|
@@ -31,7 +30,7 @@ export default class AnimationFrame implements IAnimationFrame {
|
|
|
31
30
|
* @memberof AnimationFrame
|
|
32
31
|
*/
|
|
33
32
|
constructor(
|
|
34
|
-
action: (DOMHighResTimeStamp) =>
|
|
33
|
+
action: (arg1: DOMHighResTimeStamp) => unknown,
|
|
35
34
|
schedulePolicy: SchedulePolicy = SchedulePolicies.Immediately,
|
|
36
35
|
) {
|
|
37
36
|
if (typeof action !== "function") {
|
|
@@ -52,6 +51,7 @@ export default class AnimationFrame implements IAnimationFrame {
|
|
|
52
51
|
* false.
|
|
53
52
|
* @memberof AnimationFrame
|
|
54
53
|
*/
|
|
54
|
+
// @ts-expect-error [FEI-5019] - TS2416 - Property 'isSet' in type 'AnimationFrame' is not assignable to the same property in base type 'IAnimationFrame'.
|
|
55
55
|
get isSet(): boolean {
|
|
56
56
|
return this._animationFrameId != null;
|
|
57
57
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import {
|
|
3
2
|
SchedulePolicy as SchedulePolicies,
|
|
4
3
|
ClearPolicy as ClearPolicies,
|
|
@@ -16,8 +15,8 @@ import type {IInterval, SchedulePolicy, ClearPolicy} from "./types";
|
|
|
16
15
|
* @implements {IInterval}
|
|
17
16
|
*/
|
|
18
17
|
export default class Interval implements IInterval {
|
|
19
|
-
_intervalId:
|
|
20
|
-
_action: () =>
|
|
18
|
+
_intervalId: number | null | undefined;
|
|
19
|
+
_action: () => unknown;
|
|
21
20
|
_intervalMs: number;
|
|
22
21
|
|
|
23
22
|
/**
|
|
@@ -34,7 +33,7 @@ export default class Interval implements IInterval {
|
|
|
34
33
|
* @memberof Interval
|
|
35
34
|
*/
|
|
36
35
|
constructor(
|
|
37
|
-
action: () =>
|
|
36
|
+
action: () => unknown,
|
|
38
37
|
intervalMs: number,
|
|
39
38
|
schedulePolicy: SchedulePolicy = SchedulePolicies.Immediately,
|
|
40
39
|
) {
|
|
@@ -60,6 +59,7 @@ export default class Interval implements IInterval {
|
|
|
60
59
|
* @returns {boolean} true if the interval is active, otherwise false.
|
|
61
60
|
* @memberof Interval
|
|
62
61
|
*/
|
|
62
|
+
// @ts-expect-error [FEI-5019] - TS2416 - Property 'isSet' in type 'Interval' is not assignable to the same property in base type 'IInterval'.
|
|
63
63
|
get isSet(): boolean {
|
|
64
64
|
return this._intervalId != null;
|
|
65
65
|
}
|
|
@@ -76,6 +76,7 @@ export default class Interval implements IInterval {
|
|
|
76
76
|
if (this.isSet) {
|
|
77
77
|
this.clear(ClearPolicies.Cancel);
|
|
78
78
|
}
|
|
79
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'Timer' is not assignable to type 'number'.
|
|
79
80
|
this._intervalId = setInterval(() => this._action(), this._intervalMs);
|
|
80
81
|
}
|
|
81
82
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import {
|
|
3
2
|
SchedulePolicy as SchedulePolicies,
|
|
4
3
|
ClearPolicy as ClearPolicies,
|
|
@@ -16,8 +15,8 @@ import type {ITimeout, SchedulePolicy, ClearPolicy} from "./types";
|
|
|
16
15
|
* @implements {ITimeout}
|
|
17
16
|
*/
|
|
18
17
|
export default class Timeout implements ITimeout {
|
|
19
|
-
_timeoutId:
|
|
20
|
-
_action: () =>
|
|
18
|
+
_timeoutId: number | null | undefined;
|
|
19
|
+
_action: () => unknown;
|
|
21
20
|
_timeoutMs: number;
|
|
22
21
|
|
|
23
22
|
/**
|
|
@@ -34,7 +33,7 @@ export default class Timeout implements ITimeout {
|
|
|
34
33
|
* @memberof Timeout
|
|
35
34
|
*/
|
|
36
35
|
constructor(
|
|
37
|
-
action: () =>
|
|
36
|
+
action: () => unknown,
|
|
38
37
|
timeoutMs: number,
|
|
39
38
|
schedulePolicy: SchedulePolicy = SchedulePolicies.Immediately,
|
|
40
39
|
) {
|
|
@@ -61,6 +60,7 @@ export default class Timeout implements ITimeout {
|
|
|
61
60
|
* false.
|
|
62
61
|
* @memberof Timeout
|
|
63
62
|
*/
|
|
63
|
+
// @ts-expect-error [FEI-5019] - TS2416 - Property 'isSet' in type 'Timeout' is not assignable to the same property in base type 'ITimeout'.
|
|
64
64
|
get isSet(): boolean {
|
|
65
65
|
return this._timeoutId != null;
|
|
66
66
|
}
|
|
@@ -78,6 +78,7 @@ export default class Timeout implements ITimeout {
|
|
|
78
78
|
if (this.isSet) {
|
|
79
79
|
this.clear(ClearPolicies.Cancel);
|
|
80
80
|
}
|
|
81
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'Timeout' is not assignable to type 'number'.
|
|
81
82
|
this._timeoutId = setTimeout(
|
|
82
83
|
() => this.clear(ClearPolicies.Resolve),
|
|
83
84
|
this._timeoutMs,
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
export type SchedulePolicy = "schedule-immediately" | "schedule-on-demand";
|
|
3
2
|
|
|
4
3
|
export type ClearPolicy = "resolve-on-clear" | "cancel-on-clear";
|
|
@@ -19,8 +18,7 @@ export interface ITimeout {
|
|
|
19
18
|
* false.
|
|
20
19
|
* @memberof ITimeout
|
|
21
20
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
isSet(): boolean;
|
|
24
22
|
/**
|
|
25
23
|
* Set the timeout.
|
|
26
24
|
*
|
|
@@ -31,7 +29,6 @@ export interface ITimeout {
|
|
|
31
29
|
* @memberof ITimeout
|
|
32
30
|
*/
|
|
33
31
|
set(): void;
|
|
34
|
-
|
|
35
32
|
/**
|
|
36
33
|
* Clear the set timeout.
|
|
37
34
|
*
|
|
@@ -63,8 +60,7 @@ export interface IInterval {
|
|
|
63
60
|
* @returns {boolean} true if the interval is active, otherwise false.
|
|
64
61
|
* @memberof IInterval
|
|
65
62
|
*/
|
|
66
|
-
|
|
67
|
-
|
|
63
|
+
isSet(): boolean;
|
|
68
64
|
/**
|
|
69
65
|
* Set the interval.
|
|
70
66
|
*
|
|
@@ -74,7 +70,6 @@ export interface IInterval {
|
|
|
74
70
|
* @memberof IInterval
|
|
75
71
|
*/
|
|
76
72
|
set(): void;
|
|
77
|
-
|
|
78
73
|
/**
|
|
79
74
|
* Clear the active interval.
|
|
80
75
|
*
|
|
@@ -107,8 +102,7 @@ export interface IAnimationFrame {
|
|
|
107
102
|
* false.
|
|
108
103
|
* @memberof IAnimationFrame
|
|
109
104
|
*/
|
|
110
|
-
|
|
111
|
-
|
|
105
|
+
isSet(): boolean;
|
|
112
106
|
/**
|
|
113
107
|
* Set the request.
|
|
114
108
|
*
|
|
@@ -119,7 +113,6 @@ export interface IAnimationFrame {
|
|
|
119
113
|
* @memberof IAnimationFrame
|
|
120
114
|
*/
|
|
121
115
|
set(): void;
|
|
122
|
-
|
|
123
116
|
/**
|
|
124
117
|
* Clear the set request.
|
|
125
118
|
*
|
|
@@ -136,10 +129,10 @@ export interface IAnimationFrame {
|
|
|
136
129
|
clear(policy?: ClearPolicy): void;
|
|
137
130
|
}
|
|
138
131
|
|
|
139
|
-
export type Options = {
|
|
140
|
-
schedulePolicy?: SchedulePolicy
|
|
141
|
-
clearPolicy?: ClearPolicy
|
|
142
|
-
|
|
132
|
+
export type Options = {
|
|
133
|
+
schedulePolicy?: SchedulePolicy;
|
|
134
|
+
clearPolicy?: ClearPolicy;
|
|
135
|
+
};
|
|
143
136
|
|
|
144
137
|
/**
|
|
145
138
|
* Provides means to request timeouts, intervals, and animation frames, with
|
|
@@ -172,8 +165,7 @@ export interface IScheduleActions {
|
|
|
172
165
|
* @returns {ITimeout} A interface for manipulating the created timeout.
|
|
173
166
|
* @memberof IScheduleActions
|
|
174
167
|
*/
|
|
175
|
-
timeout(action: () =>
|
|
176
|
-
|
|
168
|
+
timeout(action: () => unknown, period: number, options?: Options): ITimeout;
|
|
177
169
|
/**
|
|
178
170
|
* Request an interval.
|
|
179
171
|
*
|
|
@@ -195,8 +187,11 @@ export interface IScheduleActions {
|
|
|
195
187
|
* @returns {IInterval} An interface for manipulating the created interval.
|
|
196
188
|
* @memberof IScheduleActions
|
|
197
189
|
*/
|
|
198
|
-
interval(
|
|
199
|
-
|
|
190
|
+
interval(
|
|
191
|
+
action: () => unknown,
|
|
192
|
+
period: number,
|
|
193
|
+
options?: Options,
|
|
194
|
+
): IInterval;
|
|
200
195
|
/**
|
|
201
196
|
* Request an animation frame.
|
|
202
197
|
*
|
|
@@ -219,7 +214,6 @@ export interface IScheduleActions {
|
|
|
219
214
|
action: (time: DOMHighResTimeStamp) => void,
|
|
220
215
|
options?: Options,
|
|
221
216
|
): IAnimationFrame;
|
|
222
|
-
|
|
223
217
|
/**
|
|
224
218
|
* Clears all timeouts, intervals, and animation frame requests that were
|
|
225
219
|
* made with this scheduler.
|
|
@@ -233,23 +227,22 @@ export interface IScheduleActions {
|
|
|
233
227
|
* A props object type that can be spread into the a componenet wrapped with
|
|
234
228
|
* the `withActionScheduler` higher order component.
|
|
235
229
|
*/
|
|
236
|
-
export type WithActionSchedulerProps = {
|
|
230
|
+
export type WithActionSchedulerProps = {
|
|
237
231
|
/**
|
|
238
232
|
* An instance of the `IScheduleActions` API to use for scheduling
|
|
239
233
|
* intervals, timeouts, and animation frame requests.
|
|
240
234
|
*/
|
|
241
|
-
schedule: IScheduleActions
|
|
242
|
-
|
|
235
|
+
schedule: IScheduleActions;
|
|
236
|
+
};
|
|
243
237
|
|
|
244
|
-
export type WithoutActionScheduler<TProps
|
|
245
|
-
|
|
238
|
+
export type WithoutActionScheduler<TProps extends object> = Omit<
|
|
239
|
+
TProps,
|
|
240
|
+
"schedule"
|
|
246
241
|
>;
|
|
247
242
|
|
|
248
243
|
/**
|
|
249
244
|
* Extends the given props with props that the `withActionScheduler` higher
|
|
250
245
|
* order component will inject.
|
|
251
246
|
*/
|
|
252
|
-
export type WithActionScheduler<TOwnProps
|
|
253
|
-
|
|
254
|
-
...WithActionSchedulerProps,
|
|
255
|
-
|};
|
|
247
|
+
export type WithActionScheduler<TOwnProps extends object> = TOwnProps &
|
|
248
|
+
WithActionSchedulerProps;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
/* eslint-disable no-unused-vars */
|
|
2
|
-
// @flow
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
3
2
|
/**
|
|
4
|
-
* This file ensures that our
|
|
3
|
+
* This file ensures that our TypeScript types are working right.
|
|
5
4
|
*/
|
|
6
5
|
import * as React from "react";
|
|
7
6
|
import withActionScheduler from "../components/with-action-scheduler";
|
|
@@ -16,12 +15,13 @@ import type {WithActionSchedulerProps} from "./types";
|
|
|
16
15
|
* Given a react component with action scheduler props, we receive an
|
|
17
16
|
* abstract component of `OwnProps1`.
|
|
18
17
|
*/
|
|
19
|
-
type Props1 = {
|
|
20
|
-
test: string
|
|
21
|
-
|
|
22
|
-
|};
|
|
18
|
+
type Props1 = {
|
|
19
|
+
test: string;
|
|
20
|
+
} & WithActionSchedulerProps;
|
|
23
21
|
|
|
24
|
-
const InnerComponent1 = (props
|
|
22
|
+
const InnerComponent1: React.FC<Props1> = (props): React.ReactElement => (
|
|
23
|
+
<>{props.test}</>
|
|
24
|
+
);
|
|
25
25
|
|
|
26
26
|
const HOCComponent1 = withActionScheduler(InnerComponent1);
|
|
27
27
|
|
|
@@ -30,12 +30,13 @@ const HOCComponent1 = withActionScheduler(InnerComponent1);
|
|
|
30
30
|
* The withActionScheduler call is returning a component of type OwnProps2; the
|
|
31
31
|
* variable assigned however is incorrectly specified as Props2.
|
|
32
32
|
*/
|
|
33
|
-
type Props2 = {
|
|
34
|
-
test: string
|
|
35
|
-
|
|
36
|
-
|};
|
|
33
|
+
type Props2 = {
|
|
34
|
+
test: string;
|
|
35
|
+
} & WithActionSchedulerProps;
|
|
37
36
|
|
|
38
|
-
const InnerComponent2 = (props
|
|
37
|
+
const InnerComponent2: React.FC<Props2> = (props): React.ReactElement => (
|
|
38
|
+
<>{props.test}</>
|
|
39
|
+
);
|
|
39
40
|
|
|
40
41
|
/**
|
|
41
42
|
* Cannot assign `withActionScheduler(...)` to `HOCComponent2` because property
|
|
@@ -51,12 +52,13 @@ const HOCComponent2 = withActionScheduler(InnerComponent2);
|
|
|
51
52
|
* The withActionScheduler call is being passed a component that isn't set up
|
|
52
53
|
* to be used with it as it doesn't have all the props necessary.
|
|
53
54
|
*/
|
|
54
|
-
type Props3 = {
|
|
55
|
-
test: string
|
|
56
|
-
|
|
57
|
-
|};
|
|
55
|
+
type Props3 = {
|
|
56
|
+
test: string;
|
|
57
|
+
} & WithActionSchedulerProps;
|
|
58
58
|
|
|
59
|
-
const InnerComponent3 = (props
|
|
59
|
+
const InnerComponent3: React.FC<Props3> = (props): React.ReactElement => (
|
|
60
|
+
<>{props.test}</>
|
|
61
|
+
);
|
|
60
62
|
|
|
61
63
|
const HOCComponent3 = withActionScheduler(
|
|
62
64
|
/**
|