StackedRouter: add startTransition
All checks were successful
/ depoly (push) Successful in 1m21s

This commit is contained in:
thislight 2025-01-02 21:05:20 +08:00
parent 2e13fcacb5
commit 1115135380
No known key found for this signature in database
GPG key ID: FCFE5192241CCD4E

View file

@ -15,6 +15,8 @@ import {
useContext,
onCleanup,
type Accessor,
useTransition,
getOwner,
} from "solid-js";
import { createStore, unwrap } from "solid-js/store";
import "./StackedRouter.css";
@ -79,8 +81,8 @@ export type NewFrameOptions<T> = (T extends undefined
export type FramePusher<T, K extends keyof T = keyof T> = T[K] extends
| undefined
| any
? (path: K, state?: Readonly<NewFrameOptions<T[K]>>) => Readonly<StackFrame>
: (path: K, state: Readonly<NewFrameOptions<T[K]>>) => Readonly<StackFrame>;
? (path: K, state?: Readonly<NewFrameOptions<T[K]>>) => Promise<Readonly<StackFrame>>
: (path: K, state: Readonly<NewFrameOptions<T[K]>>) => Promise<Readonly<StackFrame>>;
export type Navigator<PushGuide = Record<string, any>> = {
frames: readonly StackFrame[];
@ -465,6 +467,8 @@ const StackedRouter: Component<StackedRouterProps> = (oprops) => {
const [stack, mutStack] = createStore([] as StackFrame[], { name: "stack" });
const windowSize = useWindowSize();
const [, startTransition] = useTransition();
if (import.meta.hot) {
const saveStack = () => {
import.meta.hot!.data[$StackedRouterSavedStack] = unwrap(stack);
@ -488,8 +492,8 @@ const StackedRouter: Component<StackedRouterProps> = (oprops) => {
});
}
const pushFrame = (path: string, opts?: Readonly<NewFrameOptions<any>>) =>
untrack(() => {
const pushFrame = async (path: string, opts?: Readonly<NewFrameOptions<any>>) =>
await untrack(async () => {
const frame = {
path,
state: opts?.state,
@ -499,14 +503,17 @@ const StackedRouter: Component<StackedRouterProps> = (oprops) => {
};
const replace = opts?.replace;
if (replace === "all" || stack.length === 0) {
const length = stack.length;
await startTransition(() => {
if (replace === "all" || length === 0) {
mutStack([frame]);
} else if (replace) {
const idx = stack.length - 1;
const idx = length - 1;
mutStack(idx, frame);
} else {
mutStack(stack.length, frame);
mutStack(length, frame);
}
});
const savedStack = serializableStack(stack);
@ -515,6 +522,7 @@ const StackedRouter: Component<StackedRouterProps> = (oprops) => {
} else {
window.history.pushState(savedStack, "", path);
}
return frame;
});