@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
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flowtype definitions for types
|
|
3
|
+
* Generated by Flowgen from a Typescript Definition
|
|
4
|
+
* Flowgen v1.21.0
|
|
5
|
+
* @flow
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type SchedulePolicy = "schedule-immediately" | "schedule-on-demand";
|
|
9
|
+
export type ClearPolicy = "resolve-on-clear" | "cancel-on-clear";
|
|
10
|
+
/**
|
|
11
|
+
* Encapsulates everything associated with calling setTimeout/clearTimeout, and
|
|
12
|
+
* managing the lifecycle of that timer, including the ability to resolve or
|
|
13
|
+
* cancel a pending timeout action.
|
|
14
|
+
* @export
|
|
15
|
+
* @interface ITimeout
|
|
16
|
+
*/
|
|
17
|
+
export interface ITimeout {
|
|
18
|
+
/**
|
|
19
|
+
* Determine if the timeout is set or not.
|
|
20
|
+
* @returns {boolean} true if the timeout is set (aka pending), otherwise
|
|
21
|
+
* false.
|
|
22
|
+
* @memberof ITimeout
|
|
23
|
+
*/
|
|
24
|
+
isSet(): boolean;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Set the timeout.
|
|
28
|
+
*
|
|
29
|
+
* If the timeout is pending, this cancels that pending timeout and
|
|
30
|
+
* starts the timeout afresh. If the timeout is not pending, this
|
|
31
|
+
* starts the timeout.
|
|
32
|
+
* @memberof ITimeout
|
|
33
|
+
*/
|
|
34
|
+
set(): void;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Clear the set timeout.
|
|
38
|
+
*
|
|
39
|
+
* If the timeout is pending, this cancels that pending timeout. If no
|
|
40
|
+
* timeout is pending, this does nothing.
|
|
41
|
+
* @param {ClearPolicy} [policy] When ClearPolicy.Resolve, if the request
|
|
42
|
+
* was set when called, the request action is invoked after cancelling
|
|
43
|
+
* the request; otherwise, the pending action is cancelled.
|
|
44
|
+
* Defaults to `ClearPolicy.Cancel`.
|
|
45
|
+
* @memberof ITimeout
|
|
46
|
+
*/
|
|
47
|
+
clear(policy?: ClearPolicy): void;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Encapsulates everything associated with calling setInterval/clearInterval,
|
|
51
|
+
* and managing the lifecycle of that interval, including the ability to resolve
|
|
52
|
+
* or cancel an active interval.
|
|
53
|
+
* @export
|
|
54
|
+
* @interface IInterval
|
|
55
|
+
*/
|
|
56
|
+
export interface IInterval {
|
|
57
|
+
/**
|
|
58
|
+
* Determine if the interval is active or not.
|
|
59
|
+
* @returns {boolean} true if the interval is active, otherwise false.
|
|
60
|
+
* @memberof IInterval
|
|
61
|
+
*/
|
|
62
|
+
isSet(): boolean;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Set the interval.
|
|
66
|
+
*
|
|
67
|
+
* If the interval is active, this cancels that interval and restarts it
|
|
68
|
+
* afresh. If the interval is not active, this starts the interval.
|
|
69
|
+
* @memberof IInterval
|
|
70
|
+
*/
|
|
71
|
+
set(): void;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Clear the active interval.
|
|
75
|
+
*
|
|
76
|
+
* If the interval is active, this cancels that interval. If the interval
|
|
77
|
+
* is not active, this does nothing.
|
|
78
|
+
* @param {ClearPolicy} [policy] When ClearPolicy.Resolve, if the request
|
|
79
|
+
* was set when called, the request action is invoked after cancelling
|
|
80
|
+
* the request; otherwise, the pending action is cancelled.
|
|
81
|
+
* Defaults to `ClearPolicy.Cancel`.
|
|
82
|
+
* @memberof IInterval
|
|
83
|
+
*/
|
|
84
|
+
clear(policy?: ClearPolicy): void;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Encapsulates everything associated with calling requestAnimationFrame/
|
|
88
|
+
* cancelAnimationFrame, and managing the lifecycle of that request, including
|
|
89
|
+
* the ability to resolve or cancel a pending request.
|
|
90
|
+
* @export
|
|
91
|
+
* @interface IAnimationFrame
|
|
92
|
+
*/
|
|
93
|
+
export interface IAnimationFrame {
|
|
94
|
+
/**
|
|
95
|
+
* Determine if the request is set or not.
|
|
96
|
+
* @returns {boolean} true if the request is set (aka pending), otherwise
|
|
97
|
+
* false.
|
|
98
|
+
* @memberof IAnimationFrame
|
|
99
|
+
*/
|
|
100
|
+
isSet(): boolean;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Set the request.
|
|
104
|
+
*
|
|
105
|
+
* If the request is pending, this cancels that pending request and
|
|
106
|
+
* starts a request afresh. If the request is not pending, this
|
|
107
|
+
* starts the request.
|
|
108
|
+
* @memberof IAnimationFrame
|
|
109
|
+
*/
|
|
110
|
+
set(): void;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Clear the set request.
|
|
114
|
+
*
|
|
115
|
+
* If the request is pending, this cancels that pending request. If no
|
|
116
|
+
* request is pending, this does nothing.
|
|
117
|
+
* @param {ClearPolicy} [policy] When ClearPolicy.Resolve, if the request
|
|
118
|
+
* was set when called, the request action is invoked after cancelling
|
|
119
|
+
* the request; otherwise, the pending action is cancelled.
|
|
120
|
+
* Defaults to `ClearPolicy.Cancel`.
|
|
121
|
+
* @memberof IAnimationFrame
|
|
122
|
+
*/
|
|
123
|
+
clear(policy?: ClearPolicy): void;
|
|
124
|
+
}
|
|
125
|
+
export type Options = {
|
|
126
|
+
schedulePolicy?: SchedulePolicy,
|
|
127
|
+
clearPolicy?: ClearPolicy,
|
|
128
|
+
...
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Provides means to request timeouts, intervals, and animation frames, with
|
|
132
|
+
* additional support for clearing all requested actions.
|
|
133
|
+
*
|
|
134
|
+
* This interface describes a replacement for the `setTimeout`/`clearTimeout`,
|
|
135
|
+
* `setInterval`/`clearInterval` and `requestAnimationFrame`/`cancelAnimationFrame`
|
|
136
|
+
* APIs that supports the ability to easily clear all pending actions.
|
|
137
|
+
* @export
|
|
138
|
+
* @interface IScheduleActions
|
|
139
|
+
*/
|
|
140
|
+
export interface IScheduleActions {
|
|
141
|
+
/**
|
|
142
|
+
* Request a timeout.
|
|
143
|
+
*
|
|
144
|
+
* A timeout will wait for a given period and then invoke a given action.
|
|
145
|
+
* @param {() => void} action The action to be invoked when the timeout
|
|
146
|
+
* period is reached.
|
|
147
|
+
* @param {number} period The timeout period in milliseconds. The action
|
|
148
|
+
* will be invoked after this period has passed since the timeout was set.
|
|
149
|
+
* This value must be greater than or equal to zero.
|
|
150
|
+
* @param {boolean} [autoSchedule] Whether or not to set the timeout as soon
|
|
151
|
+
* as this call is made, or wait until `set` is explicitly called. Defaults
|
|
152
|
+
* to `true`.
|
|
153
|
+
* @param {boolean} [resolveOnClear] Whether or not the associated action
|
|
154
|
+
* will be invoked if it is still pending at the point the timeout is
|
|
155
|
+
* cleared. Defaults to `false`.
|
|
156
|
+
* @returns {ITimeout} A interface for manipulating the created timeout.
|
|
157
|
+
* @memberof IScheduleActions
|
|
158
|
+
*/
|
|
159
|
+
timeout(action: () => mixed, period: number, options?: Options): ITimeout;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Request an interval.
|
|
163
|
+
*
|
|
164
|
+
* An interval will invoke a given action each time the given period has
|
|
165
|
+
* passed until the interval is cleared.
|
|
166
|
+
* @param {() => void} action The action to be invoked when the interval
|
|
167
|
+
* period occurs.
|
|
168
|
+
* @param {number} period The interval period in milliseconds. The action
|
|
169
|
+
* will be invoked each time this period has passed since the interval was
|
|
170
|
+
* set or last occurred.
|
|
171
|
+
* This value must be greater than zero.
|
|
172
|
+
* @param {boolean} [autoSchedule] Whether or not to set the interval as soon
|
|
173
|
+
* as this call is made, or wait until `set` is explicitly called. Defaults
|
|
174
|
+
* to `true`.
|
|
175
|
+
* @param {boolean} [resolveOnClear] Whether or not the associated action
|
|
176
|
+
* will be invoked at the point the interval is cleared if the interval
|
|
177
|
+
* is running at that time. Defaults to `false`.
|
|
178
|
+
* @returns {IInterval} An interface for manipulating the created interval.
|
|
179
|
+
* @memberof IScheduleActions
|
|
180
|
+
*/
|
|
181
|
+
interval(action: () => mixed, period: number, options?: Options): IInterval;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Request an animation frame.
|
|
185
|
+
*
|
|
186
|
+
* An animation frame request tells the browser that you wish to perform an
|
|
187
|
+
* animation and requests that the browser call a specified function to
|
|
188
|
+
* update an animation before the next repaint.
|
|
189
|
+
* @param {DOMHighResTimeStamp} => void} action The action to be invoked before the repaint.
|
|
190
|
+
* @param {boolean} [autoSchedule] Whether or not to make the request as soon
|
|
191
|
+
* as this call is made, or wait until `set` is explicitly called. Defaults
|
|
192
|
+
* to `true`.
|
|
193
|
+
* @param {boolean} [resolveOnClear] Whether or not the associated action
|
|
194
|
+
* will be invoked at the point the request is cleared if it has not yet
|
|
195
|
+
* executed. Defaults to `false`.
|
|
196
|
+
* @returns {IAnimationFrame} An interface for manipulating the created
|
|
197
|
+
* request.
|
|
198
|
+
* @memberof IScheduleActions
|
|
199
|
+
*/
|
|
200
|
+
animationFrame(
|
|
201
|
+
action: (time: DOMHighResTimeStamp) => void,
|
|
202
|
+
options?: Options
|
|
203
|
+
): IAnimationFrame;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Clears all timeouts, intervals, and animation frame requests that were
|
|
207
|
+
* made with this scheduler.
|
|
208
|
+
* @memberof IScheduleActions
|
|
209
|
+
*/
|
|
210
|
+
clearAll(): void;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* A props object type that can be spread into the a componenet wrapped with
|
|
214
|
+
* the `withActionScheduler` higher order component.
|
|
215
|
+
*/
|
|
216
|
+
export type WithActionSchedulerProps = {
|
|
217
|
+
/**
|
|
218
|
+
* An instance of the `IScheduleActions` API to use for scheduling
|
|
219
|
+
* intervals, timeouts, and animation frame requests.
|
|
220
|
+
*/
|
|
221
|
+
schedule: IScheduleActions,
|
|
222
|
+
...
|
|
223
|
+
};
|
|
224
|
+
export type WithoutActionScheduler<TProps: { [key: string]: any }> = $Diff<
|
|
225
|
+
TProps,
|
|
226
|
+
{ schedule: any }
|
|
227
|
+
>;
|
|
228
|
+
/**
|
|
229
|
+
* Extends the given props with props that the `withActionScheduler` higher
|
|
230
|
+
* order component will inject.
|
|
231
|
+
*/
|
|
232
|
+
export type WithActionScheduler<TOwnProps: { [key: string]: any }> = {
|
|
233
|
+
...TOwnProps,
|
|
234
|
+
...WithActionSchedulerProps,
|
|
235
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@khanacademy/wonder-blocks-timing",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "2.1.
|
|
4
|
+
"version": "2.1.2",
|
|
5
5
|
"design": "v1",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"description": "",
|
|
10
10
|
"main": "dist/index.js",
|
|
11
11
|
"module": "dist/es/index.js",
|
|
12
|
-
"
|
|
12
|
+
"types": "dist/index.d.ts",
|
|
13
13
|
"scripts": {
|
|
14
14
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
15
15
|
},
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"react": "16.14.0"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"wb-dev-build-settings": "^0.7.
|
|
20
|
+
"wb-dev-build-settings": "^0.7.2"
|
|
21
21
|
},
|
|
22
22
|
"author": "",
|
|
23
23
|
"license": "MIT"
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
import {render} from "@testing-library/react";
|
|
4
3
|
|
|
@@ -32,7 +31,6 @@ describe("ActionSchedulerProvider", () => {
|
|
|
32
31
|
unmount();
|
|
33
32
|
|
|
34
33
|
// Assert
|
|
35
|
-
// $FlowIgnore[prop-missing]
|
|
36
34
|
expect(childrenMock.mock.calls[0][0].disable).toHaveBeenCalledTimes(1);
|
|
37
35
|
});
|
|
38
36
|
});
|
package/src/components/__tests__/{with-action-scheduler.test.js → with-action-scheduler.test.tsx}
RENAMED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
import {render, screen} from "@testing-library/react";
|
|
4
3
|
|
|
@@ -9,8 +8,9 @@ import type {WithActionSchedulerProps} from "../../util/types";
|
|
|
9
8
|
describe("withActionScheduler", () => {
|
|
10
9
|
it("should provide wrapped component with IScheduleActions instance", () => {
|
|
11
10
|
// Arrange
|
|
12
|
-
const Component = (props) =>
|
|
13
|
-
props.schedule != null ? "true" : "false"
|
|
11
|
+
const Component = (props: any) => (
|
|
12
|
+
<>{props.schedule != null ? "true" : "false"}</>
|
|
13
|
+
);
|
|
14
14
|
|
|
15
15
|
// Act
|
|
16
16
|
const WithScheduler = withActionScheduler(Component);
|
|
@@ -23,15 +23,15 @@ describe("withActionScheduler", () => {
|
|
|
23
23
|
it("should forward a ref", () => {
|
|
24
24
|
// Arrange
|
|
25
25
|
class Component extends React.Component<WithActionSchedulerProps> {
|
|
26
|
-
render(): React.
|
|
26
|
+
render(): React.ReactElement {
|
|
27
27
|
return <div>Hello, world!</div>;
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
const TestComponent = withActionScheduler(Component);
|
|
31
|
-
let ref:
|
|
31
|
+
let ref: unknown = null;
|
|
32
32
|
|
|
33
33
|
// Act
|
|
34
|
-
render(<TestComponent ref={(node) => (ref = node)} />);
|
|
34
|
+
render(<TestComponent ref={(node: any) => (ref = node)} />);
|
|
35
35
|
|
|
36
36
|
// Assert
|
|
37
37
|
expect(ref).toBeInstanceOf(Component);
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
import ActionScheduler from "../util/action-scheduler";
|
|
4
3
|
|
|
5
4
|
import type {IScheduleActions} from "../util/types";
|
|
6
5
|
|
|
7
|
-
type Props = {
|
|
6
|
+
type Props = {
|
|
8
7
|
/**
|
|
9
8
|
* A function that, when given an instance of `IScheduleActions` will
|
|
10
9
|
* render a `React.Node`.
|
|
11
10
|
*/
|
|
12
|
-
children: (IScheduleActions) => React.
|
|
13
|
-
|
|
11
|
+
children: (arg1: IScheduleActions) => React.ReactElement;
|
|
12
|
+
};
|
|
14
13
|
|
|
15
14
|
/**
|
|
16
15
|
* A provider component that passes our action scheduling API to its children
|
|
@@ -29,7 +28,7 @@ export default class ActionSchedulerProvider extends React.Component<Props> {
|
|
|
29
28
|
|
|
30
29
|
_actionScheduler: ActionScheduler = new ActionScheduler();
|
|
31
30
|
|
|
32
|
-
render(): React.
|
|
31
|
+
render(): React.ReactElement {
|
|
33
32
|
const {children} = this.props;
|
|
34
33
|
return children(this._actionScheduler);
|
|
35
34
|
}
|
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
|
|
4
3
|
import ActionSchedulerProvider from "./action-scheduler-provider";
|
|
5
4
|
|
|
6
|
-
import type {
|
|
7
|
-
IScheduleActions,
|
|
8
|
-
WithActionSchedulerProps,
|
|
9
|
-
WithActionScheduler,
|
|
10
|
-
} from "../util/types";
|
|
5
|
+
import type {IScheduleActions, WithActionSchedulerProps} from "../util/types";
|
|
11
6
|
|
|
12
|
-
type WithoutActionScheduler<T> =
|
|
7
|
+
type WithoutActionScheduler<T> = Omit<T, "schedule">;
|
|
13
8
|
|
|
14
9
|
/**
|
|
15
10
|
* A higher order component that attaches the given component to an
|
|
@@ -21,19 +16,16 @@ type WithoutActionScheduler<T> = $Exact<$Diff<T, WithActionSchedulerProps>>;
|
|
|
21
16
|
* these props use the `WithActionScheduler` type.
|
|
22
17
|
*/
|
|
23
18
|
export default function withActionScheduler<
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
>(
|
|
28
|
-
WrappedComponent: Component,
|
|
29
|
-
): React.AbstractComponent<
|
|
30
|
-
WithoutActionScheduler<React.ElementConfig<Component>>,
|
|
31
|
-
Instance,
|
|
32
|
-
> {
|
|
33
|
-
return React.forwardRef<_, Instance>((props, ref) => (
|
|
19
|
+
Props extends WithActionSchedulerProps,
|
|
20
|
+
>(WrappedComponent: React.ComponentType<Props>) {
|
|
21
|
+
return React.forwardRef((props: WithoutActionScheduler<Props>, ref) => (
|
|
34
22
|
<ActionSchedulerProvider>
|
|
35
23
|
{(schedule: IScheduleActions) => (
|
|
36
|
-
<WrappedComponent
|
|
24
|
+
<WrappedComponent
|
|
25
|
+
{...(props as Props)}
|
|
26
|
+
ref={ref}
|
|
27
|
+
schedule={schedule}
|
|
28
|
+
/>
|
|
37
29
|
)}
|
|
38
30
|
</ActionSchedulerProvider>
|
|
39
31
|
));
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import {renderHook} from "@testing-library/react-hooks";
|
|
3
2
|
|
|
4
3
|
import {useInterval} from "../use-interval";
|
|
@@ -38,7 +37,7 @@ describe("useTimeout", () => {
|
|
|
38
37
|
|
|
39
38
|
// Act
|
|
40
39
|
const {rerender} = renderHook(
|
|
41
|
-
({active}) => useInterval(action, 500, active),
|
|
40
|
+
({active}: any) => useInterval(action, 500, active),
|
|
42
41
|
{initialProps: {active: false}},
|
|
43
42
|
);
|
|
44
43
|
rerender({active: true});
|
|
@@ -52,7 +51,7 @@ describe("useTimeout", () => {
|
|
|
52
51
|
// Arrange
|
|
53
52
|
const action = jest.fn();
|
|
54
53
|
const {rerender} = renderHook(
|
|
55
|
-
({active}) => useInterval(action, 500, active),
|
|
54
|
+
({active}: any) => useInterval(action, 500, active),
|
|
56
55
|
{initialProps: {active: true}},
|
|
57
56
|
);
|
|
58
57
|
|
|
@@ -70,7 +69,7 @@ describe("useTimeout", () => {
|
|
|
70
69
|
|
|
71
70
|
// Act
|
|
72
71
|
const {rerender} = renderHook(
|
|
73
|
-
({timeoutMs}) => useInterval(action, timeoutMs, true),
|
|
72
|
+
({timeoutMs}: any) => useInterval(action, timeoutMs, true),
|
|
74
73
|
{initialProps: {timeoutMs: 500}},
|
|
75
74
|
);
|
|
76
75
|
rerender({timeoutMs: 1000});
|
|
@@ -90,7 +89,7 @@ describe("useTimeout", () => {
|
|
|
90
89
|
|
|
91
90
|
// Act
|
|
92
91
|
const {rerender} = renderHook(
|
|
93
|
-
({action}) => useInterval(action, 500, true),
|
|
92
|
+
({action}: any) => useInterval(action, 500, true),
|
|
94
93
|
{initialProps: {action: action1}},
|
|
95
94
|
);
|
|
96
95
|
// NOTE: For some reason setTimeout is called twice by the time we get
|
|
@@ -111,7 +110,7 @@ describe("useTimeout", () => {
|
|
|
111
110
|
|
|
112
111
|
// Act
|
|
113
112
|
const {rerender} = renderHook(
|
|
114
|
-
({action}) => useInterval(action, 500, true),
|
|
113
|
+
({action}: any) => useInterval(action, 500, true),
|
|
115
114
|
{initialProps: {action: action1}},
|
|
116
115
|
);
|
|
117
116
|
rerender({action: action2});
|
package/src/hooks/__tests__/{use-scheduled-interval.test.js → use-scheduled-interval.test.ts}
RENAMED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import {renderHook, act} from "@testing-library/react-hooks";
|
|
3
2
|
import {SchedulePolicy, ClearPolicy} from "../../util/policies";
|
|
4
3
|
|
|
@@ -18,7 +17,7 @@ describe("useScheduledInterval", () => {
|
|
|
18
17
|
|
|
19
18
|
// Act
|
|
20
19
|
const {result} = renderHook(() =>
|
|
21
|
-
useScheduledInterval(
|
|
20
|
+
useScheduledInterval(null as any, 1000),
|
|
22
21
|
);
|
|
23
22
|
|
|
24
23
|
// Assert
|
|
@@ -66,7 +65,7 @@ describe("useScheduledInterval", () => {
|
|
|
66
65
|
const action1 = jest.fn();
|
|
67
66
|
const action2 = jest.fn();
|
|
68
67
|
const {rerender} = renderHook(
|
|
69
|
-
({action}) => useScheduledInterval(action, 500),
|
|
68
|
+
({action}: any) => useScheduledInterval(action, 500),
|
|
70
69
|
{
|
|
71
70
|
initialProps: {action: action1},
|
|
72
71
|
},
|
|
@@ -86,7 +85,7 @@ describe("useScheduledInterval", () => {
|
|
|
86
85
|
const action1 = jest.fn();
|
|
87
86
|
const action2 = jest.fn();
|
|
88
87
|
const {rerender} = renderHook(
|
|
89
|
-
({action}) => useScheduledInterval(action, 500),
|
|
88
|
+
({action}: any) => useScheduledInterval(action, 500),
|
|
90
89
|
{
|
|
91
90
|
initialProps: {action: action1},
|
|
92
91
|
},
|
|
@@ -103,7 +102,7 @@ describe("useScheduledInterval", () => {
|
|
|
103
102
|
// Arrange
|
|
104
103
|
const action = jest.fn();
|
|
105
104
|
const {rerender} = renderHook(
|
|
106
|
-
({intervalMs}) => useScheduledInterval(action, intervalMs),
|
|
105
|
+
({intervalMs}: any) => useScheduledInterval(action, intervalMs),
|
|
107
106
|
{
|
|
108
107
|
initialProps: {intervalMs: 500},
|
|
109
108
|
},
|
|
@@ -121,7 +120,7 @@ describe("useScheduledInterval", () => {
|
|
|
121
120
|
// Arrange
|
|
122
121
|
const intervalSpy = jest.spyOn(global, "setInterval");
|
|
123
122
|
const {rerender} = renderHook(
|
|
124
|
-
({intervalMs}) => useScheduledInterval(() => {}, intervalMs),
|
|
123
|
+
({intervalMs}: any) => useScheduledInterval(() => {}, intervalMs),
|
|
125
124
|
{
|
|
126
125
|
initialProps: {intervalMs: 500},
|
|
127
126
|
},
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import {renderHook, act} from "@testing-library/react-hooks";
|
|
3
2
|
import {SchedulePolicy, ClearPolicy} from "../../util/policies";
|
|
4
3
|
|
|
@@ -18,7 +17,7 @@ describe("useScheduledTimeout", () => {
|
|
|
18
17
|
|
|
19
18
|
// Act
|
|
20
19
|
const {result} = renderHook(() =>
|
|
21
|
-
useScheduledTimeout(
|
|
20
|
+
useScheduledTimeout(null as any, 1000),
|
|
22
21
|
);
|
|
23
22
|
|
|
24
23
|
// Assert
|
|
@@ -81,7 +80,7 @@ describe("useScheduledTimeout", () => {
|
|
|
81
80
|
const action1 = jest.fn();
|
|
82
81
|
const action2 = jest.fn();
|
|
83
82
|
const {rerender} = renderHook(
|
|
84
|
-
({action}) => useScheduledTimeout(action, 500),
|
|
83
|
+
({action}: any) => useScheduledTimeout(action, 500),
|
|
85
84
|
{
|
|
86
85
|
initialProps: {action: action1},
|
|
87
86
|
},
|
|
@@ -101,7 +100,7 @@ describe("useScheduledTimeout", () => {
|
|
|
101
100
|
const action1 = jest.fn();
|
|
102
101
|
const action2 = jest.fn();
|
|
103
102
|
const {rerender} = renderHook(
|
|
104
|
-
({action}) => useScheduledTimeout(action, 500),
|
|
103
|
+
({action}: any) => useScheduledTimeout(action, 500),
|
|
105
104
|
{
|
|
106
105
|
initialProps: {action: action1},
|
|
107
106
|
},
|
|
@@ -122,7 +121,7 @@ describe("useScheduledTimeout", () => {
|
|
|
122
121
|
// Arrange
|
|
123
122
|
const action = jest.fn();
|
|
124
123
|
const {rerender} = renderHook(
|
|
125
|
-
({timeoutMs}) => useScheduledTimeout(action, timeoutMs),
|
|
124
|
+
({timeoutMs}: any) => useScheduledTimeout(action, timeoutMs),
|
|
126
125
|
{
|
|
127
126
|
initialProps: {timeoutMs: 500},
|
|
128
127
|
},
|
|
@@ -140,7 +139,7 @@ describe("useScheduledTimeout", () => {
|
|
|
140
139
|
// Arrange
|
|
141
140
|
const timeoutSpy = jest.spyOn(global, "setTimeout");
|
|
142
141
|
const {rerender} = renderHook(
|
|
143
|
-
({timeoutMs}) => useScheduledTimeout(() => {}, timeoutMs),
|
|
142
|
+
({timeoutMs}: any) => useScheduledTimeout(() => {}, timeoutMs),
|
|
144
143
|
{
|
|
145
144
|
initialProps: {timeoutMs: 500},
|
|
146
145
|
},
|
|
@@ -211,7 +210,7 @@ describe("useScheduledTimeout", () => {
|
|
|
211
210
|
// Arrange
|
|
212
211
|
const action = jest.fn();
|
|
213
212
|
const {rerender} = renderHook(
|
|
214
|
-
({timeoutMs}) => useScheduledTimeout(action, timeoutMs),
|
|
213
|
+
({timeoutMs}: any) => useScheduledTimeout(action, timeoutMs),
|
|
215
214
|
{
|
|
216
215
|
initialProps: {timeoutMs: 1000},
|
|
217
216
|
},
|
|
@@ -236,7 +235,7 @@ describe("useScheduledTimeout", () => {
|
|
|
236
235
|
// Arrange
|
|
237
236
|
const action = jest.fn();
|
|
238
237
|
const {rerender} = renderHook(
|
|
239
|
-
({timeoutMs}) => useScheduledTimeout(action, timeoutMs),
|
|
238
|
+
({timeoutMs}: any) => useScheduledTimeout(action, timeoutMs),
|
|
240
239
|
{
|
|
241
240
|
initialProps: {timeoutMs: 1000},
|
|
242
241
|
},
|
|
@@ -257,7 +256,7 @@ describe("useScheduledTimeout", () => {
|
|
|
257
256
|
const action1 = jest.fn();
|
|
258
257
|
const action2 = jest.fn();
|
|
259
258
|
const {rerender} = renderHook(
|
|
260
|
-
({action}) => useScheduledTimeout(action, 1000),
|
|
259
|
+
({action}: any) => useScheduledTimeout(action, 1000),
|
|
261
260
|
{
|
|
262
261
|
initialProps: {action: action1},
|
|
263
262
|
},
|
|
@@ -278,7 +277,7 @@ describe("useScheduledTimeout", () => {
|
|
|
278
277
|
const action1 = jest.fn();
|
|
279
278
|
const action2 = jest.fn();
|
|
280
279
|
const {rerender} = renderHook(
|
|
281
|
-
({action}) => useScheduledTimeout(action, 1000),
|
|
280
|
+
({action}: any) => useScheduledTimeout(action, 1000),
|
|
282
281
|
{
|
|
283
282
|
initialProps: {action: action1},
|
|
284
283
|
},
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import {renderHook} from "@testing-library/react-hooks";
|
|
3
2
|
|
|
4
3
|
import {useTimeout} from "../use-timeout";
|
|
@@ -48,7 +47,7 @@ describe("useTimeout", () => {
|
|
|
48
47
|
// Arrange
|
|
49
48
|
const action = jest.fn();
|
|
50
49
|
const {rerender} = renderHook(
|
|
51
|
-
({active}) => useTimeout(action, 500, active),
|
|
50
|
+
({active}: any) => useTimeout(action, 500, active),
|
|
52
51
|
{initialProps: {active: false}},
|
|
53
52
|
);
|
|
54
53
|
|
|
@@ -64,7 +63,7 @@ describe("useTimeout", () => {
|
|
|
64
63
|
// Arrange
|
|
65
64
|
const action = jest.fn();
|
|
66
65
|
const {rerender} = renderHook(
|
|
67
|
-
({active}) => useTimeout(action, 500, active),
|
|
66
|
+
({active}: any) => useTimeout(action, 500, active),
|
|
68
67
|
{initialProps: {active: true}},
|
|
69
68
|
);
|
|
70
69
|
|
|
@@ -82,7 +81,7 @@ describe("useTimeout", () => {
|
|
|
82
81
|
|
|
83
82
|
// Act
|
|
84
83
|
const {rerender} = renderHook(
|
|
85
|
-
({timeoutMs}) => useTimeout(action, timeoutMs, true),
|
|
84
|
+
({timeoutMs}: any) => useTimeout(action, timeoutMs, true),
|
|
86
85
|
{initialProps: {timeoutMs: 500}},
|
|
87
86
|
);
|
|
88
87
|
rerender({timeoutMs: 1000});
|
|
@@ -102,7 +101,7 @@ describe("useTimeout", () => {
|
|
|
102
101
|
|
|
103
102
|
// Act
|
|
104
103
|
const {rerender} = renderHook(
|
|
105
|
-
({action}) => useTimeout(action, 500, true),
|
|
104
|
+
({action}: any) => useTimeout(action, 500, true),
|
|
106
105
|
{initialProps: {action: action1}},
|
|
107
106
|
);
|
|
108
107
|
// NOTE: For some reason setTimeout is called twice by the time we get
|
|
@@ -123,7 +122,7 @@ describe("useTimeout", () => {
|
|
|
123
122
|
|
|
124
123
|
// Act
|
|
125
124
|
const {rerender} = renderHook(
|
|
126
|
-
({action}) => useTimeout(action, 500, true),
|
|
125
|
+
({action}: any) => useTimeout(action, 500, true),
|
|
127
126
|
{initialProps: {action: action1}},
|
|
128
127
|
);
|
|
129
128
|
rerender({action: action2});
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import {useEffect, useRef} from "react";
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -11,7 +10,11 @@ import {useEffect, useRef} from "react";
|
|
|
11
10
|
*
|
|
12
11
|
* @returns {{current: T}}
|
|
13
12
|
*/
|
|
14
|
-
export const useUpdatingRef = <T>(
|
|
13
|
+
export const useUpdatingRef = <T>(
|
|
14
|
+
value: T,
|
|
15
|
+
): {
|
|
16
|
+
current: T;
|
|
17
|
+
} => {
|
|
15
18
|
const ref = useRef<T>(value);
|
|
16
19
|
useEffect(() => {
|
|
17
20
|
ref.current = value;
|
|
@@ -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 useInterval(
|
|
14
|
-
action: () =>
|
|
13
|
+
action: () => unknown,
|
|
15
14
|
intervalMs: number,
|
|
16
15
|
active: boolean,
|
|
17
16
|
) {
|
|
@@ -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 {useInterval} from "./use-interval";
|
|
12
11
|
|
|
13
12
|
export function useScheduledInterval(
|
|
14
|
-
action: () =>
|
|
13
|
+
action: () => unknown,
|
|
15
14
|
intervalMs: number,
|
|
16
15
|
options?: Options,
|
|
17
16
|
): IInterval {
|
|
@@ -69,5 +68,6 @@ export function useScheduledInterval(
|
|
|
69
68
|
|
|
70
69
|
useInterval(action, intervalMs, isSet);
|
|
71
70
|
|
|
71
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'boolean' is not assignable to type '() => boolean'.
|
|
72
72
|
return {isSet, set, clear};
|
|
73
73
|
}
|