Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1199x 1199x 1199x 1199x 1199x 1488x 1488x 770x 770x 1488x 1488x 1488x 1199x 8x 8x 1199x 389x 389x 1199x 1104x 1104x 1199x 2x 2x 1199x 322x 322x 1199x 2x 2x 1199x 869x 869x 1199x 2x 2x 1199x 735x 735x 1199x 2x 2x 1199x 53x 53x 1199x 8x 8x 1199x 713x 713x 1199x 10x 10x 1199x 727x 727x 1199x 715x 715x 1199x 12x 12x 1199x 3x 3x 1199x 3x 3x 1199x 113x 113x 57x 57x 56x 113x 1199x 1199x 1199x 1199x 10374x 10374x 1199x 13288x 13288x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 1x 1x 183x 183x 183x 183x 183x 13288x 13288x 1199x 26x 26x 1199x 1217x 1217x 1199x 841x 841x 1199x 756x 756x 1199x 74x 74x 1199x 1199x 1199x 1199x 1199x 1199x 1199x 1199x 88x 1111x 26x 1085x 1199x 1199x 1199x 14x 14x 6x 6x 14x 14x 7x 7x 7x 8x 1x 1x 7x 7x 7x 7x 14x 14x 1199x 84x 84x 84x 84x 84x 1x 1x 1x 83x 83x 84x 1x 1x 84x 24x 84x 55x 56x 3x 3x 82x 84x 1199x 31x 31x 31x 31x 31x 1199x 31x 31x 31x 31x 31x 31x 31x 1199x 836x 434x 434x 434x 434x 434x 419x 434x 15x 15x 6x 15x 8x 8x 8x 8x 8x 8x 9x 1x 1x 1x 1x 1x 1x 1x 1x 434x 434x 434x 419x 434x 836x 1199x 407x 407x 404x 407x 1199x 417x 417x 417x 417x 417x 25x 22x 1x 1x 22x 22x 26x 26x 26x 26x 26x 26x 26x 26x 26x 22x 20x 20x 20x 20x 26x 4x 4x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 26x 22x 22x 25x 25x 25x 25x 413x 413x 413x 417x 417x 417x 417x 1199x 389x 389x 389x 389x 389x 389x 389x 389x 389x 387x 387x 387x 387x 389x 1199x 324x 324x 324x 321x 321x 321x 321x 321x 321x 321x 1x 1x 1x 321x 324x 1199x 4x 2x 2x 1x 1x 1x 2x 2x 1x 1x 2x 2x 4x 1199x 2x 2x 2x 1199x 1x 2x 2x 2x 2x 2x 2x 1x 2x 2x 1x 1x 1x 1x 2x 2x | import { createObserver, mutable, retriable, type StateObserver, untrack } from '@anchorlib/core';
import { RouteCache } from './cache.js';
import { DEFAULT_CONFIG, DYNAMIC_ROUTE_KEY, inheritConfig, ROUTE_MAP_LINK, WILDCARD_ROUTE_KEY } from './constant.js';
import { RENDER_MODE, ROUTE_STATUS, ROUTE_TYPE } from './enum.js';
import { Redirect } from './redirect.js';
import { RouteRegistry } from './registry.js';
import type { Router } from './router.js';
import { getStore } from './store.js';
import type {
ActiveContext,
ExtractParams,
ExtractQueryParams,
GuardBlocker,
GuardContext,
GuardHandler,
NestedParams,
NestedQueryParams,
ProviderContext,
ProviderMap,
ProviderObserver,
ProviderOptions,
RouteError,
RouteInternalRenderer,
RouteName,
RouteOptions,
RoutePath,
RoutePathOutput,
RouteRendererFn,
RouteState,
RouteStatus,
RouteStorage,
RouteType,
TRec,
UnknownGuard,
UnknownProvider,
UnknownRoute,
} from './types.js';
/**
* Represents a route in the router with support for guards, providers, and nested routes.
*
* Routes can be static, dynamic (with parameters), or wildcards.
* They support authentication guards, data providers, and reactive state management.
*
* @template TPath - The route path type
* @template TParams - The route parameters type
* @template TQueryParams - The query parameters type
* @template TOptions - The route options type
* @template TData - The route data type
* @template TParent - The parent route type
*
* @example
* ```ts
* const usersRoute = router.route('/users');
* const userRoute = usersRoute.route('/:id');
*
* userRoute
* .guard(async ({ params }) => {
* if (!await isAuthenticated()) {
* throw redirect(loginRoute);
* }
* })
* .provide('user', async ({ params }) => {
* return await fetchUser(params.id);
* });
* ```
*/
export class Route<
TPath extends RoutePath,
TParams extends ExtractParams<TPath>,
TQueryParams extends ExtractQueryParams<TPath>,
TOptions extends RouteOptions,
TData,
TParent = never,
TOutput = any,
> {
/** The name of this route */
public readonly name: RouteName<TPath>;
/** The type of this route (static, dynamic, or wildcard) */
public readonly type: RouteType;
public readonly options: TOptions;
public renderer?: RouteInternalRenderer<TOutput>;
/**
* Sets whether this route is currently active.
*
* @param value - true if the route is active, false otherwise
*/
public set active(value: boolean) {
untrack(() => {
if (value && !this.state.resolved) {
this.state.resolving = true;
}
});
this.state.active = value;
}
/**
* Gets whether this route is currently active.
*
* @returns true if the route is active, false otherwise
*/
public get active(): boolean {
return this.state.active;
}
public get status(): RouteStatus {
return this.state.status;
}
public set status(value: RouteStatus) {
this.state.status = value;
}
public get authenticated() {
return this.state.authenticated;
}
public set authenticated(value: boolean) {
this.state.authenticated = value;
}
public get authenticating() {
return this.state.authenticating;
}
public set authenticating(value: boolean) {
this.state.authenticating = value;
}
public get resolved() {
return this.state.resolved;
}
public set resolved(value: boolean) {
this.state.resolved = value;
}
public get resolving() {
return this.state.resolving;
}
public set resolving(value: boolean) {
this.state.resolving = value;
}
/**
* Gets the data loaded for this route.
*
* @returns The route data, or undefined if not loaded
*/
public get data(): TData | undefined {
return this.state.data;
}
/**
* Sets the data for this route.
*
* @param value - The route data, or undefined to clear
*/
public set data(value: TData | undefined) {
this.state.data = value;
}
/**
* Gets any error that occurred during route loading.
*
* @returns The route error, or undefined if no error
*/
public get error(): RouteError | undefined {
return this.state.error;
}
/**
* Sets the error for this route.
*
* @param value - The route error, or undefined to clear
*/
public set error(value: RouteError | undefined) {
this.state.error = value;
}
/**
* Sets the active context for this route.
*
* @param value - The active context, or undefined to clear
*/
public set context(value: ActiveContext<TParams, TQueryParams, TData> | undefined,) {
this.state.context = value;
}
/**
* Gets the active context for this route.
*
* @returns The active context, or undefined if not active
*/
public get context(): ActiveContext<TParams, TQueryParams, TData> | undefined {
return this.state.context;
}
/**
* Gets the query parameters for this route.
*
* @returns The query parameters, or undefined if not active
*/
public get query(): TQueryParams | undefined {
return this.state.context?.query;
}
/**
* Gets the route parameters for this route.
*
* @returns The route parameters, or undefined if not active
*/
public get params(): TParams | undefined {
return this.state.context?.params;
}
/**
* Gets the full path for this route, including parent paths.
*
* @returns The full route path
*/
public get path(): RoutePathOutput<TParent, TPath> {
const parent = this.parent as UnknownRoute;
if (parent) {
return [parent.path, this.name].join('/') as never;
}
return this.name as never;
}
/** Optional index route for this route */
public index?: UnknownRoute;
/** Set of guards for this route */
public guards = new Set<UnknownGuard>();
/** Map of data providers for this route */
public providers = new Map<string, ProviderMap>();
public get state(): RouteState<TParams, TQueryParams, TData> {
return this.storage.state as RouteState<TParams, TQueryParams, TData>;
}
private get storage(): RouteStorage {
const store = getStore();
if (!store.has(this)) {
untrack(() => {
store.set(this, {
state: mutable<RouteState<TParams, TQueryParams, TData>>({
status: 'idle',
active: false,
resolved: false,
resolving: false,
authenticated: false,
authenticating: false,
}),
cache: new RouteCache(this as UnknownRoute),
dataCache: new WeakMap(),
activeResolvers: new Map(),
guardObserver: createObserver(() => {
this.guardObserver.reset();
this.authenticate((this.context ?? {}) as GuardContext<TParams, TQueryParams>, true);
}),
providerObservers: new WeakMap(),
});
});
}
return store.get(this) as RouteStorage;
}
private get cache(): RouteCache {
return this.storage.cache;
}
private get dataCache(): WeakMap<ProviderContext<TRec, TRec, TRec>, TData> {
return this.storage.dataCache as WeakMap<ProviderContext<TRec, TRec, TRec>, TData>;
}
private get activeResolvers(): Map<ProviderContext<TRec, TRec, TRec>, AbortController> {
return this.storage.activeResolvers as Map<ProviderContext<TRec, TRec, TRec>, AbortController>;
}
// Reactive observers.
private get guardObserver(): StateObserver {
return this.storage.guardObserver;
}
private get providerObservers(): WeakMap<UnknownProvider, ProviderObserver> {
return this.storage.providerObservers as WeakMap<UnknownProvider, ProviderObserver>;
}
/**
* Creates a new Route instance.
*
* @param router - The router instance
* @param name - The route path
* @param options - Optional route options
* @param parent - Optional parent route
*/
public constructor(
public router: Router<TOutput>,
name: TPath,
options?: RouteOptions,
public parent?: TParent
) {
this.name = (name ?? '').replace(/^\//, '').split(/\//g)[0] as RouteName<TPath>;
this.type = this.name.startsWith(':')
? ROUTE_TYPE.DYNAMIC
: this.name.startsWith('*')
? ROUTE_TYPE.WILDCARD
: ROUTE_TYPE.STATIC;
this.options = inheritConfig(router.options, options) as TOptions;
}
/**
* Generates a URL for this route with the given parameters and query.
*
* @param params - Optional route parameters
* @param query - Optional query parameters
* @returns The generated URL string
*
* @example
* ```ts
* const url = userRoute.url({ id: '123' }, { tab: 'profile' });
* // Returns: '/users/123?tab=profile'
* ```
*/
public url(params?: TParams, query?: TQueryParams) {
let url = this.path as string;
for (const [key, value] of Object.entries((params ?? {}) as TRec)) {
url = url.replace(`:${key}`, value as string);
}
const queries = Object.entries((query ?? {}) as TRec);
if (queries.length) {
if (!url.endsWith('?')) url += '?';
url += queries
.map(([key, value]) => {
if (Array.isArray(value)) {
return value.map((v) => `${key}=${v}`).join('&');
}
return `${key}=${value}`;
})
.join('&');
}
return url;
}
/**
* Creates a child route.
*
* If the path is '/', creates an index route and returns this route.
* Otherwise, creates a new child route and returns it.
*
* @template TChildPath - The child route path type
* @template TChildParams - The child route parameters type
* @template TChildQueryParams - The child query parameters type
* @template TChildOptions - The child route options type
* @template TChildData - The child route data type
* @param path - The child route path
* @param options - Optional child route options
* @returns This route if path is '/', otherwise the new child route
*
* @example
* ```ts
* const usersRoute = router.route('/users');
* const userRoute = usersRoute.route('/:id');
* const postsRoute = userRoute.route('/posts');
* ```
*/
public route<
TChildPath extends RoutePath,
TChildParams extends ExtractParams<TChildPath>,
TChildQueryParams extends ExtractQueryParams<TChildPath>,
TChildOptions extends RouteOptions,
TChildData,
>(
path: TChildPath,
options?: TChildOptions
): TChildPath extends '/'
? Omit<
Route<
TChildPath,
NestedParams<TParams, TChildParams>,
NestedQueryParams<TQueryParams, TChildQueryParams>,
RouteOptions & TOptions & TChildOptions,
TData & TChildData,
this,
TOutput
>,
'route'
>
: Route<
TChildPath,
NestedParams<TParams, TChildParams>,
NestedQueryParams<TQueryParams, TChildQueryParams>,
RouteOptions & TOptions & TChildOptions,
TData & TChildData,
this,
TOutput
> {
const child = new Route(this.router, path, inheritConfig(this.options, options), this);
if (path === ('/' as TChildPath)) {
this.index = child as never as UnknownRoute;
return child as never;
}
const childMap = new RouteRegistry(child as never as UnknownRoute);
const parentMap = ROUTE_MAP_LINK.get(this) as RouteRegistry;
if (!parentMap) {
throw new Error('RouteMap not found');
}
if (child.type === ROUTE_TYPE.STATIC) {
parentMap.set(child.name, childMap);
} else if (child.type === ROUTE_TYPE.DYNAMIC) {
parentMap.set(DYNAMIC_ROUTE_KEY, childMap);
} else if (child.type === ROUTE_TYPE.WILDCARD) {
parentMap.set(WILDCARD_ROUTE_KEY, childMap);
}
return child as never;
}
/**
* Adds a guard to this route.
*
* Guards are run before the route is activated and can block navigation
* by throwing an error or a Redirect.
*
* @template TGuard - The guard handler type
* @param guard - The guard function to add
* @returns This route for chaining
*
* @example
* ```ts
* route.guard(async ({ params }) => {
* if (!await isAuthenticated()) {
* throw redirect(loginRoute);
* }
* });
* ```
*/
public guard<TGuard extends GuardHandler<TParams, TQueryParams>>(
guard: TGuard
): Route<TPath, TParams, TQueryParams, TOptions, TData, TParent> {
this.guards.add(guard as UnknownGuard);
return this as never;
}
/**
* Adds a data provider to this route.
*
* Providers are run when the route is activated and their data is
* available in the route's context.
*
* @template TName - The provider name type
* @template TProviderData - The provider data type
* @param name - The name of the provider
* @param provider - The provider function
* @param options - Optional provider options
* @returns This route for chaining
*
* @example
* ```ts
* route.provide('user', async ({ params }) => {
* return await fetchUser(params.id);
* });
* ```
*/
public provide<TName extends string, TProviderData>(
name: TName,
provider: (context: ProviderContext<TParams, TQueryParams, TData>) => Promise<TProviderData> | TProviderData,
options?: ProviderOptions
): Route<TPath, TParams, TQueryParams, TOptions, TData & { [PK in TName]: TProviderData }, TParent> {
this.providers.set(name, { name, provider, options } as ProviderMap);
return this as never;
}
/**
* Runs all guards for this route.
*
* Guards are run inside an observer, so they will re-run when
* reactive state they depend on changes.
*
* @param context - The guard context
* @param force - Whether to force re-running the guards
* @returns true if all guards pass, otherwise a GuardBlocker
*
* @example
* ```ts
* const result = await route.authenticate({ params: { id: '123' }, query: {} });
* if (result !== true) {
* // Navigation was blocked
* }
* ```
*/
public async authenticate(context: GuardContext<TParams, TQueryParams>, force = false): Promise<true | GuardBlocker> {
if (this.state.authenticated && !force) return Promise.resolve(true);
// Run the guard inside an observer, so whenever the state it reads change,
// the observer will be re-run.
return await this.guardObserver.run(async () => {
this.authenticating = true;
try {
const guards = Array.from(this.guards);
await Promise.all(Array.from(guards).map((guard) => guard(context)));
this.state.authenticated = true;
} catch (error) {
this.state.authenticated = false;
if (error instanceof Redirect) {
return error;
} else if (error instanceof Error) {
this.error = {
type: 'guard',
cause: error,
message: error.message,
};
return error;
} else {
const cause = new Error('Unknown guard error.');
this.error = {
type: 'guard',
cause,
message: cause.message,
};
return cause;
}
} finally {
this.authenticating = false;
}
return true;
});
}
/**
* Preloads data for this route without activating it.
*
* Runs authentication and resolves all providers.
*
* @param context - The provider context
* @returns The loaded data, or a GuardBlocker if authentication failed
*
* @example
* ```ts
* await route.preload({ params: { id: '123' }, query: {}, data: {} });
* ```
*/
public async preload(context: ProviderContext<TParams, TQueryParams, TData>): Promise<TData | GuardBlocker> {
const authenticated = await this.authenticate(context);
if (authenticated !== true) return authenticated;
return (await this.resolve(context as ProviderContext<TRec, TRec, TRec>)) as TData;
}
/**
* Resolves all providers for this route.
*
* Providers are run inside observers, so they will re-run when
* reactive state they depend on changes.
*
* @param context - The provider context
* @returns The resolved data, or undefined if a provider failed
*
* @example
* ```ts
* const data = await route.resolve({ params: { id: '123' }, query: {}, data: {} });
* ```
*/
public async resolve(context: ProviderContext<TRec, TRec, TRec>): Promise<TData | undefined> {
const abortController = new AbortController();
this.activeResolvers.set(context, abortController);
try {
const data = (this.dataCache.get(context) ?? mutable({} as TRec)) as TRec;
for (const [name, { provider, options }] of this.providers) {
if (!this.providerObservers.has(provider)) {
const observer = createObserver(() => {
observer.reset();
resolver();
});
// Run the provider inside an observer, so whenever the state it reads change,
// the observer will be re-run.
const resolver = () => {
return observer.run(async () => {
this.resolving = true;
try {
const providerData = await retriable(
async () => {
return await this.cache.resolve(provider, context, options);
},
{ ...DEFAULT_CONFIG, ...this.options, ...options, controller: abortController }
);
if (abortController.signal.aborted) return;
untrack(() => {
context.data[name] = data[name] = providerData;
});
return providerData;
} catch (error) {
this.status = ROUTE_STATUS.ERROR;
if (error instanceof Error) {
this.error = {
type: 'provider',
cause: error,
message: error.message,
};
return error;
} else {
const cause = new Error('Unknown provider error.');
this.error = {
type: 'provider',
cause,
message: cause.message,
};
return cause;
}
/* v8 ignore next - V8 coverage considers finally to have a hidden branch here */
} finally {
this.resolving = false;
}
});
};
this.providerObservers.set(provider, { observer, resolver });
}
const { resolver } = this.providerObservers.get(provider)!;
const result = await resolver();
if (result instanceof Error) return;
}
this.dataCache.set(context, data as TData);
this.resolved = true;
return data as TData;
} finally {
this.activeResolvers.delete(context);
}
}
/**
* Activates this route.
*
* Optionally preloads data, then sets the route as active.
*
* @param context - The provider context
* @param preload - Whether to preload data (default: true)
*
* @example
* ```ts
* await route.activate({ params: { id: '123' }, query: {}, data: {} });
* ```
*/
public async activate(context: ProviderContext<TParams, TQueryParams, TData>, preload = true): Promise<void> {
this.status = ROUTE_STATUS.PENDING;
this.error = undefined;
// Set the route as active immediately if renderMode is immediate
if (this.options.renderMode === RENDER_MODE.IMMEDIATE) {
this.active = true;
}
if (preload) {
await this.preload(context);
}
// If the route is deactivated during preload, do nothing.
if (this.status !== ROUTE_STATUS.PENDING) return;
this.data = this.dataCache.get(context as ProviderContext<TRec, TRec, TRec>);
this.context = context as ActiveContext<TParams, TQueryParams, TData>;
this.status = ROUTE_STATUS.SUCCESS;
this.active = true;
}
/**
* Deactivates this route.
*
* Clears data and context unless keepAlive is enabled.
*
* @example
* ```ts
* route.deactivate();
* ```
*/
public deactivate(): void {
this.active = false;
this.status = ROUTE_STATUS.IDLE;
if (!this.options?.keepAlive) {
this.data = undefined;
this.error = undefined;
this.context = undefined;
this.resolved = false;
this.authenticated = false;
this.guardObserver.destroy();
for (const { provider } of this.providers.values()) {
this.providerObservers.get(provider)?.observer.destroy();
this.providerObservers.delete(provider);
}
}
}
/**
* Cancels any pending provider resolutions.
*
* If a context is provided, only cancels that specific resolution.
* Otherwise, cancels all pending resolutions.
*
* @param context - Optional context to cancel
*
* @example
* ```ts
* route.cancel(); // Cancel all
* route.cancel(context); // Cancel specific
* ```
*/
public cancel(context?: ProviderContext<TRec, TRec, TRec>): void {
if (context) {
const controller = this.activeResolvers.get(context);
if (controller) {
controller.abort('Resolution cancelled');
this.activeResolvers.delete(context);
}
} else {
for (const controller of this.activeResolvers.values()) {
controller.abort('Resolution cancelled');
}
this.activeResolvers.clear();
}
}
public render(renderer: RouteRendererFn<TParams, TQueryParams, TData, TOutput>): this {
this.renderer = createRenderer(this as UnknownRoute, renderer, true);
return this;
}
}
let createRenderer = <TParams, TQueryParams, TData, TOutput>(
route: UnknownRoute,
renderer: RouteRendererFn<TParams, TQueryParams, TData, TOutput>,
layout?: boolean
): RouteInternalRenderer<TOutput> => {
return ({ children }) => {
if (layout) return renderer(route.state as never, route.context as never, children);
return renderer(route.state as never);
};
};
export type RendererFactory = typeof createRenderer;
export function getRendererFactory(): RendererFactory {
return createRenderer;
}
export function setRendererFactory(factory: RendererFactory) {
createRenderer = factory;
}
|