-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: implement getServerSnapshot for useSyncExternalStore #4979
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
1048818
f1f096a
7c07af8
54700be
9571df2
ae47bd7
8387589
fde4a0e
7670785
70af09a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,35 @@ | ||
| import { useState, useLayoutEffect, useEffect } from 'preact/hooks'; | ||
| import { MODE_HYDRATE } from '../../src/constants'; | ||
| import { options as _options } from 'preact'; | ||
|
|
||
| /** @type {boolean} */ | ||
| let hydrating; | ||
| // Cast to use internal Options type | ||
| const options = /** @type {import('../../src/internal').Options} */ (_options); | ||
| let oldBeforeRender = options._render; | ||
|
|
||
| /** @type {(vnode: import('./internal').VNode) => void} */ | ||
| options._render = _vnode => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we move this to the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I tried it locally it was 32b gzip
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Roughly a third then, hefty. |
||
| hydrating = !!(_vnode._flags & MODE_HYDRATE); | ||
| if (oldBeforeRender) oldBeforeRender(_vnode); | ||
| }; | ||
|
|
||
| /** | ||
| * This is taken from https://github.com/facebook/react/blob/main/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L84 | ||
| * on a high level this cuts out the warnings, ... and attempts a smaller implementation | ||
| * @typedef {{ _value: any; _getSnapshot: () => any }} Store | ||
| */ | ||
| export function useSyncExternalStore(subscribe, getSnapshot) { | ||
| const value = getSnapshot(); | ||
| export function useSyncExternalStore( | ||
| subscribe, | ||
| getSnapshot, | ||
| getServerSnapshot | ||
| ) { | ||
| const value = | ||
| typeof window === 'undefined' || hydrating | ||
|
rschristian marked this conversation as resolved.
Outdated
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This or we use |
||
| ? getServerSnapshot | ||
| ? getServerSnapshot() | ||
| : missingGetServerSnapshot() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not add oversized error strings, this seems a bit against our bundle philosophy
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That (the oversized string) was my suggestion FWIW, my bad |
||
| : getSnapshot(); | ||
|
|
||
| /** | ||
| * @typedef {{ _instance: Store }} StoreRef | ||
|
|
@@ -52,6 +75,10 @@ function didSnapshotChange(inst) { | |
| } | ||
| } | ||
|
|
||
| function missingGetServerSnapshot() { | ||
| throw new Error('Missing getServerSnapshot for server rendering'); | ||
| } | ||
|
rschristian marked this conversation as resolved.
Outdated
|
||
|
|
||
| export function startTransition(cb) { | ||
| cb(); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.