@khanacademy/wonder-blocks-timing 2.0.3 → 2.1.1
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/es/index.js +107 -230
- package/dist/index.js +260 -547
- package/dist/index.js.flow +1 -1
- package/package.json +3 -4
- package/src/__docs__/_overview_.stories.mdx +17 -0
- package/src/components/__docs__/migration.stories.mdx +112 -0
- package/{docs.md → src/components/__docs__/types.ischedule-actions.stories.mdx} +11 -260
- package/src/components/__docs__/with-action-scheduler-examples.js +80 -0
- package/src/components/__docs__/with-action-scheduler.stories.mdx +218 -0
- package/src/components/__tests__/action-scheduler-provider.test.js +7 -7
- package/src/components/__tests__/with-action-scheduler.test.js +6 -6
- package/src/components/action-scheduler-provider.js +2 -2
- package/src/components/with-action-scheduler.js +2 -2
- package/src/hooks/__docs__/use-interval.stories.mdx +80 -0
- package/src/hooks/__docs__/use-scheduled-interval.stories.mdx +147 -0
- package/src/hooks/{use-timeout.stories.mdx → __docs__/use-scheduled-timeout.stories.mdx} +13 -17
- package/src/hooks/__docs__/use-timeout.stories.mdx +80 -0
- package/src/hooks/__tests__/use-interval.test.js +124 -0
- package/src/hooks/__tests__/use-scheduled-interval.test.js +461 -0
- package/src/hooks/__tests__/use-scheduled-timeout.test.js +479 -0
- package/src/hooks/__tests__/use-timeout.test.js +94 -294
- package/src/hooks/internal/use-updating-ref.js +20 -0
- package/src/hooks/use-interval.js +37 -0
- package/src/hooks/use-scheduled-interval.js +73 -0
- package/src/hooks/use-scheduled-timeout.js +80 -0
- package/src/hooks/use-timeout.js +22 -55
- package/src/index.js +7 -4
- package/src/util/__tests__/action-scheduler.test.js +9 -9
- package/src/util/__tests__/animation-frame.test.js +2 -2
- package/src/util/__tests__/interval.test.js +2 -2
- package/src/util/__tests__/timeout.test.js +2 -2
- package/src/util/action-scheduler.js +4 -4
- package/src/util/animation-frame.js +2 -2
- package/src/util/interval.js +2 -2
- package/src/util/timeout.js +2 -2
- package/src/util/types.flowtest.js +2 -2
- package/LICENSE +0 -21
- package/src/__tests__/__snapshots__/generated-snapshot.test.js.snap +0 -353
- package/src/__tests__/generated-snapshot.test.js +0 -156
- package/src/components/with-action-scheduler.md +0 -18
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
// This file is auto-generated by gen-snapshot-tests.js
|
|
2
|
-
// Do not edit this file. To make changes to these snapshot tests:
|
|
3
|
-
// 1. edit the markdown documentation files in the package,
|
|
4
|
-
// packages/wonder-blocks-timing
|
|
5
|
-
// 2. Run `yarn run gen-snapshot-tests`.
|
|
6
|
-
import React from "react";
|
|
7
|
-
import renderer from "react-test-renderer";
|
|
8
|
-
|
|
9
|
-
// Mock react-dom as jest doesn't like findDOMNode.
|
|
10
|
-
jest.mock("react-dom");
|
|
11
|
-
import Button from "@khanacademy/wonder-blocks-button";
|
|
12
|
-
import {IDProvider, View} from "@khanacademy/wonder-blocks-core";
|
|
13
|
-
import {withActionScheduler} from "@khanacademy/wonder-blocks-timing";
|
|
14
|
-
|
|
15
|
-
describe("wonder-blocks-timing", () => {
|
|
16
|
-
it("example 1", () => {
|
|
17
|
-
class Unmounter extends React.Component {
|
|
18
|
-
constructor() {
|
|
19
|
-
super();
|
|
20
|
-
this.state = {
|
|
21
|
-
mountKids: true,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
maybeRenderKids() {
|
|
26
|
-
if (this.state.mountKids) {
|
|
27
|
-
return (
|
|
28
|
-
<React.Fragment>
|
|
29
|
-
<Button onClick={() => this.onClick()}>
|
|
30
|
-
Unmount
|
|
31
|
-
</Button>
|
|
32
|
-
{this.props.children}
|
|
33
|
-
</React.Fragment>
|
|
34
|
-
);
|
|
35
|
-
} else {
|
|
36
|
-
return "Children unmounted";
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
onClick() {
|
|
41
|
-
this.setState({
|
|
42
|
-
mountKids: false,
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
render() {
|
|
47
|
-
return <View>{this.maybeRenderKids()}</View>;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
class MyNaughtyComponent extends React.Component {
|
|
52
|
-
componentDidMount() {
|
|
53
|
-
const {targetId} = this.props;
|
|
54
|
-
let counter = 0;
|
|
55
|
-
const domElement = document.getElementById(targetId);
|
|
56
|
-
setInterval(() => {
|
|
57
|
-
domElement.innerText =
|
|
58
|
-
"Naughty interval logged: " + counter++;
|
|
59
|
-
}, 200);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
render() {
|
|
63
|
-
return <View>NaughtyComponent here</View>;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const example = (
|
|
68
|
-
<IDProvider>
|
|
69
|
-
{(id) => (
|
|
70
|
-
<View>
|
|
71
|
-
<Unmounter>
|
|
72
|
-
<MyNaughtyComponent targetId={id} />
|
|
73
|
-
</Unmounter>
|
|
74
|
-
<View>
|
|
75
|
-
<View id={id}></View>
|
|
76
|
-
</View>
|
|
77
|
-
</View>
|
|
78
|
-
)}
|
|
79
|
-
</IDProvider>
|
|
80
|
-
);
|
|
81
|
-
const tree = renderer.create(example).toJSON();
|
|
82
|
-
expect(tree).toMatchSnapshot();
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it("example 2", () => {
|
|
86
|
-
class Unmounter extends React.Component {
|
|
87
|
-
constructor() {
|
|
88
|
-
super();
|
|
89
|
-
this.state = {
|
|
90
|
-
mountKids: true,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
maybeRenderKids() {
|
|
95
|
-
if (this.state.mountKids) {
|
|
96
|
-
return (
|
|
97
|
-
<React.Fragment>
|
|
98
|
-
<Button onClick={() => this.onClick()}>
|
|
99
|
-
Unmount
|
|
100
|
-
</Button>
|
|
101
|
-
{this.props.children}
|
|
102
|
-
</React.Fragment>
|
|
103
|
-
);
|
|
104
|
-
} else {
|
|
105
|
-
return "Children unmounted";
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
onClick() {
|
|
110
|
-
this.setState({
|
|
111
|
-
mountKids: false,
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
render() {
|
|
116
|
-
return <View>{this.maybeRenderKids()}</View>;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
class MyGoodComponent extends React.Component {
|
|
121
|
-
componentDidMount() {
|
|
122
|
-
const {targetId, schedule} = this.props;
|
|
123
|
-
let counter = 0;
|
|
124
|
-
const domElement = document.getElementById(targetId);
|
|
125
|
-
schedule.interval(() => {
|
|
126
|
-
domElement.innerText =
|
|
127
|
-
"Naughty interval logged: " + counter++;
|
|
128
|
-
}, 200);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
render() {
|
|
132
|
-
return <View>GoodComponent here</View>;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const MyGoodComponentWithScheduler = withActionScheduler(
|
|
137
|
-
MyGoodComponent,
|
|
138
|
-
);
|
|
139
|
-
const example = (
|
|
140
|
-
<IDProvider>
|
|
141
|
-
{(id) => (
|
|
142
|
-
<View>
|
|
143
|
-
<Unmounter>
|
|
144
|
-
<MyGoodComponentWithScheduler targetId={id} />
|
|
145
|
-
</Unmounter>
|
|
146
|
-
<View>
|
|
147
|
-
<View id={id}></View>
|
|
148
|
-
</View>
|
|
149
|
-
</View>
|
|
150
|
-
)}
|
|
151
|
-
</IDProvider>
|
|
152
|
-
);
|
|
153
|
-
const tree = renderer.create(example).toJSON();
|
|
154
|
-
expect(tree).toMatchSnapshot();
|
|
155
|
-
});
|
|
156
|
-
});
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
This is a higher order component (HOC) that attaches the given component to an
|
|
2
|
-
[`IScheduleActions`](#ischeduleactions) instance. Any actions scheduled will automatically be
|
|
3
|
-
cleared on unmount. This allows for "set it and forget it" behavior that won't
|
|
4
|
-
leave timers dangling when the component's lifecycle ends.
|
|
5
|
-
|
|
6
|
-
For more details on using this component and the [`IScheduleActions`](#ischeduleactions) interface,
|
|
7
|
-
see the [API overview](#timing-api-overview).
|
|
8
|
-
|
|
9
|
-
### Flow Types
|
|
10
|
-
If you are using Flow typing, you can use the `WithActionSchedulerProps` type
|
|
11
|
-
to build the props for the component that you will pass to the `withActionScheduler`
|
|
12
|
-
function by spreading the type into your component's `Props` type.
|
|
13
|
-
|
|
14
|
-
The added `schedule` prop is of type [`IScheduleActions`](#ischeduleactions). This is what the
|
|
15
|
-
`withActionScheduler` function injects to your component.
|
|
16
|
-
|
|
17
|
-
The returned value from `withActionScheduler` is a React component with props of
|
|
18
|
-
type `TProps`.
|