Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: stay true to brandon's tweet about me #14

Merged
merged 3 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

163 changes: 136 additions & 27 deletions projects/router/src/lib/create-router.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import { isPlatformBrowser } from '@angular/common';
import {
ApplicationRef,
computed,
effect,
EnvironmentInjector,
inject,
linkedSignal,
PLATFORM_ID,
Provider,
signal,
Type,
} from '@angular/core';
import type { RouterHistory } from '@tanstack/history';
import type {
import {
AnyRoute,
CreateRouterFn,
getLocationChangeInfo,
RouterConstructorOptions,
RouterCore,
TrailingSlashOption,
trimPathRight,
} from '@tanstack/router-core';
import { RouterCore } from '@tanstack/router-core';

declare module '@tanstack/router-core' {
export interface UpdatableRouteOptionsExtensions {
component?: () => Type<any>;
notFoundComponent?: () => Type<any>;
pendingComponent?: () => Type<any>;
errorComponent?: () => Type<any>;
providers?: Provider[];
}
export interface RouterOptionsExtensions {
Expand All @@ -34,39 +45,22 @@ declare module '@tanstack/router-core' {
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/solid/api/router/RouterOptionsType#defaulterrorcomponent-property)
* @link [Guide](https://tanstack.com/router/latest/docs/framework/solid/guide/data-loading#handling-errors-with-routeoptionserrorcomponent)
*/
// defaultErrorComponent?: ErrorRouteComponent
defaultErrorComponent?: () => Type<any>;
/**
* The default `pendingComponent` a route should use if no pending component is provided.
*
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/solid/api/router/RouterOptionsType#defaultpendingcomponent-property)
* @link [Guide](https://tanstack.com/router/latest/docs/framework/solid/guide/data-loading#showing-a-pending-component)
*/
// defaultPendingComponent?: RouteComponent
defaultPendingComponent?: () => Type<any>;
/**
* The default `notFoundComponent` a route should use if no notFound component is provided.
*
* @default NotFound
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/solid/api/router/RouterOptionsType#defaultnotfoundcomponent-property)
* @link [Guide](https://tanstack.com/router/latest/docs/framework/solid/guide/not-found-errors#default-router-wide-not-found-handling)
*/
// defaultNotFoundComponent?: NotFoundRouteComponent
/**
* A component that will be used to wrap the entire router.
*
* This is useful for providing a context to the entire router.
*
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/solid/api/router/RouterOptionsType#wrap-property)
*/
// Wrap?: (props: { children: any }) => Type<unknown>
/**
* A component that will be used to wrap the inner contents of the router.
*
* This is useful for providing a context to the inner contents of the router where you also need access to the router context and hooks.
*
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/solid/api/router/RouterOptionsType#innerwrap-property)
*/
// InnerWrap?: (props: { children: any }) => Type<unknown>

defaultNotFoundComponent?: () => Type<any>;
/**
* The default `onCatch` handler for errors caught by the Router ErrorBoundary
*
Expand Down Expand Up @@ -94,8 +88,39 @@ export class NgRouter<
TRouterHistory,
TDehydrated
> {
readonly routerState = signal(this.state);
readonly injector = inject(EnvironmentInjector);
injector = inject(EnvironmentInjector);
private platformId = inject(PLATFORM_ID);
private appRef = inject(ApplicationRef);

historyState = linkedSignal(() => this.history);
routerState = linkedSignal(() => this.state);
isTransitioning = signal(false);

private matches = computed(() => this.routerState().matches);

hasPendingMatches = computed(() =>
this.matches().some((match) => match.status === 'pending')
);

isLoading = computed(() => this.routerState().isLoading);
prevIsLoading = linkedSignal<boolean, boolean>({
source: this.isLoading,
computation: (src, prev) => prev?.source ?? src,
});

isAnyPending = computed(
() => this.isLoading() || this.isTransitioning() || this.hasPendingMatches()
);
prevIsAnyPending = linkedSignal<boolean, boolean>({
source: this.isAnyPending,
computation: (src, prev) => prev?.source ?? src,
});

isPagePending = computed(() => this.isLoading() || this.hasPendingMatches());
prevIsPagePending = linkedSignal<boolean, boolean>({
source: this.isPagePending,
computation: (src, prev) => prev?.source ?? src,
});

constructor(
options: RouterConstructorOptions<
Expand All @@ -108,9 +133,93 @@ export class NgRouter<
) {
super(options);

void this.load({ sync: true });
this.__store.subscribe(() => this.routerState.set(this.state));
this.history.subscribe(() => this.load());
if (isPlatformBrowser(this.platformId)) {
this.startTransition = (fn: () => void) => {
this.isTransitioning.set(true);
// NOTE: not quite the same as `React.startTransition` but close enough
queueMicrotask(() => {
fn();
this.isTransitioning.set(false);
this.appRef.tick();
});
};
}

effect((onCleanup) => {
const unsub = this.__store.subscribe(() => {
this.routerState.set(this.state);
});
onCleanup(() => unsub());
});

effect((onCleanup) => {
const unsub = this.history.subscribe(() => {
this.historyState.set(this.history);
void this.load();
});

// track history state
this.historyState();
const nextLocation = this.buildLocation({
to: this.latestLocation.pathname,
search: true,
params: true,
hash: true,
state: true,
_includeValidateSearch: true,
});

if (
trimPathRight(this.latestLocation.href) !==
trimPathRight(nextLocation.href)
) {
void this.commitLocation({ ...nextLocation, replace: true });
}

onCleanup(() => unsub());
});

effect(() => {
const [prevIsLoading, isLoading] = [
this.prevIsLoading(),
this.isLoading(),
];
if (prevIsLoading && !isLoading) {
this.emit({
type: 'onLoad', // When the new URL has committed, when the new matches have been loaded into state.matches
...getLocationChangeInfo(this.state),
});
}
});

effect(() => {
const [prevIsPagePending, isPagePending] = [
this.prevIsPagePending(),
this.isPagePending(),
];
if (prevIsPagePending && !isPagePending) {
this.emit({
type: 'onBeforeRouteMount',
...getLocationChangeInfo(this.state),
});
}
});

effect(() => {
const [prevIsAnyPending, isAnyPending] = [
this.prevIsAnyPending(),
this.isAnyPending(),
];
// The router was pending and now it's not
if (prevIsAnyPending && !isAnyPending) {
this.emit({ type: 'onResolved', ...getLocationChangeInfo(this.state) });
this.__store.setState((s) => ({
...s,
status: 'idle',
resolvedLocation: s.location,
}));
}
});
}

getRouteById(routeId: string) {
Expand Down
11 changes: 11 additions & 0 deletions projects/router/src/lib/not-found.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';

@Component({
selector: 'default-not-found',
template: `
<p>Page not found</p>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
host: { style: 'display: contents;' },
})
export class DefaultNotFound {}
Loading