Skip to content

Commit

Permalink
chore(visualization): improve
Browse files Browse the repository at this point in the history
  • Loading branch information
manudeli committed Sep 17, 2023
1 parent badde70 commit 0169d1a
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 102 deletions.
13 changes: 13 additions & 0 deletions websites/visualization/src/app/api/delay/route.ts
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 })
)
}
14 changes: 0 additions & 14 deletions websites/visualization/src/app/api/during/route.ts

This file was deleted.

116 changes: 78 additions & 38 deletions websites/visualization/src/app/react/experimental/await/page.tsx
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>
)
}
42 changes: 20 additions & 22 deletions websites/visualization/src/app/react/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { RejectedFallback, UseSuspenseQuery } from '~/components'
import { Area, Button, DescriptionText, Spinner } from '~/components/uis'
import { api } from '~/utils/api'

const BoundaryPage = () => {
const { reset: queryErrorReset } = useQueryErrorResetBoundary()
export default function Page() {
const queryErrorResetBoundary = useQueryErrorResetBoundary()

return (
<Area title="ErrorBoundaryGroup">
Expand All @@ -30,19 +30,19 @@ const BoundaryPage = () => {
/>
<Area title="Suspense (Continuous 3 fetching)">
<Suspense.CSROnly fallback={<Spinner />}>
<UseSuspenseQuery queryKey={['boundary', 1]} queryFn={api.alwaysSuccess500} />
<UseSuspenseQuery queryKey={['boundary', 2]} queryFn={api.alwaysSuccess1000} />
<UseSuspenseQuery queryKey={['boundary', 3]} queryFn={api.alwaysSuccess1500} />
<UseSuspenseQuery queryKey={['boundary', 1]} queryFn={() => api.delay(500, { percentage: 100 })} />
<UseSuspenseQuery queryKey={['boundary', 2]} queryFn={() => api.delay(1000, { percentage: 100 })} />
<UseSuspenseQuery queryKey={['boundary', 3]} queryFn={() => api.delay(1500, { percentage: 100 })} />
</Suspense.CSROnly>
</Area>

<DescriptionText>+</DescriptionText>

<Area title="ErrorBoundary (100% Error)">
<ErrorBoundary onReset={queryErrorReset} fallback={RejectedFallback}>
<ErrorBoundary onReset={queryErrorResetBoundary.reset} fallback={RejectedFallback}>
<Suspense.CSROnly fallback={<Spinner />}>
<UseSuspenseQuery queryKey={['boundary', 4]} queryFn={api.alwaysFailure} />
<UseSuspenseQuery queryKey={['boundary', 5]} queryFn={api.alwaysSuccess500} />
<UseSuspenseQuery queryKey={['boundary', 4]} queryFn={() => api.delay(500, { percentage: 0 })} />
<UseSuspenseQuery queryKey={['boundary', 5]} queryFn={() => api.delay(500, { percentage: 100 })} />
</Suspense.CSROnly>
</ErrorBoundary>
</Area>
Expand All @@ -51,12 +51,12 @@ const BoundaryPage = () => {

<Area title="AsyncBoundary (50% Success)">
<AsyncBoundary.CSROnly
onReset={queryErrorReset}
onReset={queryErrorResetBoundary.reset}
pendingFallback={<Spinner />}
rejectedFallback={RejectedFallback}
>
<UseSuspenseQuery queryKey={['boundary', 6]} queryFn={api.alwaysSuccess500} />
<UseSuspenseQuery queryKey={['boundary', 7]} queryFn={api.halfSuccess} />
<UseSuspenseQuery queryKey={['boundary', 6]} queryFn={() => api.delay(500, { percentage: 100 })} />
<UseSuspenseQuery queryKey={['boundary', 7]} queryFn={() => api.delay(500, { percentage: 50 })} />
</AsyncBoundary.CSROnly>
</Area>
</ErrorBoundaryGroup>
Expand All @@ -73,17 +73,17 @@ const BoundaryPage = () => {
/>
<Area title="Suspense">
<Suspense.CSROnly fallback={<Spinner />}>
<UseSuspenseQuery queryKey={['boundary', 1]} queryFn={api.alwaysSuccess500} />
<UseSuspenseQuery queryKey={['boundary', 1]} queryFn={() => api.delay(500, { percentage: 100 })} />
</Suspense.CSROnly>
</Area>

<DescriptionText>+</DescriptionText>

<Area title="ErrorBoundary (100% Error)">
<ErrorBoundary onReset={queryErrorReset} fallback={RejectedFallback}>
<ErrorBoundary onReset={queryErrorResetBoundary.reset} fallback={RejectedFallback}>
<Suspense.CSROnly fallback={<Spinner />}>
<UseSuspenseQuery queryKey={['boundary', 4]} queryFn={api.alwaysFailure} />
<UseSuspenseQuery queryKey={['boundary', 5]} queryFn={api.alwaysSuccess500} />
<UseSuspenseQuery queryKey={['boundary', 4]} queryFn={() => api.delay(500, { percentage: 0 })} />
<UseSuspenseQuery queryKey={['boundary', 5]} queryFn={() => api.delay(500, { percentage: 100 })} />
</Suspense.CSROnly>
</ErrorBoundary>
</Area>
Expand All @@ -92,27 +92,25 @@ const BoundaryPage = () => {

<Area title="AsyncBoundary (50% Success)">
<AsyncBoundary.CSROnly
onReset={queryErrorReset}
onReset={queryErrorResetBoundary.reset}
pendingFallback={<Spinner />}
rejectedFallback={RejectedFallback}
>
<UseSuspenseQuery queryKey={['boundary', 7]} queryFn={api.halfSuccess} />
<UseSuspenseQuery queryKey={['boundary', 7]} queryFn={() => api.delay(500, { percentage: 50 })} />
</AsyncBoundary.CSROnly>
</Area>
</ErrorBoundaryGroup>
</Area>

<Area title="ErrorBoundary (100% Error)">
<ErrorBoundary onReset={queryErrorReset} fallback={RejectedFallback}>
<ErrorBoundary onReset={queryErrorResetBoundary.reset} fallback={RejectedFallback}>
<Suspense.CSROnly fallback={<Spinner />}>
<UseSuspenseQuery queryKey={['boundary', 4]} queryFn={api.alwaysFailure} />
<UseSuspenseQuery queryKey={['boundary', 5]} queryFn={api.alwaysSuccess500} />
<UseSuspenseQuery queryKey={['boundary', 4]} queryFn={() => api.delay(500, { percentage: 0 })} />
<UseSuspenseQuery queryKey={['boundary', 5]} queryFn={() => api.delay(500, { percentage: 100 })} />
</Suspense.CSROnly>
</ErrorBoundary>
</Area>
</ErrorBoundaryGroup>
</Area>
)
}

export default BoundaryPage
26 changes: 4 additions & 22 deletions websites/visualization/src/utils/api.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
import axios from 'axios'

const getAxios =
({ waitMs = 500, successPercentage }: { waitMs?: number; successPercentage: number }) =>
async () =>
axios
.get<string>('/api/during', {
params: {
waitMs,
successPercentage,
},
})
.then(({ data }) => data)

export const api = {
alwaysSuccess500: getAxios({ successPercentage: 100, waitMs: 500 }),
alwaysSuccess1000: getAxios({ successPercentage: 100, waitMs: 1000 }),
alwaysSuccess1500: getAxios({ successPercentage: 100, waitMs: 1500 }),
halfSuccess: getAxios({ successPercentage: 50 }),
almostFailure: getAxios({ successPercentage: 40 }),
alwaysFailure: getAxios({ successPercentage: 0 }),
manual: (options: Parameters<typeof getAxios>[0]) =>
delay: (ms: number, { percentage }: { percentage: number }) =>
axios
.get<string>('/api/during', {
.get<string>('/api/delay', {
params: {
successPercentage: options.successPercentage,
waitMs: options.waitMs,
waitMs: ms,
percentage,
},
})
.then(({ data }) => data),
Expand Down
5 changes: 1 addition & 4 deletions websites/visualization/src/utils/delay.ts
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))
4 changes: 2 additions & 2 deletions websites/visualization/src/utils/index.ts
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'

0 comments on commit 0169d1a

Please sign in to comment.