@effectweb/tanstack-router 0.8.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,7 +6,7 @@ EffectWeb adapter for TanStack Router Core.
6
6
  - `RootRoute` and `Route` expose TanStack's route constructors: matching, params, validated search, redirects, loaders and errors remain in the core.
7
7
  - `mountRouter(router)` owns the history subscription in an Effect scope and returns an EffectWeb `Source` of router state.
8
8
  - The store adapter batches publications and copies plain state before EffectWeb freezes it. It never freezes TanStack's internal match records.
9
- - The host program subscribes to that Source and renders its own matching views. Scope cleanup releases the Source, subscriptions and owned history.
9
+ - The host program subscribes to that Source and renders its own matching views. Scope cleanup interrupts loaders, before-load hooks and preloads, settles pending navigation, and releases the Source, subscriptions and owned history. Mount each router only once.
10
10
  - `createLink(router)` returns a typed `<Link>`: `to`, `params` and `search` are checked against the route tree. It renders a real `<a href>` (middle-click, copy link and screen readers keep working) and navigates in place on plain left clicks. Pass `active` from your route model to set `aria-current="page"`.
11
11
 
12
12
  ```tsx
@@ -20,3 +20,5 @@ const Link = createLink(router);
20
20
  Prefer `<Link>` to buttons that call `navigate(path: string)`: string paths lose route typing, and buttons are not links for keyboard, assistive technology or new-tab navigation.
21
21
 
22
22
  Currently there is no JSX Outlet API, route-level view/error boundary system, SSR adapter or file-route generator. Those can build on this binding without introducing a second route matcher.
23
+
24
+ Link preloading and automatic active matching are not exposed as link props. Call `router.preloadRoute(options)` explicitly when needed, and derive `active` from the route model.
package/dist/index.js CHANGED
@@ -33,6 +33,7 @@ const snapshotCopy = (value, seen = /* @__PURE__ */ new WeakMap()) => {
33
33
  return copy;
34
34
  };
35
35
  const publications = /* @__PURE__ */ new WeakMap();
36
+ const closedRouters = /* @__PURE__ */ new WeakSet();
36
37
  const createRouter = (options) => {
37
38
  const listeners = /* @__PURE__ */ new Set();
38
39
  let batchDepth = 0;
@@ -67,9 +68,36 @@ const createRouter = (options) => {
67
68
  }
68
69
  }
69
70
  }));
71
+ const load = instance.load;
72
+ const navigate = instance.navigate;
73
+ const preload = instance.preloadRoute;
74
+ const closed = () => new Error("Cannot use a disposed router");
75
+ instance.load = (...args) => closedRouters.has(instance) ? Promise.reject(closed()) : load(...args);
76
+ instance.navigate = (...args) => closedRouters.has(instance) ? Promise.reject(closed()) : navigate(...args);
77
+ instance.preloadRoute = (...args) => closedRouters.has(instance) ? Promise.reject(closed()) : preload(...args);
70
78
  publications.set(instance, listeners);
71
79
  return instance;
72
80
  };
81
+ const mountedRouters = /* @__PURE__ */ new WeakSet();
82
+ function stopRouter(router) {
83
+ closedRouters.add(router);
84
+ const transaction = router._tx;
85
+ const preflight = router._preflight;
86
+ const flights = [...router._flights?.values() ?? []];
87
+ delete router._tx;
88
+ delete router._preflight;
89
+ router._flights?.clear();
90
+ if (router._pending?.[3] !== void 0) clearTimeout(router._pending[3]);
91
+ delete router._pending;
92
+ const rendered = router._rendered;
93
+ delete router._rendered;
94
+ rendered?.[1]?.(false);
95
+ preflight?.abort();
96
+ transaction?.[0].abort();
97
+ for (const flight of flights) flight[1].abort();
98
+ router.clearCache();
99
+ router._commitPromise?.resolve();
100
+ }
73
101
  const mountRouter = (router) => Effect.gen(function* () {
74
102
  const source = yield* Effect.acquireRelease(
75
103
  Effect.sync(() => {
@@ -77,6 +105,8 @@ const mountRouter = (router) => Effect.gen(function* () {
77
105
  const history = router.history;
78
106
  if (!listeners)
79
107
  throw new Error("Use @effectweb/tanstack-router.createRouter before mountRouter");
108
+ if (mountedRouters.has(router)) throw new Error("A router can only be mounted once");
109
+ mountedRouters.add(router);
80
110
  const source2 = projectionSource({
81
111
  project: () => snapshotCopy(router.state)
82
112
  });
@@ -91,6 +121,7 @@ const mountRouter = (router) => Effect.gen(function* () {
91
121
  unsubscribe();
92
122
  listeners.delete(source2.changed);
93
123
  source2.dispose();
124
+ stopRouter(router);
94
125
  history.flush();
95
126
  history.destroy();
96
127
  }
package/dist/link.d.ts CHANGED
@@ -1,15 +1,21 @@
1
1
  import { type AnyRouter, type LinkOptions } from '@tanstack/router-core';
2
2
  import type { JSX } from 'effectweb';
3
3
  import { jsxComponent } from 'effectweb/jsx';
4
+ type SupportedLinkOptions<R extends AnyRouter, F extends string, T extends string | undefined> = LinkOptions<R, F, T> & {
5
+ readonly preload?: never;
6
+ readonly preloadDelay?: never;
7
+ readonly preloadIntentProximity?: never;
8
+ readonly activeOptions?: never;
9
+ };
4
10
  type Anchor = Omit<JSX.IntrinsicElements['a'], 'href' | 'onClick' | 'target' | 'children'>;
5
- export type LinkProps<Router extends AnyRouter, From extends string = string, To extends string | undefined = '.'> = LinkOptions<Router, From, To> & Anchor & {
11
+ export type LinkProps<Router extends AnyRouter, From extends string = string, To extends string | undefined = '.'> = SupportedLinkOptions<Router, From, To> & Anchor & {
6
12
  /** Mark the link as the current page (`aria-current="page"`). Derive it from your route model. */
7
13
  readonly active?: boolean;
8
14
  readonly children?: JSX.Element;
9
15
  };
10
16
  type ClickEvent = Pick<MouseEvent, 'button' | 'metaKey' | 'altKey' | 'ctrlKey' | 'shiftKey' | 'defaultPrevented' | 'preventDefault'>;
11
17
  /** Resolve a typed destination to an href and a click handler that navigates in place. */
12
- export declare function linkTarget(router: AnyRouter, props: LinkOptions<AnyRouter, string, string> & Pick<Anchor, 'download'>): {
18
+ export declare function linkTarget(router: AnyRouter, props: SupportedLinkOptions<AnyRouter, string, string> & Pick<Anchor, 'download'>): {
13
19
  href: string | undefined;
14
20
  onClick: (event: ClickEvent) => void;
15
21
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effectweb/tanstack-router",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,7 +28,7 @@
28
28
  "@tanstack/router-core": "1.171.34"
29
29
  },
30
30
  "peerDependencies": {
31
- "effect": "4.0.0-rc.118",
32
- "effectweb": "^0.8.1"
31
+ "effect": "^4.0.0",
32
+ "effectweb": "0.9.0"
33
33
  }
34
34
  }