@tanstack/react-router 0.0.1-beta.38 → 0.0.1-beta.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -13
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
- package/build/cjs/index.js +306 -241
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.js +296 -252
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +87 -56
- package/build/types/index.d.ts +50 -42
- package/build/umd/index.development.js +1368 -858
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +2 -2
- package/build/umd/index.production.js.map +1 -1
- package/package.json +3 -2
- package/src/index.tsx +468 -421
- package/src/uSES/useSyncExternalStore.ts +16 -0
- package/src/uSES/useSyncExternalStoreShim.ts +20 -0
- package/src/uSES/useSyncExternalStoreShimClient.ts +87 -0
- package/src/uSES/useSyncExternalStoreShimServer.ts +20 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @flow
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
'use strict'
|
|
11
|
+
|
|
12
|
+
// Intentionally not using named imports because Rollup uses dynamic
|
|
13
|
+
// dispatch for CommonJS interop named imports.
|
|
14
|
+
import * as React from 'react'
|
|
15
|
+
|
|
16
|
+
export const useSyncExternalStore = React.useSyncExternalStore
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @flow
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { useSyncExternalStore as client } from './useSyncExternalStoreShimClient'
|
|
11
|
+
import { useSyncExternalStore as server } from './useSyncExternalStoreShimServer'
|
|
12
|
+
import { useSyncExternalStore as builtInAPI } from 'react'
|
|
13
|
+
|
|
14
|
+
const shim = typeof document === 'undefined' ? server : client
|
|
15
|
+
|
|
16
|
+
export const useSyncExternalStore: <T>(
|
|
17
|
+
subscribe: (cb: () => void) => () => void,
|
|
18
|
+
getSnapshot: () => T,
|
|
19
|
+
getServerSnapshot?: () => T,
|
|
20
|
+
) => T = builtInAPI !== undefined ? builtInAPI : shim
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
const { useState, useEffect, useLayoutEffect, useDebugValue } = React
|
|
3
|
+
|
|
4
|
+
let didWarnOld18Alpha = false
|
|
5
|
+
let didWarnUncachedGetSnapshot = false
|
|
6
|
+
|
|
7
|
+
export function useSyncExternalStore<T>(
|
|
8
|
+
subscribe: (cb: () => void) => () => void,
|
|
9
|
+
getSnapshot: () => T,
|
|
10
|
+
getServerSnapshot?: () => T,
|
|
11
|
+
): T {
|
|
12
|
+
const value = getSnapshot()
|
|
13
|
+
|
|
14
|
+
// Because updates are synchronous, we don't queue them. Instead we force a
|
|
15
|
+
// re-render whenever the subscribed state changes by updating an some
|
|
16
|
+
// arbitrary useState hook. Then, during render, we call getSnapshot to read
|
|
17
|
+
// the current value.
|
|
18
|
+
//
|
|
19
|
+
// Because we don't actually use the state returned by the useState hook, we
|
|
20
|
+
// can save a bit of memory by storing other stuff in that slot.
|
|
21
|
+
//
|
|
22
|
+
// To implement the early bailout, we need to track some things on a mutable
|
|
23
|
+
// object. Usually, we would put that in a useRef hook, but we can stash it in
|
|
24
|
+
// our useState hook instead.
|
|
25
|
+
//
|
|
26
|
+
// To force a re-render, we call forceUpdate({inst}). That works because the
|
|
27
|
+
// new object always fails an equality check.
|
|
28
|
+
const [{ inst }, forceUpdate] = useState({ inst: { value, getSnapshot } })
|
|
29
|
+
|
|
30
|
+
// Track the latest getSnapshot function with a ref. This needs to be updated
|
|
31
|
+
// in the layout phase so we can access it during the tearing check that
|
|
32
|
+
// happens on subscribe.
|
|
33
|
+
useLayoutEffect(() => {
|
|
34
|
+
inst.value = value
|
|
35
|
+
inst.getSnapshot = getSnapshot
|
|
36
|
+
|
|
37
|
+
// Whenever getSnapshot or subscribe changes, we need to check in the
|
|
38
|
+
// commit phase if there was an interleaved mutation. In concurrent mode
|
|
39
|
+
// this can happen all the time, but even in synchronous mode, an earlier
|
|
40
|
+
// effect may have mutated the store.
|
|
41
|
+
if (checkIfSnapshotChanged(inst)) {
|
|
42
|
+
// Force a re-render.
|
|
43
|
+
forceUpdate({ inst })
|
|
44
|
+
}
|
|
45
|
+
}, [subscribe, value, getSnapshot])
|
|
46
|
+
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
// Check for changes right before subscribing. Subsequent changes will be
|
|
49
|
+
// detected in the subscription handler.
|
|
50
|
+
if (checkIfSnapshotChanged(inst)) {
|
|
51
|
+
// Force a re-render.
|
|
52
|
+
forceUpdate({ inst })
|
|
53
|
+
}
|
|
54
|
+
const handleStoreChange = () => {
|
|
55
|
+
// TODO: Because there is no cross-renderer API for batching updates, it's
|
|
56
|
+
// up to the consumer of this library to wrap their subscription event
|
|
57
|
+
// with unstable_batchedUpdates. Should we try to detect when this isn't
|
|
58
|
+
// the case and print a warning in development?
|
|
59
|
+
|
|
60
|
+
// The store changed. Check if the snapshot changed since the last time we
|
|
61
|
+
// read from the store.
|
|
62
|
+
if (checkIfSnapshotChanged(inst)) {
|
|
63
|
+
// Force a re-render.
|
|
64
|
+
forceUpdate({ inst })
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// Subscribe to the store and return a clean-up function.
|
|
68
|
+
return subscribe(handleStoreChange)
|
|
69
|
+
}, [subscribe])
|
|
70
|
+
|
|
71
|
+
useDebugValue(value)
|
|
72
|
+
return value
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function checkIfSnapshotChanged<T>(inst: {
|
|
76
|
+
value: T
|
|
77
|
+
getSnapshot: () => T
|
|
78
|
+
}): boolean {
|
|
79
|
+
const latestGetSnapshot = inst.getSnapshot
|
|
80
|
+
const prevValue = inst.value
|
|
81
|
+
try {
|
|
82
|
+
const nextValue = latestGetSnapshot()
|
|
83
|
+
return !Object.is(prevValue, nextValue)
|
|
84
|
+
} catch (error) {
|
|
85
|
+
return true
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @flow
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export function useSyncExternalStore<T>(
|
|
11
|
+
subscribe: (cb: () => void) => () => void,
|
|
12
|
+
getSnapshot: () => T,
|
|
13
|
+
getServerSnapshot?: () => T,
|
|
14
|
+
): T {
|
|
15
|
+
// Note: The shim does not use getServerSnapshot, because pre-18 versions of
|
|
16
|
+
// React do not expose a way to check if we're hydrating. So users of the shim
|
|
17
|
+
// will need to track that themselves and return the correct value
|
|
18
|
+
// from `getSnapshot`.
|
|
19
|
+
return getSnapshot()
|
|
20
|
+
}
|