-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
118 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { NextResponse } from 'next/server' | ||
import { delay } from '~/utils/delay' | ||
|
||
export const GET = (req: Request) => { | ||
const url = new URL(req.url) | ||
const [waitMs, percentage] = ['waitMs', 'percentage'].map((key) => Number(url.searchParams.get(key))) | ||
|
||
return delay(waitMs).then(() => | ||
Math.random() * 100 < percentage | ||
? NextResponse.json(`Success after ${waitMs}ms`) | ||
: NextResponse.json('Server Error', { status: 500 }) | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
116 changes: 78 additions & 38 deletions
116
websites/visualization/src/app/react/experimental/await/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,88 @@ | ||
'use client' | ||
|
||
import { Stack } from '@jsxcss/emotion' | ||
import { ErrorBoundary, Suspense } from '@suspensive/react' | ||
import { Await } from '@suspensive/react/experimental' | ||
|
||
const delay = (ms: number) => | ||
new Promise((resolve) => | ||
setTimeout(() => { | ||
resolve('resolved') | ||
}, ms) | ||
) | ||
|
||
const asyncFn = () => delay(4000 * Math.random()).then(() => 'success' as const) | ||
import { api } from '~/utils' | ||
|
||
export default function Page() { | ||
return ( | ||
<ErrorBoundary fallback={() => <>error</>}> | ||
<Suspense fallback={<>loading...</>}> | ||
<Await options={{ key: [2000] as const, fn: asyncFn }}> | ||
{(awaited) => ( | ||
<div> | ||
<div>{awaited.data}</div> | ||
<div>{awaited.data}</div> | ||
</div> | ||
)} | ||
</Await> | ||
</Suspense> | ||
<Suspense fallback={<>loading...</>}> | ||
<Await options={{ key: [3000] as const, fn: asyncFn }}> | ||
{(awaited) => ( | ||
<div> | ||
<div>{awaited.data}</div> | ||
<div>{awaited.data}</div> | ||
</div> | ||
)} | ||
</Await> | ||
<Await options={{ key: [4000] as const, fn: asyncFn }}> | ||
{(awaited) => ( | ||
<div> | ||
<div>{awaited.data}</div> | ||
<div>{awaited.data}</div> | ||
</div> | ||
)} | ||
</Await> | ||
</Suspense> | ||
<ErrorBoundary fallback={() => <div>error</div>}> | ||
<Stack.Vertical> | ||
<Suspense.CSROnly fallback={<div>loading...</div>}> | ||
<Await options={{ key: [2000] as const, fn: ({ key: [ms] }) => api.delay(ms, { percentage: 100 }) }}> | ||
{(awaited) => ( | ||
<div> | ||
<button onClick={awaited.reset}>reset</button> | ||
<div>{awaited.data}</div> | ||
</div> | ||
)} | ||
</Await> | ||
</Suspense.CSROnly> | ||
<Suspense.CSROnly fallback={<div>loading...</div>}> | ||
<Await options={{ key: [2000] as const, fn: ({ key: [ms] }) => api.delay(ms, { percentage: 100 }) }}> | ||
{(awaited) => ( | ||
<div> | ||
<button onClick={awaited.reset}>reset</button> | ||
<div>{awaited.data}</div> | ||
</div> | ||
)} | ||
</Await> | ||
</Suspense.CSROnly> | ||
<Suspense.CSROnly fallback={<div>loading...</div>}> | ||
<Await options={{ key: [2000] as const, fn: ({ key: [ms] }) => api.delay(ms, { percentage: 100 }) }}> | ||
{(awaited) => ( | ||
<div> | ||
<button onClick={awaited.reset}>reset</button> | ||
<div>{awaited.data}</div> | ||
</div> | ||
)} | ||
</Await> | ||
</Suspense.CSROnly> | ||
<Suspense.CSROnly fallback={<div>loading...</div>}> | ||
<Await options={{ key: [2000] as const, fn: ({ key: [ms] }) => api.delay(ms, { percentage: 100 }) }}> | ||
{(awaited) => ( | ||
<div> | ||
<button onClick={awaited.reset}>reset</button> | ||
<div>{awaited.data}</div> | ||
</div> | ||
)} | ||
</Await> | ||
</Suspense.CSROnly> | ||
</Stack.Vertical> | ||
|
||
<Stack.Vertical> | ||
<Suspense.CSROnly fallback={<div>loading...</div>}> | ||
<Await options={{ key: [3000] as const, fn: ({ key: [ms] }) => api.delay(ms, { percentage: 100 }) }}> | ||
{(awaited) => ( | ||
<div> | ||
<button onClick={awaited.reset}>reset</button> | ||
<div>{awaited.data}</div> | ||
</div> | ||
)} | ||
</Await> | ||
</Suspense.CSROnly> | ||
<Suspense.CSROnly fallback={<div>loading...</div>}> | ||
<Await options={{ key: [4000] as const, fn: ({ key: [ms] }) => api.delay(ms, { percentage: 100 }) }}> | ||
{(awaited) => ( | ||
<div> | ||
<button onClick={awaited.reset}>reset</button> | ||
<div>{awaited.data}</div> | ||
</div> | ||
)} | ||
</Await> | ||
</Suspense.CSROnly> | ||
<Suspense.CSROnly fallback={<div>loading...</div>}> | ||
<Await options={{ key: [4000] as const, fn: ({ key: [ms] }) => api.delay(ms, { percentage: 100 }) }}> | ||
{(awaited) => ( | ||
<div> | ||
<button onClick={awaited.reset}>reset</button> | ||
<div>{awaited.data}</div> | ||
</div> | ||
)} | ||
</Await> | ||
</Suspense.CSROnly> | ||
</Stack.Vertical> | ||
</ErrorBoundary> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
export const delay = (ms = 1000) => | ||
new Promise((resolve) => { | ||
setTimeout(resolve, ms) | ||
}) | ||
export const delay = (ms = 1000) => new Promise((resolve) => setTimeout(resolve, ms)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './delay' | ||
export * from './api' | ||
export { delay } from './delay' | ||
export { api } from './api' |