Server: Phase 2 (REVIEW not respected yet)
This commit is contained in:
1 parent
5d16ded553
commit
142b47674d
4271 files changed
+1203269
No files matched your search
+49
@@ -0,0 +1,49 @@
|
||||
"use client";
|
||||
import { dummyMatchContext, matchContext } from "./matchContext.js";
|
||||
import { useRouter } from "./useRouter.js";
|
||||
import { invariant, replaceEqualDeep } from "@tanstack/router-core";
|
||||
import * as React$1 from "react";
|
||||
import { isServer } from "@tanstack/router-core/isServer";
|
||||
import { useSelector } from "@tanstack/react-store";
|
||||
//#region src/useMatch.tsx
|
||||
var dummyMatch = {};
|
||||
function useStructuralSharing(opts, router) {
|
||||
const previousResult = React$1.useRef();
|
||||
return (slice) => {
|
||||
const selected = opts?.select ? opts.select(slice) : slice;
|
||||
if (opts?.structuralSharing ?? router.options.defaultStructuralSharing) return previousResult.current = replaceEqualDeep(previousResult.current, selected);
|
||||
return selected;
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Read and select the nearest or targeted route match.
|
||||
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useMatchHook
|
||||
*/
|
||||
function useMatch(opts) {
|
||||
const router = useRouter();
|
||||
const nearestRouteId = React$1.useContext(opts.from ? dummyMatchContext : matchContext);
|
||||
const routeId = opts.from ?? nearestRouteId;
|
||||
const matchStore = router.stores.getMatchStore(routeId);
|
||||
if (isServer ?? router.isServer) {
|
||||
const match = matchStore.get();
|
||||
if (!match) {
|
||||
if (opts.shouldThrow ?? true) {
|
||||
if (process.env.NODE_ENV !== "production") throw new Error(`Invariant failed: Could not find ${opts.from ? `an active match from "${opts.from}"` : "a nearest match!"}`);
|
||||
invariant();
|
||||
}
|
||||
return;
|
||||
}
|
||||
return opts.select ? opts.select(match) : match;
|
||||
}
|
||||
const selector = useStructuralSharing(opts, router);
|
||||
const matchSelection = useSelector(matchStore, (match) => match ? selector(match) : dummyMatch);
|
||||
if (matchSelection !== dummyMatch) return matchSelection;
|
||||
if (opts.shouldThrow ?? true) {
|
||||
if (process.env.NODE_ENV !== "production") throw new Error(`Invariant failed: Could not find ${opts.from ? `an active match from "${opts.from}"` : "a nearest match!"}`);
|
||||
invariant();
|
||||
}
|
||||
}
|
||||
//#endregion
|
||||
export { useMatch, useStructuralSharing };
|
||||
|
||||
//# sourceMappingURL=useMatch.js.map
|
||||
Reference in new issue
Block a user