@real-router/svelte 0.16.2 → 0.16.4

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.
@@ -1,3 +1,4 @@
1
+ import { createRouteEnterGate } from "@real-router/sources";
1
2
  import { useRoute } from "./useRoute.svelte";
2
3
  /**
3
4
  * Fire `handler` once when the component mounts as a result of a
@@ -53,41 +54,19 @@ import { useRoute } from "./useRoute.svelte";
53
54
  export function useRouteEnter(handler, options) {
54
55
  const { route, previousRoute } = useRoute();
55
56
  const skipSameRoute = options?.skipSameRoute ?? true;
56
- let lastHandledRoute = null;
57
+ // The canonical enter-guard set + `lastHandledRoute` dedupe live in the
58
+ // shared gate (@real-router/sources, #1435). Svelte composables run once, so
59
+ // a plain const holds the gate across `$effect` re-runs. The gate's leading
60
+ // `!route` arm folds in svelte's SSR / pre-start guard (`route.current` may
61
+ // be `undefined`), and the gate owns skip-initial / same-route / dedupe / the
62
+ // `!previousRoute` guard — the sole defense of the non-nullable
63
+ // `RouteEnterContext.previousRoute` contract (#1218), now tested once in
64
+ // sources rather than v8-ignored per adapter.
65
+ const gate = createRouteEnterGate();
57
66
  $effect(() => {
58
- const currentRoute = route.current;
59
- const prev = previousRoute.current;
60
- // Early-exit guards, top-down:
61
- //
62
- // - **Defensive**: `route.current` may be undefined during SSR or
63
- // pre-start hydration. Not testable from vitest, v8-ignored.
64
- // - **Skip-initial**: `state.transition.from` is undefined only
65
- // for the very first state committed by `router.start()`.
66
- // - **Skip-same-route**: query-only navigations have
67
- // `transition.from === route.name`. Opt-out via
68
- // `skipSameRoute: false`.
69
- // - **Defensive dedupe + missing `previousRoute`**: same `route`
70
- // ref between `$effect` re-runs is unexpected (createSubscriber
71
- // only fires on real reference changes); `!prev` is unreachable
72
- // once `transition.from` is set (core populates them together).
73
- // Both kept for parity with React; v8-ignored.
74
- /* v8 ignore start */
75
- if (!currentRoute) {
76
- return;
67
+ const context = gate(route.current, previousRoute.current, skipSameRoute);
68
+ if (context) {
69
+ handler(context);
77
70
  }
78
- /* v8 ignore stop */
79
- if (!currentRoute.transition.from) {
80
- return;
81
- }
82
- if (skipSameRoute && currentRoute.transition.from === currentRoute.name) {
83
- return;
84
- }
85
- /* v8 ignore start */
86
- if (lastHandledRoute === currentRoute || !prev) {
87
- return;
88
- }
89
- /* v8 ignore stop */
90
- lastHandledRoute = currentRoute;
91
- handler({ route: currentRoute, previousRoute: prev });
92
71
  });
93
72
  }
@@ -38,8 +38,10 @@ export type RouteExitHandler = (context: RouteExitContext) => void | Promise<voi
38
38
  * during component initialization (synchronous in `<script>`).
39
39
  *
40
40
  * If the handler returns a Promise, the router blocks on it. If the
41
- * Promise resolves, navigation proceeds. If it rejects, the router emits
42
- * `TRANSITION_CANCELLED`.
41
+ * Promise resolves, navigation proceeds. If it **rejects**, the router
42
+ * rejects `navigate()` with the handler's **original error** and emits
43
+ * `TRANSITION_ERROR` — it is NOT re-coded to `TRANSITION_CANCELLED` (that
44
+ * arises only when the navigation's `signal` aborts).
43
45
  *
44
46
  * **Handler reactivity (Svelte):** Svelte composables run **once** at
45
47
  * component init; `handler` is captured in closure at the call site. To
@@ -1,3 +1,4 @@
1
+ import { guardLeaveListener } from "@real-router/sources";
1
2
  import { onDestroy } from "svelte";
2
3
  import { useRouter } from "./useRouter.svelte";
3
4
  /**
@@ -16,8 +17,10 @@ import { useRouter } from "./useRouter.svelte";
16
17
  * during component initialization (synchronous in `<script>`).
17
18
  *
18
19
  * If the handler returns a Promise, the router blocks on it. If the
19
- * Promise resolves, navigation proceeds. If it rejects, the router emits
20
- * `TRANSITION_CANCELLED`.
20
+ * Promise resolves, navigation proceeds. If it **rejects**, the router
21
+ * rejects `navigate()` with the handler's **original error** and emits
22
+ * `TRANSITION_ERROR` — it is NOT re-coded to `TRANSITION_CANCELLED` (that
23
+ * arises only when the navigation's `signal` aborts).
21
24
  *
22
25
  * **Handler reactivity (Svelte):** Svelte composables run **once** at
23
26
  * component init; `handler` is captured in closure at the call site. To
@@ -61,14 +64,9 @@ import { useRouter } from "./useRouter.svelte";
61
64
  export function useRouteExit(handler, options) {
62
65
  const router = useRouter();
63
66
  const skipSameRoute = options?.skipSameRoute ?? true;
64
- const off = router.subscribeLeave(({ route, nextRoute, signal }) => {
65
- if (skipSameRoute && route.name === nextRoute.name) {
66
- return;
67
- }
68
- if (signal.aborted) {
69
- return;
70
- }
71
- return handler({ route, nextRoute, signal });
72
- });
67
+ // The same-route + reentrant-abort guards and the Promise passthrough live
68
+ // in the shared listener (@real-router/sources, #1435); the handler is
69
+ // captured at init (Svelte composables run once).
70
+ const off = router.subscribeLeave(guardLeaveListener(handler, { skipSameRoute }));
73
71
  onDestroy(off);
74
72
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@real-router/svelte",
3
- "version": "0.16.2",
3
+ "version": "0.16.4",
4
4
  "type": "module",
5
5
  "description": "Svelte 5 integration for Real-Router",
6
6
  "svelte": "./dist/index.js",
@@ -48,9 +48,9 @@
48
48
  "license": "MIT",
49
49
  "sideEffects": false,
50
50
  "dependencies": {
51
- "@real-router/core": "^0.76.0",
52
- "@real-router/sources": "^0.11.4",
53
- "@real-router/route-utils": "^0.2.8"
51
+ "@real-router/core": "^0.77.2",
52
+ "@real-router/route-utils": "^0.2.8",
53
+ "@real-router/sources": "^0.12.0"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@sveltejs/package": "2.5.8",
@@ -63,7 +63,7 @@
63
63
  "svelte": "~5.56.4",
64
64
  "svelte-check": "4.6.0",
65
65
  "svelte-eslint-parser": "1.8.0",
66
- "@real-router/browser-plugin": "^0.18.20"
66
+ "@real-router/browser-plugin": "^0.18.21"
67
67
  },
68
68
  "peerDependencies": {
69
69
  "svelte": ">=5.7.0"