generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Ensure referential stability for values (#617)
* test: Making sure it fails beforehand * fix: Ensure referential stablity * ref: Carry state & serialized query across hook sync So that internal refs can be synced together without calling the serializer on each reception site. * doc: Add caveat about multiple parsers on the same key
- Loading branch information
Showing
7 changed files
with
226 additions
and
34 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,29 @@ | ||
/// <reference types="cypress" /> | ||
|
||
it('Referential equality', () => { | ||
cy.visit('/app/referential-equality') | ||
cy.contains('#hydration-marker', 'hydrated').should('be.hidden') | ||
cy.get('#ref-a').should('have.text', '1') | ||
cy.get('#ref-b').should('have.text', '1') | ||
cy.get('#increment-a').click() | ||
cy.get('#ref-a').should('have.text', '2') | ||
cy.get('#ref-b').should('have.text', '1') | ||
cy.get('#increment-b').click() | ||
cy.get('#ref-a').should('have.text', '2') | ||
cy.get('#ref-b').should('have.text', '2') | ||
cy.get('#idempotent-a').click() | ||
cy.get('#ref-a').should('have.text', '2') | ||
cy.get('#ref-b').should('have.text', '2') | ||
cy.get('#idempotent-b').click() | ||
cy.get('#ref-a').should('have.text', '2') | ||
cy.get('#ref-b').should('have.text', '2') | ||
cy.get('#clear-a').click() | ||
cy.get('#ref-a').should('have.text', '3') | ||
cy.get('#ref-b').should('have.text', '2') | ||
cy.get('#clear-b').click() | ||
cy.get('#ref-a').should('have.text', '3') | ||
cy.get('#ref-b').should('have.text', '3') | ||
cy.get('#link').click() | ||
cy.get('#ref-a').should('have.text', '3') | ||
cy.get('#ref-b').should('have.text', '3') | ||
}) |
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,98 @@ | ||
'use client' | ||
|
||
import Link from 'next/link' | ||
import { parseAsJson, useQueryState, useQueryStates } from 'nuqs' | ||
import { Suspense, useEffect, useState } from 'react' | ||
|
||
export default function Page() { | ||
return ( | ||
<Suspense> | ||
<Client /> | ||
</Suspense> | ||
) | ||
} | ||
|
||
const defaultValue = { x: 0 } | ||
type Value = typeof defaultValue | ||
|
||
function increment(value: Value): Value { | ||
return { x: value.x + 1 } | ||
} | ||
|
||
const makeLoggingSpy = | ||
(key: string) => | ||
(value: unknown): Value => { | ||
console.log(`[%s]: Parser running with value %O`, key, value) | ||
return value as Value | ||
} | ||
|
||
function Client() { | ||
const [aRefCount, setARefCount] = useState(0) | ||
const [bRefCount, setBRefCount] = useState(0) | ||
const [a, setA] = useQueryState( | ||
'a', | ||
parseAsJson<Value>(makeLoggingSpy('a')).withDefault(defaultValue) | ||
) | ||
const [{ b }, setB] = useQueryStates({ | ||
b: parseAsJson<Value>(makeLoggingSpy('b')).withDefault(defaultValue) | ||
}) | ||
|
||
useEffect(() => { | ||
setARefCount(old => old + 1) | ||
}, [a]) | ||
useEffect(() => { | ||
setBRefCount(old => old + 1) | ||
}, [b]) | ||
|
||
return ( | ||
<> | ||
<div> | ||
<button id="increment-a" onClick={() => setA(increment)}> | ||
Increment A | ||
</button> | ||
<button id="idempotent-a" onClick={() => setA(x => x)}> | ||
Itempotent A | ||
</button> | ||
<button id="clear-a" onClick={() => setA(null)}> | ||
Clear A | ||
</button> | ||
<span> | ||
Refs seen: <span id="ref-a">{aRefCount}</span> | ||
</span> | ||
</div> | ||
<div> | ||
<button | ||
id="increment-b" | ||
onClick={() => | ||
setB(old => ({ | ||
b: increment(old.b) | ||
})) | ||
} | ||
> | ||
Increment B | ||
</button> | ||
<button id="idempotent-b" onClick={() => setB(x => x)}> | ||
Itempotent B | ||
</button> | ||
<button | ||
id="clear-b" | ||
onClick={() => | ||
setB({ | ||
b: null | ||
}) | ||
} | ||
> | ||
Clear B | ||
</button> | ||
<span> | ||
Refs seen: <span id="ref-b">{bRefCount}</span> | ||
</span> | ||
</div> | ||
<div> | ||
<Link href="#" id="link"> | ||
Link to # | ||
</Link> | ||
</div> | ||
</> | ||
) | ||
} |
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
Oops, something went wrong.