generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for transitions (#406)
* feat: Add support for transitions This lets non-shallow updates be notified of server rendering loading status, using an external `React.useTransition` hook. * test: Add e2e test for transitions * fix: improve the Option type for transitions (#410) * feat: Add support for transitions This lets non-shallow updates be notified of server rendering loading status, using an external `React.useTransition` hook. * fix: improve typing * fix: better typing * fix: remove unnecessary code * chore: remove unnecessary changes * fix: some edge cases when shallow is not a boolean type * fix: remove as any * fix: e2e build * chore: better name for generic * fix: parser type * fix: better naming * fix: better naming * chore: better naming * test: Add type tests for shallow/startTransition interaction * fix: simplify type * test: add a extra test case * chore: extract type exclusion logic * chore: simplify types * chore: remove unnecessary generics * chore: add test case for startTransition * test: Add type tests * chore: Simplify type & prettify --------- Co-authored-by: Francois Best <[email protected]> * doc: Add transitions docs --------- Co-authored-by: Jon Sun <[email protected]>
- Loading branch information
Showing
13 changed files
with
297 additions
and
29 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
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,20 @@ | ||
/// <reference types="cypress" /> | ||
|
||
it('transitions', () => { | ||
cy.visit('/app/transitions') | ||
cy.contains('#hydration-marker', 'hydrated').should('be.hidden') | ||
cy.get('#server-rendered').should('have.text', '{}') | ||
cy.get('#server-status').should('have.text', 'idle') | ||
const button = cy.get('button') | ||
button.should('have.text', '0') | ||
button.click() | ||
button.should('have.text', '1') // Instant setState | ||
cy.get('#server-rendered').should('have.text', '{}') | ||
cy.get('#server-status').should('have.text', 'loading') | ||
cy.wait(500) | ||
cy.get('#server-rendered').should('have.text', '{}') | ||
cy.get('#server-status').should('have.text', 'loading') | ||
cy.wait(500) | ||
cy.get('#server-rendered').should('have.text', '{"counter":"1"}') | ||
cy.get('#server-status').should('have.text', 'idle') | ||
}) |
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,20 @@ | ||
'use client' | ||
|
||
import { parseAsInteger, useQueryState } from 'next-usequerystate' | ||
import React from 'react' | ||
import { HydrationMarker } from '../../../components/hydration-marker' | ||
|
||
export function Client() { | ||
const [isLoading, startTransition] = React.useTransition() | ||
const [counter, setCounter] = useQueryState( | ||
'counter', | ||
parseAsInteger.withDefault(0).withOptions({ startTransition }) | ||
) | ||
return ( | ||
<> | ||
<HydrationMarker /> | ||
<p id="server-status">{isLoading ? 'loading' : 'idle'}</p> | ||
<button onClick={() => setCounter(counter + 1)}>{counter}</button> | ||
</> | ||
) | ||
} |
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,20 @@ | ||
import { setTimeout } from 'node:timers/promises' | ||
import { Suspense } from 'react' | ||
import { Client } from './client' | ||
|
||
type PageProps = { | ||
searchParams: Record<string, string | string[] | undefined> | ||
} | ||
|
||
export default async function Page({ searchParams }: PageProps) { | ||
await setTimeout(1000) | ||
return ( | ||
<> | ||
<h1>Transitions</h1> | ||
<pre id="server-rendered">{JSON.stringify(searchParams)}</pre> | ||
<Suspense> | ||
<Client /> | ||
</Suspense> | ||
</> | ||
) | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { describe, expect, test, vi } from 'vitest' | ||
import { compose } from './update-queue' | ||
|
||
describe('update-queue/compose', () => { | ||
test('empty array', () => { | ||
const final = vi.fn() | ||
compose([], final) | ||
expect(final).toHaveBeenCalledOnce() | ||
}) | ||
test('one item', () => { | ||
const a = vi | ||
.fn() | ||
.mockImplementation(x => x()) | ||
.mockName('a') | ||
const final = vi.fn() | ||
compose([a], final) | ||
expect(a).toHaveBeenCalledOnce() | ||
expect(final).toHaveBeenCalledOnce() | ||
expect(a.mock.invocationCallOrder[0]).toBeLessThan( | ||
final.mock.invocationCallOrder[0]! | ||
) | ||
}) | ||
test('several items', () => { | ||
const a = vi.fn().mockImplementation(x => x()) | ||
const b = vi.fn().mockImplementation(x => x()) | ||
const c = vi.fn().mockImplementation(x => x()) | ||
const final = vi.fn() | ||
compose([a, b, c], final) | ||
expect(a).toHaveBeenCalledOnce() | ||
expect(b).toHaveBeenCalledOnce() | ||
expect(c).toHaveBeenCalledOnce() | ||
expect(final).toHaveBeenCalledOnce() | ||
expect(a.mock.invocationCallOrder[0]).toBeLessThan( | ||
b.mock.invocationCallOrder[0]! | ||
) | ||
expect(b.mock.invocationCallOrder[0]).toBeLessThan( | ||
c.mock.invocationCallOrder[0]! | ||
) | ||
expect(c.mock.invocationCallOrder[0]).toBeLessThan( | ||
final.mock.invocationCallOrder[0]! | ||
) | ||
}) | ||
}) |
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
Oops, something went wrong.