svelte-tv 1.0.1 → 1.0.2
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,5 +1,6 @@
|
|
|
1
1
|
import type { RouteContextValue } from './types.js';
|
|
2
2
|
export declare function setRouterContext(value: RouteContextValue): void;
|
|
3
|
+
export declare function clearRouterContext(value: RouteContextValue): void;
|
|
3
4
|
export declare function getRouterContext(): RouteContextValue;
|
|
4
5
|
export declare function navigate(...args: Parameters<RouteContextValue['navigate']>): void;
|
|
5
6
|
export declare function params(): import("./types.js").RouteParams;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<script lang="ts">import { onDestroy, onMount } from "svelte";
|
|
2
2
|
import RouteView from "./RouteView.svelte";
|
|
3
|
-
import { setRouterContext } from "./context.js";
|
|
3
|
+
import { clearRouterContext, setRouterContext } from "./context.js";
|
|
4
4
|
import { createLocation, matchRoutes, routeKey } from "./match.js";
|
|
5
5
|
import { setRouteParentContext } from "./routeContext.js";
|
|
6
6
|
let props = $props();
|
|
@@ -20,7 +20,7 @@ const navigate = (href, options) => {
|
|
|
20
20
|
}
|
|
21
21
|
window.location.hash = path;
|
|
22
22
|
};
|
|
23
|
-
|
|
23
|
+
const context = {
|
|
24
24
|
registerRoute(route) {
|
|
25
25
|
routes = [...routes, route];
|
|
26
26
|
return () => {
|
|
@@ -32,7 +32,8 @@ setRouterContext({
|
|
|
32
32
|
params: () => activeParams,
|
|
33
33
|
routeData: () => activeData,
|
|
34
34
|
currentRoute: () => activeRoute
|
|
35
|
-
}
|
|
35
|
+
};
|
|
36
|
+
setRouterContext(context);
|
|
36
37
|
setRouteParentContext({ registerRoute(route) {
|
|
37
38
|
routes = [...routes, route];
|
|
38
39
|
return () => {
|
|
@@ -75,7 +76,10 @@ onMount(() => {
|
|
|
75
76
|
updateLocation();
|
|
76
77
|
window.addEventListener("hashchange", updateLocation);
|
|
77
78
|
});
|
|
78
|
-
onDestroy(() =>
|
|
79
|
+
onDestroy(() => {
|
|
80
|
+
window.removeEventListener("hashchange", updateLocation);
|
|
81
|
+
clearRouterContext(context);
|
|
82
|
+
});
|
|
79
83
|
</script>
|
|
80
84
|
|
|
81
85
|
{@render props.children?.()}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { RouteContextValue } from './types.js';
|
|
2
2
|
export declare function setRouterContext(value: RouteContextValue): void;
|
|
3
|
+
export declare function clearRouterContext(value: RouteContextValue): void;
|
|
3
4
|
export declare function getRouterContext(): RouteContextValue;
|
|
4
5
|
export declare function navigate(...args: Parameters<RouteContextValue['navigate']>): void;
|
|
5
6
|
export declare function params(): import("./types.js").RouteParams;
|
|
@@ -1,11 +1,27 @@
|
|
|
1
1
|
import { getContext, setContext } from 'svelte';
|
|
2
2
|
const routerContext = Symbol('svelte-tv-router');
|
|
3
|
+
let activeRouterContext;
|
|
3
4
|
export function setRouterContext(value) {
|
|
5
|
+
activeRouterContext = value;
|
|
4
6
|
setContext(routerContext, value);
|
|
5
7
|
}
|
|
8
|
+
export function clearRouterContext(value) {
|
|
9
|
+
if (activeRouterContext === value)
|
|
10
|
+
activeRouterContext = undefined;
|
|
11
|
+
}
|
|
6
12
|
export function getRouterContext() {
|
|
7
|
-
|
|
13
|
+
let context;
|
|
14
|
+
try {
|
|
15
|
+
context = getContext(routerContext);
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
if (activeRouterContext)
|
|
19
|
+
return activeRouterContext;
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
8
22
|
if (!context) {
|
|
23
|
+
if (activeRouterContext)
|
|
24
|
+
return activeRouterContext;
|
|
9
25
|
throw new Error('Router context is not available.');
|
|
10
26
|
}
|
|
11
27
|
return context;
|