-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: support use hook #4970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gaoachao
wants to merge
8
commits into
preactjs:main
Choose a base branch
from
gaoachao:feat/support-use
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+277
−40
Open
feat: support use hook #4970
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ef6cdc2
feat: support use hook
gaoachao 0273f32
Apply suggestion from @rschristian
gaoachao 66ef156
Apply suggestion from @rschristian
gaoachao 6897dc7
Apply suggestion from @JoviDeCroock
gaoachao 0ef32f0
chore: rename usable to resource & delete CONTEXT_TYPE
gaoachao 846560d
Apply suggestion from @JoviDeCroock
gaoachao 11f6241
Apply suggestion from @JoviDeCroock
gaoachao 9707147
chore: delete CONTEXT_TYPE
gaoachao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,210 @@ | ||
| import { setupScratch, teardown } from '../../../test/_util/helpers'; | ||
| import { setupRerender } from 'preact/test-utils'; | ||
| import { createElement, render, createContext } from 'preact'; | ||
| import { Suspense } from 'preact/compat'; | ||
| import { use, useErrorBoundary } from 'preact/hooks'; | ||
|
|
||
| describe('use(promise)', () => { | ||
| /** @type {HTMLDivElement} */ | ||
| let scratch; | ||
| /** @type {() => void} */ | ||
| let rerender; | ||
|
|
||
| beforeEach(() => { | ||
| scratch = setupScratch(); | ||
| rerender = setupRerender(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| teardown(scratch); | ||
| }); | ||
|
|
||
| it('suspends on pending and renders fallback, then shows resolved data', async () => { | ||
| /** @type {(v: string) => void} */ | ||
| let resolve; | ||
| const p = new Promise((res, _rej) => { | ||
| resolve = v => res(v); | ||
| }); | ||
|
|
||
| function Data() { | ||
| const val = use(p); | ||
| return <div>Data: {val}</div>; | ||
| } | ||
|
|
||
| render( | ||
| <Suspense fallback={<div>Loading</div>}> | ||
| <Data /> | ||
| </Suspense>, | ||
| scratch | ||
| ); | ||
| // Initial render followed by rerender to reflect fallback during suspension | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal('<div>Loading</div>'); | ||
|
|
||
| resolve('hello'); | ||
| await p; | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal('<div>Data: hello</div>'); | ||
| }); | ||
|
|
||
| it('renders two components using same promise and updates both on resolve', async () => { | ||
| /** @type {(v: string) => void} */ | ||
| let resolve; | ||
| const p = new Promise((res, _rej) => { | ||
| resolve = v => res(v); | ||
| }); | ||
|
|
||
| function A() { | ||
| const val = use(p); | ||
| return <div>A: {val}</div>; | ||
| } | ||
| function B() { | ||
| const val = use(p); | ||
| return <div>B: {val}</div>; | ||
| } | ||
|
|
||
| render( | ||
| <Suspense fallback={<div>Loading</div>}> | ||
| <A /> | ||
| <B /> | ||
| </Suspense>, | ||
| scratch | ||
| ); | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal('<div>Loading</div>'); | ||
|
|
||
| resolve('x'); | ||
| await p; | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal('<div>A: x</div><div>B: x</div>'); | ||
| }); | ||
|
|
||
| it('propagates rejection to error boundary after suspension', async () => { | ||
| /** @type {() => void} */ | ||
| let reject; | ||
| const p = new Promise((res, rej) => { | ||
| reject = () => rej(new Error('boom')); | ||
| }); | ||
| p.catch(() => {}); | ||
|
|
||
| function Catcher(props) { | ||
| const [err] = useErrorBoundary(); | ||
| return err ? <div>Caught: {err.message}</div> : props.children; | ||
| } | ||
|
|
||
| function Data() { | ||
| const val = use(p); | ||
| return <div>Data: {val}</div>; | ||
| } | ||
|
|
||
| render( | ||
| <Suspense fallback={<div>Loading</div>}> | ||
| <Catcher> | ||
| <Data /> | ||
| </Catcher> | ||
| </Suspense>, | ||
| scratch | ||
| ); | ||
|
|
||
| await new Promise(resolve => setTimeout(resolve, 0)); | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal('<div>Loading</div>'); | ||
|
|
||
| reject(); | ||
|
|
||
| await new Promise(resolve => setTimeout(resolve, 0)); | ||
| rerender(); | ||
|
|
||
| expect(scratch.innerHTML).to.equal('<div>Caught: boom</div>'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('use(context)', () => { | ||
| /** @type {HTMLDivElement} */ | ||
| let scratch; | ||
|
|
||
| beforeEach(() => { | ||
| scratch = setupScratch(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| teardown(scratch); | ||
| }); | ||
|
|
||
| it('gets values from context via use(Context)', () => { | ||
| const values = []; | ||
| const Ctx = createContext(13); | ||
|
|
||
| function Comp() { | ||
| const value = use(Ctx); | ||
| values.push(value); | ||
| return null; | ||
| } | ||
|
|
||
| render(<Comp />, scratch); | ||
| render( | ||
| <Ctx.Provider value={42}> | ||
| <Comp /> | ||
| </Ctx.Provider>, | ||
| scratch | ||
| ); | ||
| render( | ||
| <Ctx.Provider value={69}> | ||
| <Comp /> | ||
| </Ctx.Provider>, | ||
| scratch | ||
| ); | ||
|
|
||
| expect(values).to.deep.equal([13, 42, 69]); | ||
| }); | ||
|
|
||
| it('uses default value when no provider is present', () => { | ||
| const Foo = createContext(42); | ||
| let read; | ||
|
|
||
| function App() { | ||
| read = use(Foo); | ||
| return <div />; | ||
| } | ||
|
|
||
| render(<App />, scratch); | ||
| expect(read).to.equal(42); | ||
| }); | ||
|
|
||
| it('supports multiple contexts via use(Context)', () => { | ||
| const Foo = createContext(0); | ||
| const Bar = createContext(10); | ||
| /** @type {Array<[number, number]>} */ | ||
| const reads = []; | ||
|
|
||
| function Comp() { | ||
| const foo = use(Foo); | ||
| const bar = use(Bar); | ||
| reads.push([foo, bar]); | ||
| return <div />; | ||
| } | ||
|
|
||
| render( | ||
| <Foo.Provider value={0}> | ||
| <Bar.Provider value={10}> | ||
| <Comp /> | ||
| </Bar.Provider> | ||
| </Foo.Provider>, | ||
| scratch | ||
| ); | ||
| expect(reads).to.deep.equal([[0, 10]]); | ||
|
|
||
| render( | ||
| <Foo.Provider value={11}> | ||
| <Bar.Provider value={42}> | ||
| <Comp /> | ||
| </Bar.Provider> | ||
| </Foo.Provider>, | ||
| scratch | ||
| ); | ||
| expect(reads).to.deep.equal([ | ||
| [0, 10], | ||
| [11, 42] | ||
| ]); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.