Skip to content

Commit 73e4ea3

Browse files
committed
example/CascadedAsyncStateExample: init example that shows how to cascade the state of multiple long running processes
1 parent bee9155 commit 73e4ea3

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*
2+
* Copyright 2022 MFB Technologies, Inc.
3+
*/
4+
5+
.cascaded-async-state-example {}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2022 MFB Technologies, Inc.
2+
3+
import { AsyncRequestStatus, createAsyncRenderer, getCascadedAsyncState } from "@mfbtech/react-async-renderer"
4+
import { useState } from "react"
5+
import "./CascadedAsyncStateExample.css"
6+
import { useLongRunningProcess } from "./useLongRunningProcess"
7+
8+
export function CascadedAsyncStateExample() {
9+
const [startProcesses, setStartProcesses] = useState(false)
10+
const processThree = useLongRunningProcess(1000, "process 3 data", startProcesses === false)
11+
const processOne = useLongRunningProcess(3000, "process 1 data", startProcesses === false)
12+
const processTwo = useLongRunningProcess(
13+
3000,
14+
"process 2 data",
15+
// Delay process 2 until process 3 has finished
16+
startProcesses === false || processThree.status !== AsyncRequestStatus.FULFILLED
17+
)
18+
19+
const cascadedStatus = getCascadedAsyncState([
20+
processOne,
21+
processTwo,
22+
processThree
23+
])
24+
const renderer = createAsyncRenderer({
25+
...cascadedStatus,
26+
onCompletedSuccessfullyArgs: {
27+
processOneData: processOne.data,
28+
processTwoData: processTwo.data,
29+
processThreeData: processThree.data
30+
}
31+
})
32+
33+
function resetProcesses() {
34+
setStartProcesses(false)
35+
processThree.reset()
36+
processOne.reset()
37+
processTwo.reset()
38+
}
39+
40+
return (
41+
<div className="cascaded-async-state-example">
42+
<h2>Cascaded async state example</h2>
43+
<p>
44+
Process 3 depends on process 2, which depends on process 1. Process 3 is hard coded to
45+
finish first, then process 1 and finally process 2. The success state will not be rendered
46+
until process 3 and its dependencies have finished. If any of the processes fail then the
47+
error state will be rendered.
48+
</p>
49+
<div>
50+
<button onClick={resetProcesses}>Reset</button>
51+
<p>process 1 status: {getAsyncStatusUi(processOne.status)}</p>
52+
<p>process 2 status: {getAsyncStatusUi(processTwo.status)}</p>
53+
<p>process 3 status: {getAsyncStatusUi(processThree.status)}</p>
54+
</div>
55+
<div>
56+
{renderer(
57+
data => (<>{
58+
Object.entries(data).map(([key, value]) => (<p key={key}>{value}</p>))
59+
}</>),
60+
{
61+
onInit: () => (<button onClick={() => { setStartProcesses(true) }}>Start</button>),
62+
}
63+
)}
64+
</div>
65+
</div>
66+
)
67+
}
68+
69+
function getAsyncStatusUi(status: AsyncRequestStatus) {
70+
switch (status) {
71+
case AsyncRequestStatus.INIT: {
72+
return "🔵 - init"
73+
}
74+
case AsyncRequestStatus.PENDING: {
75+
return "🟡 - pending"
76+
}
77+
case AsyncRequestStatus.FULFILLED: {
78+
return "🟢 - fulfilled"
79+
}
80+
case AsyncRequestStatus.ERROR: {
81+
return "🔴 - error"
82+
}
83+
default: {
84+
return "wasn't expecting that status... 🤷‍♂️"
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)