@tanstack/router-core
Version:
Modern and scalable routing for React applications
71 lines (70 loc) • 3.65 kB
text/typescript
import { AnyRoute } from './route.cjs';
import { RouterState } from './router.cjs';
import { FullSearchSchema } from './routeInfo.cjs';
import { ParsedLocation } from './location.cjs';
import { AnyRedirect } from './redirect.cjs';
import { AnyRouteMatch } from './Matches.cjs';
export interface RouterReadableStore<TValue> {
readonly state: TValue;
}
export interface RouterWritableStore<TValue> extends RouterReadableStore<TValue> {
setState: (updater: (prev: TValue) => TValue) => void;
}
export type RouterBatchFn = (fn: () => void) => void;
export type MutableStoreFactory = <TValue>(initialValue: TValue) => RouterWritableStore<TValue>;
export type ReadonlyStoreFactory = <TValue>(read: () => TValue) => RouterReadableStore<TValue>;
export type GetStoreConfig = (opts: {
isServer?: boolean;
}) => StoreConfig;
export type StoreConfig = {
createMutableStore: MutableStoreFactory;
createReadonlyStore: ReadonlyStoreFactory;
batch: RouterBatchFn;
init?: (stores: RouterStores<AnyRoute>) => void;
};
type MatchStore = RouterWritableStore<AnyRouteMatch> & {
routeId?: string;
};
type ReadableStore<TValue> = RouterReadableStore<TValue>;
/** SSR non-reactive createMutableStore */
export declare function createNonReactiveMutableStore<TValue>(initialValue: TValue): RouterWritableStore<TValue>;
/** SSR non-reactive createReadonlyStore */
export declare function createNonReactiveReadonlyStore<TValue>(read: () => TValue): RouterReadableStore<TValue>;
export interface RouterStores<in out TRouteTree extends AnyRoute> {
status: RouterWritableStore<RouterState<TRouteTree>['status']>;
loadedAt: RouterWritableStore<number>;
isLoading: RouterWritableStore<boolean>;
isTransitioning: RouterWritableStore<boolean>;
location: RouterWritableStore<ParsedLocation<FullSearchSchema<TRouteTree>>>;
resolvedLocation: RouterWritableStore<ParsedLocation<FullSearchSchema<TRouteTree>> | undefined>;
statusCode: RouterWritableStore<number>;
redirect: RouterWritableStore<AnyRedirect | undefined>;
matchesId: RouterWritableStore<Array<string>>;
pendingMatchesId: RouterWritableStore<Array<string>>;
activeMatchesSnapshot: ReadableStore<Array<AnyRouteMatch>>;
pendingMatchesSnapshot: ReadableStore<Array<AnyRouteMatch>>;
cachedMatchesSnapshot: ReadableStore<Array<AnyRouteMatch>>;
firstMatchId: ReadableStore<string | undefined>;
hasPendingMatches: ReadableStore<boolean>;
matchRouteReactivity: ReadableStore<{
locationHref: string;
resolvedLocationHref: string | undefined;
status: RouterState<TRouteTree>['status'];
}>;
__store: RouterReadableStore<RouterState<TRouteTree>>;
activeMatchStoresById: Map<string, MatchStore>;
pendingMatchStoresById: Map<string, MatchStore>;
cachedMatchStoresById: Map<string, MatchStore>;
/**
* Get a computed store that resolves a routeId to its current match state.
* Returns the same cached store instance for repeated calls with the same key.
* The computed depends on matchesId + the individual match store, so
* subscribers are only notified when the resolved match state changes.
*/
getMatchStoreByRouteId: (routeId: string) => RouterReadableStore<AnyRouteMatch | undefined>;
setActiveMatches: (nextMatches: Array<AnyRouteMatch>) => void;
setPendingMatches: (nextMatches: Array<AnyRouteMatch>) => void;
setCachedMatches: (nextMatches: Array<AnyRouteMatch>) => void;
}
export declare function createRouterStores<TRouteTree extends AnyRoute>(initialState: RouterState<TRouteTree>, config: StoreConfig): RouterStores<TRouteTree>;
export {};