@khanacademy/wonder-blocks-timing 4.0.1 → 5.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.
- package/CHANGELOG.md +18 -0
- package/dist/components/with-action-scheduler.d.ts +4 -1
- package/dist/es/index.js +93 -105
- package/dist/hooks/use-interval.d.ts +27 -5
- package/dist/hooks/use-timeout.d.ts +27 -5
- package/dist/index.d.ts +2 -5
- package/dist/index.js +92 -105
- package/dist/util/animation-frame.d.ts +6 -6
- package/dist/util/interval.d.ts +8 -8
- package/dist/util/policies.d.ts +12 -8
- package/dist/util/timeout.d.ts +8 -7
- package/dist/util/types.d.ts +18 -10
- package/package.json +3 -3
- package/src/components/with-action-scheduler.tsx +9 -1
- package/src/hooks/__tests__/use-interval.test.ts +453 -56
- package/src/hooks/__tests__/use-timeout.test.ts +412 -58
- package/src/hooks/use-interval.ts +90 -25
- package/src/hooks/use-timeout.ts +90 -25
- package/src/index.ts +4 -14
- package/src/util/__tests__/animation-frame.test.ts +9 -10
- package/src/util/animation-frame.ts +12 -16
- package/src/util/interval.ts +13 -18
- package/src/util/policies.ts +13 -8
- package/src/util/timeout.ts +14 -18
- package/src/util/types.ts +19 -11
- package/tsconfig-build.json +2 -4
- package/tsconfig-build.tsbuildinfo +1 -1
- package/dist/components/action-scheduler-provider.js.flow +0 -33
- package/dist/components/with-action-scheduler.js.flow +0 -22
- package/dist/hooks/internal/use-updating-ref.d.ts +0 -13
- package/dist/hooks/internal/use-updating-ref.js.flow +0 -20
- package/dist/hooks/use-interval.js.flow +0 -17
- package/dist/hooks/use-scheduled-interval.d.ts +0 -2
- package/dist/hooks/use-scheduled-interval.js.flow +0 -12
- package/dist/hooks/use-scheduled-timeout.d.ts +0 -2
- package/dist/hooks/use-scheduled-timeout.js.flow +0 -12
- package/dist/hooks/use-timeout.js.flow +0 -17
- package/dist/index.js.flow +0 -31
- package/dist/util/action-scheduler.js.flow +0 -38
- package/dist/util/animation-frame.js.flow +0 -70
- package/dist/util/interval.js.flow +0 -69
- package/dist/util/policies.js.flow +0 -14
- package/dist/util/timeout.js.flow +0 -71
- package/dist/util/types.js.flow +0 -232
- package/dist/util/types.typestest.js.flow +0 -6
- package/src/hooks/__tests__/use-scheduled-interval.test.ts +0 -460
- package/src/hooks/__tests__/use-scheduled-timeout.test.ts +0 -478
- package/src/hooks/internal/use-updating-ref.ts +0 -23
- package/src/hooks/use-scheduled-interval.ts +0 -72
- package/src/hooks/use-scheduled-timeout.ts +0 -79
package/dist/util/interval.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { SchedulePolicy, ClearPolicy } from "./policies";
|
|
2
|
+
import type { IInterval } from "./types";
|
|
2
3
|
/**
|
|
3
4
|
* Encapsulates everything associated with calling setInterval/clearInterval,
|
|
4
5
|
* and managing the lifecycle of that interval. This includes the ability to
|
|
@@ -9,17 +10,17 @@ import type { IInterval, SchedulePolicy, ClearPolicy } from "./types";
|
|
|
9
10
|
* @implements {IInterval}
|
|
10
11
|
*/
|
|
11
12
|
export default class Interval implements IInterval {
|
|
12
|
-
_intervalId:
|
|
13
|
+
_intervalId: ReturnType<typeof setInterval> | null | undefined;
|
|
13
14
|
_action: () => unknown;
|
|
14
15
|
_intervalMs: number;
|
|
15
16
|
/**
|
|
16
17
|
* Creates an interval that will invoke the given action after
|
|
17
18
|
* the given period. The interval does not start until set is called.
|
|
18
19
|
*
|
|
19
|
-
* @param
|
|
20
|
+
* @param action The action to be invoked each time the
|
|
20
21
|
* interval period has passed.
|
|
21
|
-
* @param
|
|
22
|
-
* @param
|
|
22
|
+
* @param intervalMs The interval period.
|
|
23
|
+
* @param [schedulePolicy] When SchedulePolicy.Immediately,
|
|
23
24
|
* the interval is set immediately on instantiation; otherwise, `set` must be
|
|
24
25
|
* called to set the interval.
|
|
25
26
|
* Defaults to `SchedulePolicy.Immediately`.
|
|
@@ -29,7 +30,7 @@ export default class Interval implements IInterval {
|
|
|
29
30
|
/**
|
|
30
31
|
* Determine if the interval is active or not.
|
|
31
32
|
*
|
|
32
|
-
* @returns
|
|
33
|
+
* @returns true if the interval is active, otherwise false.
|
|
33
34
|
* @memberof Interval
|
|
34
35
|
*/
|
|
35
36
|
get isSet(): boolean;
|
|
@@ -48,12 +49,11 @@ export default class Interval implements IInterval {
|
|
|
48
49
|
* If the interval is active, this cancels that interval. If no interval is
|
|
49
50
|
* pending, this does nothing.
|
|
50
51
|
*
|
|
51
|
-
* @param
|
|
52
|
+
* @param [policy] When ClearPolicy.Resolve, if the request
|
|
52
53
|
* was set when called, the request action is invoked after cancelling
|
|
53
54
|
* the request; otherwise, the pending action is cancelled.
|
|
54
55
|
* Defaults to `ClearPolicy.Cancel`.
|
|
55
56
|
*
|
|
56
|
-
* @returns {void}
|
|
57
57
|
* @memberof Interval
|
|
58
58
|
*/
|
|
59
59
|
clear(policy?: ClearPolicy): void;
|
package/dist/util/policies.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
export declare
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
export declare
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
1
|
+
export declare enum SchedulePolicy {
|
|
2
|
+
Immediately = "schedule-immediately",
|
|
3
|
+
OnDemand = "schedule-on-demand"
|
|
4
|
+
}
|
|
5
|
+
export declare enum ClearPolicy {
|
|
6
|
+
Resolve = "resolve-on-clear",
|
|
7
|
+
Cancel = "cancel-on-clear"
|
|
8
|
+
}
|
|
9
|
+
export declare enum ActionPolicy {
|
|
10
|
+
Reset = "reset",
|
|
11
|
+
Passive = "passive"
|
|
12
|
+
}
|
package/dist/util/timeout.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { SchedulePolicy, ClearPolicy } from "./policies";
|
|
2
|
+
import type { ITimeout } from "./types";
|
|
2
3
|
/**
|
|
3
4
|
* Encapsulates everything associated with calling setTimeout/clearTimeout, and
|
|
4
5
|
* managing the lifecycle of that timer, including the ability to resolve or
|
|
@@ -9,17 +10,17 @@ import type { ITimeout, SchedulePolicy, ClearPolicy } from "./types";
|
|
|
9
10
|
* @implements {ITimeout}
|
|
10
11
|
*/
|
|
11
12
|
export default class Timeout implements ITimeout {
|
|
12
|
-
_timeoutId:
|
|
13
|
+
_timeoutId: ReturnType<typeof setTimeout> | null | undefined;
|
|
13
14
|
_action: () => unknown;
|
|
14
15
|
_timeoutMs: number;
|
|
15
16
|
/**
|
|
16
17
|
* Creates a timeout that will invoke the given action after
|
|
17
18
|
* the given period. The timeout does not start until set is called.
|
|
18
19
|
*
|
|
19
|
-
* @param
|
|
20
|
+
* @param action The action to be invoked when the timeout
|
|
20
21
|
* period has passed.
|
|
21
|
-
* @param
|
|
22
|
-
* @param
|
|
22
|
+
* @param timeoutMs The timeout period.
|
|
23
|
+
* @param [schedulePolicy] When SchedulePolicy.Immediately,
|
|
23
24
|
* the timer is set immediately on instantiation; otherwise, `set` must be
|
|
24
25
|
* called to set the timeout.
|
|
25
26
|
* Defaults to `SchedulePolicy.Immediately`.
|
|
@@ -29,7 +30,7 @@ export default class Timeout implements ITimeout {
|
|
|
29
30
|
/**
|
|
30
31
|
* Determine if the timeout is set or not.
|
|
31
32
|
*
|
|
32
|
-
* @returns
|
|
33
|
+
* @returns true if the timeout is set (aka pending), otherwise
|
|
33
34
|
* false.
|
|
34
35
|
* @memberof Timeout
|
|
35
36
|
*/
|
|
@@ -50,7 +51,7 @@ export default class Timeout implements ITimeout {
|
|
|
50
51
|
* If the timeout is pending, this cancels that pending timeout without
|
|
51
52
|
* invoking the action. If no timeout is pending, this does nothing.
|
|
52
53
|
*
|
|
53
|
-
* @param
|
|
54
|
+
* @param [policy] When ClearPolicy.Resolve, if the request
|
|
54
55
|
* was set when called, the request action is invoked after cancelling
|
|
55
56
|
* the request; otherwise, the pending action is cancelled.
|
|
56
57
|
* Defaults to `ClearPolicy.Cancel`.
|
package/dist/util/types.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export type ClearPolicy = "resolve-on-clear" | "cancel-on-clear";
|
|
1
|
+
import * as Policies from "./policies";
|
|
3
2
|
/**
|
|
4
3
|
* Encapsulates everything associated with calling setTimeout/clearTimeout, and
|
|
5
4
|
* managing the lifecycle of that timer, including the ability to resolve or
|
|
@@ -33,14 +32,14 @@ export interface ITimeout {
|
|
|
33
32
|
* If the timeout is pending, this cancels that pending timeout. If no
|
|
34
33
|
* timeout is pending, this does nothing.
|
|
35
34
|
*
|
|
36
|
-
* @param
|
|
35
|
+
* @param [policy] When ClearPolicy.Resolve, if the request
|
|
37
36
|
* was set when called, the request action is invoked after cancelling
|
|
38
37
|
* the request; otherwise, the pending action is cancelled.
|
|
39
38
|
* Defaults to `ClearPolicy.Cancel`.
|
|
40
39
|
*
|
|
41
40
|
* @memberof ITimeout
|
|
42
41
|
*/
|
|
43
|
-
clear(policy?: ClearPolicy): void;
|
|
42
|
+
clear(policy?: Policies.ClearPolicy): void;
|
|
44
43
|
}
|
|
45
44
|
/**
|
|
46
45
|
* Encapsulates everything associated with calling setInterval/clearInterval,
|
|
@@ -73,14 +72,14 @@ export interface IInterval {
|
|
|
73
72
|
* If the interval is active, this cancels that interval. If the interval
|
|
74
73
|
* is not active, this does nothing.
|
|
75
74
|
*
|
|
76
|
-
* @param
|
|
75
|
+
* @param [policy] When ClearPolicy.Resolve, if the request
|
|
77
76
|
* was set when called, the request action is invoked after cancelling
|
|
78
77
|
* the request; otherwise, the pending action is cancelled.
|
|
79
78
|
* Defaults to `ClearPolicy.Cancel`.
|
|
80
79
|
*
|
|
81
80
|
* @memberof IInterval
|
|
82
81
|
*/
|
|
83
|
-
clear(policy?: ClearPolicy): void;
|
|
82
|
+
clear(policy?: Policies.ClearPolicy): void;
|
|
84
83
|
}
|
|
85
84
|
/**
|
|
86
85
|
* Encapsulates everything associated with calling requestAnimationFrame/
|
|
@@ -115,18 +114,27 @@ export interface IAnimationFrame {
|
|
|
115
114
|
* If the request is pending, this cancels that pending request. If no
|
|
116
115
|
* request is pending, this does nothing.
|
|
117
116
|
*
|
|
118
|
-
* @param
|
|
117
|
+
* @param [policy] When ClearPolicy.Resolve, if the request
|
|
119
118
|
* was set when called, the request action is invoked after cancelling
|
|
120
119
|
* the request; otherwise, the pending action is cancelled.
|
|
121
120
|
* Defaults to `ClearPolicy.Cancel`.
|
|
122
121
|
*
|
|
123
122
|
* @memberof IAnimationFrame
|
|
124
123
|
*/
|
|
125
|
-
clear(policy?: ClearPolicy): void;
|
|
124
|
+
clear(policy?: Policies.ClearPolicy): void;
|
|
126
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Options for the scheduling APIs.
|
|
128
|
+
*/
|
|
127
129
|
export type Options = {
|
|
128
|
-
schedulePolicy?: SchedulePolicy;
|
|
129
|
-
clearPolicy?: ClearPolicy;
|
|
130
|
+
schedulePolicy?: Policies.SchedulePolicy;
|
|
131
|
+
clearPolicy?: Policies.ClearPolicy;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Options for the hook variants of our scheduling APIs.
|
|
135
|
+
*/
|
|
136
|
+
export type HookOptions = Options & {
|
|
137
|
+
actionPolicy?: Policies.ActionPolicy;
|
|
130
138
|
};
|
|
131
139
|
/**
|
|
132
140
|
* Provides means to request timeouts, intervals, and animation frames, with
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@khanacademy/wonder-blocks-timing",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.0.0",
|
|
5
5
|
"design": "v1",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"react": "16.14.0"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"wb-dev-build-settings": "^0.
|
|
20
|
+
"@khanacademy/wb-dev-build-settings": "^1.0.0"
|
|
21
21
|
},
|
|
22
22
|
"author": "",
|
|
23
23
|
"license": "MIT"
|
|
24
|
-
}
|
|
24
|
+
}
|
|
@@ -18,11 +18,19 @@ type WithoutActionScheduler<T> = Omit<T, "schedule">;
|
|
|
18
18
|
export default function withActionScheduler<
|
|
19
19
|
Props extends WithActionSchedulerProps,
|
|
20
20
|
>(WrappedComponent: React.ComponentType<Props>) {
|
|
21
|
-
|
|
21
|
+
const displayName = `withActionScheduler(${
|
|
22
|
+
WrappedComponent.displayName || WrappedComponent.name
|
|
23
|
+
})`;
|
|
24
|
+
|
|
25
|
+
const C = (props: WithoutActionScheduler<Props>) => (
|
|
22
26
|
<ActionSchedulerProvider>
|
|
23
27
|
{(schedule: IScheduleActions) => (
|
|
24
28
|
<WrappedComponent {...(props as Props)} schedule={schedule} />
|
|
25
29
|
)}
|
|
26
30
|
</ActionSchedulerProvider>
|
|
27
31
|
);
|
|
32
|
+
|
|
33
|
+
C.displayName = displayName;
|
|
34
|
+
|
|
35
|
+
return C;
|
|
28
36
|
}
|