Skip to content

Commit eceaf6f

Browse files
committed
Update suspense lecture
1 parent dce634e commit eceaf6f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+203
-30552
lines changed

09-suspense/exercise/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"private": true,
55
"dependencies": {
66
"@reach/router": "^1.2.1",
7-
"react": "../vendor/react",
8-
"react-dom": "../vendor/react-dom",
9-
"simple-cache-provider": "../vendor/simple-cache-provider"
7+
"react": "^16.7.0-alpha.0",
8+
"react-cache": "^2.0.0-alpha.0",
9+
"react-dom": "^16.7.0-alpha.0"
1010
},
1111
"devDependencies": {
1212
"react-scripts": "1.0.10"

09-suspense/exercise/src/App.solution.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import React, { Placeholder } from "react";
2-
import { cache } from "./lib/cache";
3-
import { createResource } from "simple-cache-provider";
1+
import React, { Suspense } from "react";
2+
import { unstable_createResource as createResource } from "react-cache";
43
import { Router, Link } from "@reach/router";
54

65
let ContactsResource = createResource(async path => {
@@ -23,11 +22,11 @@ let ImageResource = createResource(src => {
2322

2423
function Img({ src, ...props }) {
2524
// eslint-disable-next-line jsx-a11y/alt-text
26-
return <img src={ImageResource.read(cache, src)} {...props} />;
25+
return <img src={ImageResource.read(src)} {...props} />;
2726
}
2827

2928
function Home() {
30-
let { contacts } = ContactsResource.read(cache, "/contacts");
29+
let { contacts } = ContactsResource.read("/contacts");
3130
return (
3231
<div>
3332
<h1>Contacts</h1>
@@ -45,20 +44,20 @@ function Home() {
4544
}
4645

4746
function Contact({ id }) {
48-
let { contact } = ContactsResource.read(cache, `/contacts/${id}`);
47+
let { contact } = ContactsResource.read(`/contacts/${id}`);
4948
return (
5049
<div>
5150
<h1>
5251
{contact.first} {contact.last}
5352
</h1>
5453
<p>
55-
<Placeholder delayMs={250} fallback={<span>Loading...</span>}>
54+
<Suspense maxDuration={1000} fallback={<span>Loading...</span>}>
5655
<Img
5756
alt={`${contact.first} smiling, maybe`}
5857
height="250"
5958
src={contact.avatar}
6059
/>
61-
</Placeholder>
60+
</Suspense>
6261
</p>
6362
<p>
6463
<Link to="/">Home</Link>
@@ -68,8 +67,10 @@ function Contact({ id }) {
6867
}
6968

7069
export default () => (
71-
<Router>
72-
<Home path="/" />
73-
<Contact path=":id" />
74-
</Router>
70+
<Suspense maxDuration={3000} fallback={<div>Loading...</div>}>
71+
<Router>
72+
<Home path="/" />
73+
<Contact path=":id" />
74+
</Router>
75+
</Suspense>
7576
);

09-suspense/exercise/src/App.start.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ function sleep(ms=1000) {
3535
3636
*/
3737

38-
import React from "react";
39-
import { cache } from "./lib/cache";
40-
import { createResource } from "simple-cache-provider";
38+
import React, { Suspense } from "react";
39+
import { unstable_createResource as createResource } from "react-cache";
4140
import { Router, Link } from "@reach/router";
4241

4342
let ContactsResource = createResource(async path => {
@@ -47,7 +46,7 @@ let ContactsResource = createResource(async path => {
4746
});
4847

4948
function Home() {
50-
let { contacts } = ContactsResource.read(cache, "/contacts");
49+
let { contacts } = ContactsResource.read("/contacts");
5150
return (
5251
<div>
5352
<h1>Contacts</h1>
@@ -95,8 +94,10 @@ function Contact({ id }) {
9594
}
9695

9796
export default () => (
98-
<Router>
99-
<Home path="/" />
100-
<Contact path=":id" />
101-
</Router>
97+
<Suspense maxDuration={3000} fallback={<div>Loading...</div>}>
98+
<Router>
99+
<Home path="/" />
100+
<Contact path=":id" />
101+
</Router>
102+
</Suspense>
102103
);

09-suspense/exercise/src/index.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import React, { Placeholder } from "react";
1+
import React from "react";
22
import "./lib/index.css";
3+
import { createRoot } from "react-dom";
34
import App from "./App.start";
4-
import { unstable_createRoot } from "react-dom";
55

6-
unstable_createRoot(document.getElementById("root")).render(
7-
<Placeholder delayMs={1000} fallback={<div>Loading...</div>}>
8-
<App />
9-
</Placeholder>
10-
);
6+
createRoot(document.getElementById("root")).render(<App />);

09-suspense/exercise/yarn.lock

+23-7
Original file line numberDiff line numberDiff line change
@@ -6538,6 +6538,11 @@ rc@^1.0.1, rc@^1.1.6, rc@^1.1.7, rc@^1.2.7:
65386538
minimist "^1.2.0"
65396539
strip-json-comments "~2.0.1"
65406540

6541+
react-cache@^2.0.0-alpha.0:
6542+
version "2.0.0-alpha.0"
6543+
resolved "https://registry.yarnpkg.com/react-cache/-/react-cache-2.0.0-alpha.0.tgz#d02d16a4565fd9f12478a2a41e980ba4fcbab4d5"
6544+
integrity sha512-o7nA1dIbi6wOoIoQPQBL58CgIQ4tCA3itIR3WlE2VHMIFkIXbvsMyolg+Q9wDVUlCvU6b2n40RTTv8vOmjXCOQ==
6545+
65416546
react-dev-utils@^3.0.2, react-dev-utils@^3.1.0:
65426547
version "3.1.2"
65436548
resolved "https://registry.yarnpkg.com/react-dev-utils/-/react-dev-utils-3.1.2.tgz#9c8647abf83545e1dd269a218fda4f2a246c4593"
@@ -6563,12 +6568,15 @@ react-dev-utils@^3.0.2, react-dev-utils@^3.1.0:
65636568
strip-ansi "3.0.1"
65646569
text-table "0.2.0"
65656570

6566-
react-dom@../vendor/react-dom:
6567-
version "16.4.1"
6571+
react-dom@^16.7.0-alpha.0:
6572+
version "16.7.0-alpha.0"
6573+
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.7.0-alpha.0.tgz#8379158d4c76d63c989f325f45dfa5762582584f"
6574+
integrity sha512-/XUn1ldxmoV2B7ov0rWT5LMZaaHMlF9GGLkUsmPRxmWTJwRDOuAPXidSaSlmR/VOhDSI1s+v3+KzFqhhDFJxYA==
65686575
dependencies:
65696576
loose-envify "^1.1.0"
65706577
object-assign "^4.1.1"
65716578
prop-types "^15.6.2"
6579+
scheduler "^0.11.0-alpha.0"
65726580

65736581
react-error-overlay@^1.0.9:
65746582
version "1.0.10"
@@ -6631,12 +6639,15 @@ [email protected]:
66316639
optionalDependencies:
66326640
fsevents "1.1.2"
66336641

6634-
react@../vendor/react:
6635-
version "16.4.1"
6642+
react@^16.7.0-alpha.0:
6643+
version "16.7.0-alpha.0"
6644+
resolved "https://registry.yarnpkg.com/react/-/react-16.7.0-alpha.0.tgz#e2ed4abe6f268c9b092a1d1e572953684d1783a9"
6645+
integrity sha512-V0za4H01aoAF0SdzahHepvfvzTQ1xxkgMX4z8uKzn+wzZAlVk0IVpleqyxZWluqmdftNedj6fIIZRO/rVYVFvQ==
66366646
dependencies:
66376647
loose-envify "^1.1.0"
66386648
object-assign "^4.1.1"
66396649
prop-types "^15.6.2"
6650+
scheduler "^0.11.0-alpha.0"
66406651

66416652
read-pkg-up@^1.0.1:
66426653
version "1.0.1"
@@ -7082,6 +7093,14 @@ sax@^1.2.1, sax@^1.2.4, sax@~1.2.1:
70827093
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
70837094
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==
70847095

7096+
scheduler@^0.11.0-alpha.0:
7097+
version "0.11.0-alpha.0"
7098+
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.11.0-alpha.0.tgz#7b132c726608993471db07866f2d59a52b9e190b"
7099+
integrity sha512-0tUDHYSyni/EHkMMBysVXVwfanCWWbLsulnDB1tGrQiWWrVuRVoclWCPHCYC/1iR5Rj34EQhxh3/F36V+F+ZpA==
7100+
dependencies:
7101+
loose-envify "^1.1.0"
7102+
object-assign "^4.1.1"
7103+
70857104
schema-utils@^0.3.0:
70867105
version "0.3.0"
70877106
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-0.3.0.tgz#f5877222ce3e931edae039f17eb3716e7137f8cf"
@@ -7254,9 +7273,6 @@ signal-exit@^3.0.0, signal-exit@^3.0.2:
72547273
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
72557274
integrity sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=
72567275

7257-
simple-cache-provider@../vendor/simple-cache-provider:
7258-
version "0.7.0"
7259-
72607276
slash@^1.0.0:
72617277
version "1.0.0"
72627278
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"

09-suspense/lecture/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"@reach/component-component": "^0.1.1",
77
"@reach/router": "^1.2.1",
88
"glamor": "^2.20.40",
9-
"react": "../vendor/react",
10-
"react-dom": "../vendor/react-dom",
11-
"react-svg-spinner": "^1.0.1",
12-
"simple-cache-provider": "../vendor/simple-cache-provider"
9+
"react": "16.6",
10+
"react-cache": "^2.0.0-alpha.0",
11+
"react-dom": "16.6",
12+
"react-svg-spinner": "^1.0.1"
1313
},
1414
"devDependencies": {
1515
"react-scripts": "1.0.10"

09-suspense/lecture/src/App.Refactor.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
import { createElement } from "glamor/react"; // eslint-disable-line
55
/* @jsx createElement */
6-
import React, { Placeholder } from "react";
6+
import React, { Suspense } from "react";
77
import { Router, Link } from "@reach/router";
88
import Spinner from "react-svg-spinner";
99
import Competitions from "./lib/Competitions";
1010
import ManageScroll from "./lib/ManageScroll";
11-
import { cache } from "./lib/cache";
1211

1312
import {
1413
fetchWorkout,
@@ -40,7 +39,6 @@ class Workout extends React.Component {
4039
this.setState({ exercises });
4140
});
4241
fetchNextWorkout(workoutId).then(nextWorkout => {
43-
console.log(nextWorkout);
4442
this.setState({ nextWorkout });
4543
});
4644
}
@@ -115,7 +113,7 @@ const Home = () => (
115113
);
116114

117115
const Workouts = () => {
118-
const workouts = WorkoutsResource.read(cache, 10);
116+
const workouts = WorkoutsResource.read(10);
119117
return (
120118
<div>
121119
<Link to="..">Home</Link>
@@ -145,15 +143,15 @@ const NextWorkout = ({ nextWorkout }) => {
145143

146144
const App = () => {
147145
return (
148-
<Placeholder delayMs={patience} fallback={<Spinner size="100" />}>
146+
<Suspense maxDuration={patience} fallback={<Spinner size="100" />}>
149147
<ManageScroll />
150148
<Router style={{ padding: 20 }}>
151149
<Home path="/" />
152150
<Workouts path="workouts" />
153151
<Workout path="workouts/:workoutId" />
154152
<Competitions path="competitions" />
155153
</Router>
156-
</Placeholder>
154+
</Suspense>
157155
);
158156
};
159157

09-suspense/lecture/src/App.js

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createElement } from "glamor/react"; // eslint-disable-line
22
/* @jsx createElement */
3-
import React, { Placeholder, lazy } from "react";
3+
import React, { Suspense, lazy } from "react";
44
import { Router, Link } from "@reach/router";
55
import Component from "@reach/component-component";
66
import Spinner from "react-svg-spinner";
@@ -12,7 +12,6 @@ import {
1212
NextWorkoutResource,
1313
network
1414
} from "./lib/utils";
15-
import { cache } from "./lib/cache";
1615

1716
const Competitions = lazy(() => import("./lib/Competitions"));
1817

@@ -85,57 +84,57 @@ const Home = () => (
8584
);
8685

8786
const Workouts = () => {
88-
const workouts = WorkoutsResource.read(cache, 10);
87+
const workouts = WorkoutsResource.read(10);
8988
return (
9089
<div>
9190
<Link to="..">Home</Link>
9291
<h1>Workouts</h1>
9392
{workouts.map(workout => (
94-
<LoadingLink key={workout.id} to={workout.id} css={link}>
93+
<Link key={workout.id} to={workout.id} css={link}>
9594
{workout.name}
96-
</LoadingLink>
95+
</Link>
9796
))}
9897
</div>
9998
);
10099
};
101100

102101
const Exercises = ({ workoutId }) => {
103-
const exercises = ExercisesResource.read(cache, workoutId);
102+
const exercises = ExercisesResource.read(workoutId);
104103
return <ul>{exercises.map((exercise, i) => <li key={i}>{exercise}</li>)}</ul>;
105104
};
106105

107106
class Workout extends React.Component {
108107
render() {
109108
const { workoutId } = this.props;
110109

111-
const workout = WorkoutResource.read(cache, workoutId);
110+
const workout = WorkoutResource.read(workoutId);
112111

113112
return (
114113
<div>
115114
<Link to="../..">Home</Link> / <Link to="..">Workouts</Link>
116115
<h1>{workout.name}</h1>
117-
<Placeholder delayMs={patience} fallback={<Spinner size="150" />}>
116+
<Suspense maxDuration={patience} fallback={<Spinner size="150" />}>
118117
<Exercises workoutId={workoutId} />
119-
</Placeholder>
118+
</Suspense>
120119
{workout.completed && (
121-
<Placeholder
122-
delayMs={patience}
120+
<Suspense
121+
maxDuration={patience}
123122
fallback={
124123
<h2>
125124
Up Next! <Spinner size="0.75em" />
126125
</h2>
127126
}
128127
>
129128
<NextWorkout workoutId={workoutId} />
130-
</Placeholder>
129+
</Suspense>
131130
)}
132131
</div>
133132
);
134133
}
135134
}
136135

137136
const NextWorkout = ({ workoutId }) => {
138-
const nextWorkout = NextWorkoutResource.read(cache, workoutId);
137+
const nextWorkout = NextWorkoutResource.read(workoutId);
139138
return (
140139
<div>
141140
<h2>
@@ -168,15 +167,15 @@ const Network = () => (
168167
const App = () => {
169168
return (
170169
<React.Fragment>
171-
<Placeholder delayMs={patience} fallback={<Spinner size="100" />}>
170+
<Suspense maxDuration={patience} fallback={<Spinner size="100" />}>
172171
<ManageScroll />
173172
<Router style={{ padding: 20 }}>
174173
<Home path="/" />
175174
<Workouts path="workouts" />
176175
<Workout path="workouts/:workoutId" />
177176
<Competitions path="competitions" />
178177
</Router>
179-
</Placeholder>
178+
</Suspense>
180179
<Network />
181180
</React.Fragment>
182181
);

09-suspense/lecture/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import "./lib/index.css";
22
import React from "react";
33
import { unstable_createRoot } from "react-dom";
4-
import App from "./App";
4+
import App from "./App.Refactor";
55

66
unstable_createRoot(document.getElementById("root")).render(<App />);

09-suspense/lecture/src/lib/cache.js

-7
This file was deleted.

0 commit comments

Comments
 (0)