@khanacademy/wonder-blocks-timing 5.0.1 → 6.0.0

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.
@@ -1,102 +0,0 @@
1
- import {SchedulePolicy, ClearPolicy} from "./policies";
2
-
3
- import type {IInterval} from "./types";
4
-
5
- /**
6
- * Encapsulates everything associated with calling setInterval/clearInterval,
7
- * and managing the lifecycle of that interval. This includes the ability to
8
- * cancel the interval, and knowing if the interval is active.
9
- *
10
- * @export
11
- * @class Interval
12
- * @implements {IInterval}
13
- */
14
- export default class Interval implements IInterval {
15
- _intervalId: ReturnType<typeof setInterval> | null | undefined;
16
- _action: () => unknown;
17
- _intervalMs: number;
18
-
19
- /**
20
- * Creates an interval that will invoke the given action after
21
- * the given period. The interval does not start until set is called.
22
- *
23
- * @param action The action to be invoked each time the
24
- * interval period has passed.
25
- * @param intervalMs The interval period.
26
- * @param [schedulePolicy] When SchedulePolicy.Immediately,
27
- * the interval is set immediately on instantiation; otherwise, `set` must be
28
- * called to set the interval.
29
- * Defaults to `SchedulePolicy.Immediately`.
30
- * @memberof Interval
31
- */
32
- constructor(
33
- action: () => unknown,
34
- intervalMs: number,
35
- schedulePolicy: SchedulePolicy = SchedulePolicy.Immediately,
36
- ) {
37
- if (typeof action !== "function") {
38
- throw new Error("Action must be a function");
39
- }
40
-
41
- if (intervalMs < 1) {
42
- throw new Error("Interval period must be >= 1");
43
- }
44
-
45
- this._action = action;
46
- this._intervalMs = intervalMs;
47
-
48
- if (schedulePolicy === SchedulePolicy.Immediately) {
49
- this.set();
50
- }
51
- }
52
-
53
- /**
54
- * Determine if the interval is active or not.
55
- *
56
- * @returns true if the interval is active, otherwise false.
57
- * @memberof Interval
58
- */
59
- get isSet(): boolean {
60
- return this._intervalId != null;
61
- }
62
-
63
- /**
64
- * Activate the interval.
65
- *
66
- * If the interval is active, this cancels that interval and starts the
67
- * interval afresh. If the interval is not active, this starts it.
68
- *
69
- * @memberof Interval
70
- */
71
- set(): void {
72
- if (this.isSet) {
73
- this.clear(ClearPolicy.Cancel);
74
- }
75
- this._intervalId = setInterval(() => this._action(), this._intervalMs);
76
- }
77
-
78
- /**
79
- * Clear the active interval.
80
- *
81
- * If the interval is active, this cancels that interval. If no interval is
82
- * pending, this does nothing.
83
- *
84
- * @param [policy] When ClearPolicy.Resolve, if the request
85
- * was set when called, the request action is invoked after cancelling
86
- * the request; otherwise, the pending action is cancelled.
87
- * Defaults to `ClearPolicy.Cancel`.
88
- *
89
- * @memberof Interval
90
- */
91
- clear(policy: ClearPolicy = ClearPolicy.Cancel): void {
92
- const intervalId = this._intervalId;
93
- this._intervalId = null;
94
- if (intervalId == null) {
95
- return;
96
- }
97
- clearInterval(intervalId);
98
- if (policy === ClearPolicy.Resolve) {
99
- this._action();
100
- }
101
- }
102
- }
@@ -1,14 +0,0 @@
1
- export enum SchedulePolicy {
2
- Immediately = "schedule-immediately",
3
- OnDemand = "schedule-on-demand",
4
- }
5
-
6
- export enum ClearPolicy {
7
- Resolve = "resolve-on-clear",
8
- Cancel = "cancel-on-clear",
9
- }
10
-
11
- export enum ActionPolicy {
12
- Reset = "reset",
13
- Passive = "passive",
14
- }
@@ -1,108 +0,0 @@
1
- import {SchedulePolicy, ClearPolicy} from "./policies";
2
-
3
- import type {ITimeout} from "./types";
4
-
5
- /**
6
- * Encapsulates everything associated with calling setTimeout/clearTimeout, and
7
- * managing the lifecycle of that timer, including the ability to resolve or
8
- * cancel a pending timeout action.
9
- *
10
- * @export
11
- * @class Timeout
12
- * @implements {ITimeout}
13
- */
14
- export default class Timeout implements ITimeout {
15
- _timeoutId: ReturnType<typeof setTimeout> | null | undefined;
16
- _action: () => unknown;
17
- _timeoutMs: number;
18
-
19
- /**
20
- * Creates a timeout that will invoke the given action after
21
- * the given period. The timeout does not start until set is called.
22
- *
23
- * @param action The action to be invoked when the timeout
24
- * period has passed.
25
- * @param timeoutMs The timeout period.
26
- * @param [schedulePolicy] When SchedulePolicy.Immediately,
27
- * the timer is set immediately on instantiation; otherwise, `set` must be
28
- * called to set the timeout.
29
- * Defaults to `SchedulePolicy.Immediately`.
30
- * @memberof Timeout
31
- */
32
- constructor(
33
- action: () => unknown,
34
- timeoutMs: number,
35
- schedulePolicy: SchedulePolicy = SchedulePolicy.Immediately,
36
- ) {
37
- if (typeof action !== "function") {
38
- throw new Error("Action must be a function");
39
- }
40
-
41
- if (timeoutMs < 0) {
42
- throw new Error("Timeout period must be >= 0");
43
- }
44
-
45
- this._action = action;
46
- this._timeoutMs = timeoutMs;
47
-
48
- if (schedulePolicy === SchedulePolicy.Immediately) {
49
- this.set();
50
- }
51
- }
52
-
53
- /**
54
- * Determine if the timeout is set or not.
55
- *
56
- * @returns true if the timeout is set (aka pending), otherwise
57
- * false.
58
- * @memberof Timeout
59
- */
60
- get isSet(): boolean {
61
- return this._timeoutId != null;
62
- }
63
-
64
- /**
65
- * Set the timeout.
66
- *
67
- * If the timeout is pending, this cancels that pending timeout and
68
- * sets the timeout afresh. If the timeout is not pending, this
69
- * sets a new timeout.
70
- *
71
- * @memberof Timeout
72
- */
73
- set(): void {
74
- if (this.isSet) {
75
- this.clear(ClearPolicy.Cancel);
76
- }
77
- this._timeoutId = setTimeout(
78
- () => this.clear(ClearPolicy.Resolve),
79
- this._timeoutMs,
80
- );
81
- }
82
-
83
- /**
84
- * Clear the set timeout.
85
- *
86
- * If the timeout is pending, this cancels that pending timeout without
87
- * invoking the action. If no timeout is pending, this does nothing.
88
- *
89
- * @param [policy] When ClearPolicy.Resolve, if the request
90
- * was set when called, the request action is invoked after cancelling
91
- * the request; otherwise, the pending action is cancelled.
92
- * Defaults to `ClearPolicy.Cancel`.
93
- *
94
- * @returns {void}
95
- * @memberof Timeout
96
- */
97
- clear(policy: ClearPolicy = ClearPolicy.Cancel): void {
98
- const timeoutId = this._timeoutId;
99
- this._timeoutId = null;
100
- if (timeoutId == null) {
101
- return;
102
- }
103
- clearTimeout(timeoutId);
104
- if (policy === ClearPolicy.Resolve) {
105
- this._action();
106
- }
107
- }
108
- }
package/src/util/types.ts DELETED
@@ -1,256 +0,0 @@
1
- import * as Policies from "./policies";
2
-
3
- /**
4
- * Encapsulates everything associated with calling setTimeout/clearTimeout, and
5
- * managing the lifecycle of that timer, including the ability to resolve or
6
- * cancel a pending timeout action.
7
- *
8
- * @export
9
- * @interface ITimeout
10
- */
11
- export interface ITimeout {
12
- /**
13
- * Determine if the timeout is set or not.
14
- *
15
- * @returns {boolean} true if the timeout is set (aka pending), otherwise
16
- * false.
17
- * @memberof ITimeout
18
- */
19
- get isSet(): boolean;
20
- /**
21
- * Set the timeout.
22
- *
23
- * If the timeout is pending, this cancels that pending timeout and
24
- * starts the timeout afresh. If the timeout is not pending, this
25
- * starts the timeout.
26
- *
27
- * @memberof ITimeout
28
- */
29
- set(): void;
30
- /**
31
- * Clear the set timeout.
32
- *
33
- * If the timeout is pending, this cancels that pending timeout. If no
34
- * timeout is pending, this does nothing.
35
- *
36
- * @param [policy] When ClearPolicy.Resolve, if the request
37
- * was set when called, the request action is invoked after cancelling
38
- * the request; otherwise, the pending action is cancelled.
39
- * Defaults to `ClearPolicy.Cancel`.
40
- *
41
- * @memberof ITimeout
42
- */
43
- clear(policy?: Policies.ClearPolicy): void;
44
- }
45
-
46
- /**
47
- * Encapsulates everything associated with calling setInterval/clearInterval,
48
- * and managing the lifecycle of that interval, including the ability to resolve
49
- * or cancel an active interval.
50
- *
51
- * @export
52
- * @interface IInterval
53
- */
54
- export interface IInterval {
55
- /**
56
- * Determine if the interval is active or not.
57
- *
58
- * @returns {boolean} true if the interval is active, otherwise false.
59
- * @memberof IInterval
60
- */
61
- get isSet(): boolean;
62
- /**
63
- * Set the interval.
64
- *
65
- * If the interval is active, this cancels that interval and restarts it
66
- * afresh. If the interval is not active, this starts the interval.
67
- *
68
- * @memberof IInterval
69
- */
70
- set(): void;
71
- /**
72
- * Clear the active interval.
73
- *
74
- * If the interval is active, this cancels that interval. If the interval
75
- * is not active, this does nothing.
76
- *
77
- * @param [policy] When ClearPolicy.Resolve, if the request
78
- * was set when called, the request action is invoked after cancelling
79
- * the request; otherwise, the pending action is cancelled.
80
- * Defaults to `ClearPolicy.Cancel`.
81
- *
82
- * @memberof IInterval
83
- */
84
- clear(policy?: Policies.ClearPolicy): void;
85
- }
86
-
87
- /**
88
- * Encapsulates everything associated with calling requestAnimationFrame/
89
- * cancelAnimationFrame, and managing the lifecycle of that request, including
90
- * the ability to resolve or cancel a pending request.
91
- *
92
- * @export
93
- * @interface IAnimationFrame
94
- */
95
- export interface IAnimationFrame {
96
- /**
97
- * Determine if the request is set or not.
98
- *
99
- * @returns {boolean} true if the request is set (aka pending), otherwise
100
- * false.
101
- * @memberof IAnimationFrame
102
- */
103
- get isSet(): boolean;
104
- /**
105
- * Set the request.
106
- *
107
- * If the request is pending, this cancels that pending request and
108
- * starts a request afresh. If the request is not pending, this
109
- * starts the request.
110
- *
111
- * @memberof IAnimationFrame
112
- */
113
- set(): void;
114
- /**
115
- * Clear the set request.
116
- *
117
- * If the request is pending, this cancels that pending request. If no
118
- * request is pending, this does nothing.
119
- *
120
- * @param [policy] When ClearPolicy.Resolve, if the request
121
- * was set when called, the request action is invoked after cancelling
122
- * the request; otherwise, the pending action is cancelled.
123
- * Defaults to `ClearPolicy.Cancel`.
124
- *
125
- * @memberof IAnimationFrame
126
- */
127
- clear(policy?: Policies.ClearPolicy): void;
128
- }
129
-
130
- /**
131
- * Options for the scheduling APIs.
132
- */
133
- export type Options = {
134
- schedulePolicy?: Policies.SchedulePolicy;
135
- clearPolicy?: Policies.ClearPolicy;
136
- };
137
-
138
- /**
139
- * Options for the hook variants of our scheduling APIs.
140
- */
141
- export type HookOptions = Options & {
142
- actionPolicy?: Policies.ActionPolicy;
143
- };
144
-
145
- /**
146
- * Provides means to request timeouts, intervals, and animation frames, with
147
- * additional support for clearing all requested actions.
148
- *
149
- * This interface describes a replacement for the `setTimeout`/`clearTimeout`,
150
- * `setInterval`/`clearInterval` and `requestAnimationFrame`/`cancelAnimationFrame`
151
- * APIs that supports the ability to easily clear all pending actions.
152
- *
153
- * @export
154
- * @interface IScheduleActions
155
- */
156
- export interface IScheduleActions {
157
- /**
158
- * Request a timeout.
159
- *
160
- * A timeout will wait for a given period and then invoke a given action.
161
- *
162
- * @param {() => void} action The action to be invoked when the timeout
163
- * period is reached.
164
- * @param {number} period The timeout period in milliseconds. The action
165
- * will be invoked after this period has passed since the timeout was set.
166
- * This value must be greater than or equal to zero.
167
- * @param {boolean} [autoSchedule] Whether or not to set the timeout as soon
168
- * as this call is made, or wait until `set` is explicitly called. Defaults
169
- * to `true`.
170
- * @param {boolean} [resolveOnClear] Whether or not the associated action
171
- * will be invoked if it is still pending at the point the timeout is
172
- * cleared. Defaults to `false`.
173
- * @returns {ITimeout} A interface for manipulating the created timeout.
174
- * @memberof IScheduleActions
175
- */
176
- timeout(action: () => unknown, period: number, options?: Options): ITimeout;
177
- /**
178
- * Request an interval.
179
- *
180
- * An interval will invoke a given action each time the given period has
181
- * passed until the interval is cleared.
182
- *
183
- * @param {() => void} action The action to be invoked when the interval
184
- * period occurs.
185
- * @param {number} period The interval period in milliseconds. The action
186
- * will be invoked each time this period has passed since the interval was
187
- * set or last occurred.
188
- * This value must be greater than zero.
189
- * @param {boolean} [autoSchedule] Whether or not to set the interval as soon
190
- * as this call is made, or wait until `set` is explicitly called. Defaults
191
- * to `true`.
192
- * @param {boolean} [resolveOnClear] Whether or not the associated action
193
- * will be invoked at the point the interval is cleared if the interval
194
- * is running at that time. Defaults to `false`.
195
- * @returns {IInterval} An interface for manipulating the created interval.
196
- * @memberof IScheduleActions
197
- */
198
- interval(
199
- action: () => unknown,
200
- period: number,
201
- options?: Options,
202
- ): IInterval;
203
- /**
204
- * Request an animation frame.
205
- *
206
- * An animation frame request tells the browser that you wish to perform an
207
- * animation and requests that the browser call a specified function to
208
- * update an animation before the next repaint.
209
- *
210
- * @param {(time: DOMHighResTimeStamp) => void} action The action to be invoked before the repaint.
211
- * @param {boolean} [autoSchedule] Whether or not to make the request as soon
212
- * as this call is made, or wait until `set` is explicitly called. Defaults
213
- * to `true`.
214
- * @param {boolean} [resolveOnClear] Whether or not the associated action
215
- * will be invoked at the point the request is cleared if it has not yet
216
- * executed. Defaults to `false`.
217
- * @returns {IAnimationFrame} An interface for manipulating the created
218
- * request.
219
- * @memberof IScheduleActions
220
- */
221
- animationFrame(
222
- action: (time: DOMHighResTimeStamp) => void,
223
- options?: Options,
224
- ): IAnimationFrame;
225
- /**
226
- * Clears all timeouts, intervals, and animation frame requests that were
227
- * made with this scheduler.
228
- *
229
- * @memberof IScheduleActions
230
- */
231
- clearAll(): void;
232
- }
233
-
234
- /**
235
- * A props object type that can be spread into the a componenet wrapped with
236
- * the `withActionScheduler` higher order component.
237
- */
238
- export type WithActionSchedulerProps = Readonly<{
239
- /**
240
- * An instance of the `IScheduleActions` API to use for scheduling
241
- * intervals, timeouts, and animation frame requests.
242
- */
243
- schedule: IScheduleActions;
244
- }>;
245
-
246
- export type WithoutActionScheduler<TProps extends object> = Omit<
247
- TProps,
248
- "schedule"
249
- >;
250
-
251
- /**
252
- * Extends the given props with props that the `withActionScheduler` higher
253
- * order component will inject.
254
- */
255
- export type WithActionScheduler<TOwnProps extends object> = TOwnProps &
256
- WithActionSchedulerProps;
@@ -1,72 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unused-vars */
2
- /**
3
- * This file ensures that our TypeScript types are working right.
4
- */
5
- import * as React from "react";
6
- import withActionScheduler from "../components/with-action-scheduler";
7
-
8
- import type {WithActionSchedulerProps} from "./types";
9
-
10
- /**
11
- * Test WithActionScheduler and withActionScheduler usage.
12
- */
13
- /**
14
- * Case 1: Correct usage
15
- * Given a react component with action scheduler props, we receive an
16
- * abstract component of `OwnProps1`.
17
- */
18
- type Props1 = {
19
- test: string;
20
- } & WithActionSchedulerProps;
21
-
22
- const InnerComponent1 = (props: Props1): React.ReactElement => (
23
- <>{props.test}</>
24
- );
25
-
26
- const HOCComponent1 = withActionScheduler(InnerComponent1);
27
-
28
- /**
29
- * Case 2: Incorrect Usage
30
- * The withActionScheduler call is returning a component of type OwnProps2; the
31
- * variable assigned however is incorrectly specified as Props2.
32
- */
33
- type Props2 = {
34
- test: string;
35
- } & WithActionSchedulerProps;
36
-
37
- const InnerComponent2 = (props: Props2): React.ReactElement => (
38
- <>{props.test}</>
39
- );
40
-
41
- /**
42
- * Cannot assign `withActionScheduler(...)` to `HOCComponent2` because property
43
- * `schedule` is missing in `OwnProps2` [1] but exists in `WithActionScheduler`
44
- * [2] in type argument `Config` [3].
45
- *
46
- * $ExpectError
47
- */
48
- const HOCComponent2 = withActionScheduler(InnerComponent2);
49
-
50
- /**
51
- * Case 3: Incorrect Usage
52
- * The withActionScheduler call is being passed a component that isn't set up
53
- * to be used with it as it doesn't have all the props necessary.
54
- */
55
- type Props3 = {
56
- test: string;
57
- } & WithActionSchedulerProps;
58
-
59
- const InnerComponent3 = (props: Props3): React.ReactElement => (
60
- <>{props.test}</>
61
- );
62
-
63
- const HOCComponent3 = withActionScheduler(
64
- /**
65
- * Cannot call `withActionScheduler` with `InnerComponent3` bound to
66
- * `Component` because property `schedule` is missing in `OwnProps3` [1]
67
- * but exists in `WithActionScheduler` [2] in type argument `Config` [3].
68
- *
69
- * $ExpectError
70
- */
71
- InnerComponent3,
72
- );
@@ -1,9 +0,0 @@
1
- {
2
- "exclude": ["dist"],
3
- "extends": "../tsconfig-shared.json",
4
- "compilerOptions": {
5
- "outDir": "./dist",
6
- "rootDir": "src"
7
- },
8
- "references": []
9
- }