@tinyfx/runtime 0.1.8 → 0.1.9
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/dist/__tests__/mount-state.test.d.ts +1 -0
- package/dist/__tests__/mount-state.test.js +30 -0
- package/dist/__tests__/page-registry.test.d.ts +1 -0
- package/dist/__tests__/page-registry.test.js +38 -0
- package/dist/__tests__/path-matcher.test.d.ts +1 -0
- package/dist/__tests__/path-matcher.test.js +43 -0
- package/dist/__tests__/registry.test.d.ts +1 -0
- package/dist/__tests__/registry.test.js +67 -0
- package/dist/context.d.ts +15 -0
- package/dist/context.js +0 -2
- package/dist/dom.d.ts +35 -0
- package/dist/dom.js +35 -0
- package/dist/each.d.ts +10 -0
- package/dist/each.js +24 -0
- package/dist/http/data.d.ts +76 -0
- package/dist/http/data.js +36 -0
- package/dist/http/helper.d.ts +20 -0
- package/dist/http/helper.js +20 -0
- package/dist/http/http.d.ts +13 -0
- package/dist/http/http.js +13 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/init.d.ts +12 -0
- package/dist/init.js +40 -0
- package/dist/mount-state.d.ts +17 -0
- package/dist/mount-state.js +27 -0
- package/dist/page-registry.d.ts +31 -0
- package/dist/page-registry.js +38 -0
- package/dist/registry.d.ts +31 -0
- package/dist/registry.js +49 -0
- package/dist/router/active-links.d.ts +3 -0
- package/dist/router/active-links.js +3 -0
- package/dist/router/index.d.ts +1 -0
- package/dist/router/index.js +1 -0
- package/dist/router/lifecycle.d.ts +26 -0
- package/dist/router/lifecycle.js +26 -0
- package/dist/router/navigate.d.ts +8 -0
- package/dist/router/navigate.js +8 -0
- package/dist/router/params.d.ts +19 -0
- package/dist/router/params.js +19 -0
- package/dist/router/path-matcher.d.ts +30 -0
- package/dist/router/path-matcher.js +45 -0
- package/dist/router/types.d.ts +15 -2
- package/dist/signals.d.ts +41 -0
- package/dist/signals.js +41 -0
- package/package.json +6 -2
package/dist/init.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// @tinyfx/runtime — unified initialization
|
|
2
|
+
// Single entry point for TinyFX bootstrap.
|
|
3
|
+
import { matchPath, splitPath } from "./router/path-matcher";
|
|
4
|
+
import { initLifecycle } from "./router/lifecycle";
|
|
5
|
+
import { highlightActiveLinks } from "./router/active-links";
|
|
6
|
+
import { navigate } from "./router/navigate";
|
|
7
|
+
import { mountComponents } from "./registry";
|
|
8
|
+
import { runPageInit } from "./page-registry";
|
|
9
|
+
let initialized = false;
|
|
10
|
+
export function init(config) {
|
|
11
|
+
if (initialized)
|
|
12
|
+
return null;
|
|
13
|
+
initialized = true;
|
|
14
|
+
const pathname = window.location.pathname;
|
|
15
|
+
const pathSegments = splitPath(pathname);
|
|
16
|
+
let matchedPath = null;
|
|
17
|
+
let params = {};
|
|
18
|
+
for (const [path, def] of Object.entries(config.routes)) {
|
|
19
|
+
const result = matchPath(def, pathSegments);
|
|
20
|
+
if (result) {
|
|
21
|
+
matchedPath = path;
|
|
22
|
+
params = result;
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (!matchedPath) {
|
|
27
|
+
console.warn(`[tinyfx] No route matched for pathname: "${pathname}". ` +
|
|
28
|
+
"Check that a page file exists for this URL in src/pages/.");
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
initLifecycle();
|
|
32
|
+
highlightActiveLinks(pathname);
|
|
33
|
+
const ctx = { params, navigate };
|
|
34
|
+
const instances = mountComponents(ctx);
|
|
35
|
+
if (config.setupDirectives) {
|
|
36
|
+
config.setupDirectives(ctx);
|
|
37
|
+
}
|
|
38
|
+
runPageInit(matchedPath, ctx);
|
|
39
|
+
return { matchedPath, params, instances };
|
|
40
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mark an element as mounted. Returns false if already mounted.
|
|
3
|
+
*
|
|
4
|
+
* @param el - The element to mark
|
|
5
|
+
* @returns true if newly marked, false if already mounted
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* if (!markMounted(el)) return undefined;
|
|
9
|
+
*/
|
|
10
|
+
export declare function markMounted(el: HTMLElement): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Check if an element has been marked as mounted.
|
|
13
|
+
*
|
|
14
|
+
* @param el - The element to check
|
|
15
|
+
* @returns true if the element is marked as mounted
|
|
16
|
+
*/
|
|
17
|
+
export declare function isMounted(el: HTMLElement): boolean;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// @tinyfx/runtime — mount state tracking
|
|
2
|
+
// WeakSet-based mount tracking that is safe for minifier mangling.
|
|
3
|
+
const mounted = new WeakSet();
|
|
4
|
+
/**
|
|
5
|
+
* Mark an element as mounted. Returns false if already mounted.
|
|
6
|
+
*
|
|
7
|
+
* @param el - The element to mark
|
|
8
|
+
* @returns true if newly marked, false if already mounted
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* if (!markMounted(el)) return undefined;
|
|
12
|
+
*/
|
|
13
|
+
export function markMounted(el) {
|
|
14
|
+
if (mounted.has(el))
|
|
15
|
+
return false;
|
|
16
|
+
mounted.add(el);
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Check if an element has been marked as mounted.
|
|
21
|
+
*
|
|
22
|
+
* @param el - The element to check
|
|
23
|
+
* @returns true if the element is marked as mounted
|
|
24
|
+
*/
|
|
25
|
+
export function isMounted(el) {
|
|
26
|
+
return mounted.has(el);
|
|
27
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { TinyFxContext } from "./context";
|
|
2
|
+
type PageModule = {
|
|
3
|
+
init?: (el: HTMLElement, ctx: TinyFxContext) => void;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Register a page module by its route path.
|
|
7
|
+
*
|
|
8
|
+
* @param path - The route path (e.g., "/blog/:slug")
|
|
9
|
+
* @param module - The page module object with optional init()
|
|
10
|
+
* @returns Nothing
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* registerPage("/blog/:slug", PageModule);
|
|
14
|
+
*/
|
|
15
|
+
export declare function registerPage(path: string, module: PageModule): void;
|
|
16
|
+
/**
|
|
17
|
+
* Retrieve a registered page module.
|
|
18
|
+
*
|
|
19
|
+
* @param path - The route path to look up
|
|
20
|
+
* @returns The page module, or undefined if not registered
|
|
21
|
+
*/
|
|
22
|
+
export declare function getPageModule(path: string): PageModule | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Run the init() function for a registered page module.
|
|
25
|
+
*
|
|
26
|
+
* @param path - The route path of the page to initialize
|
|
27
|
+
* @param ctx - The TinyFx context to pass to page init
|
|
28
|
+
* @returns Nothing
|
|
29
|
+
*/
|
|
30
|
+
export declare function runPageInit(path: string, ctx: TinyFxContext): void;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// @tinyfx/runtime — page module registry
|
|
2
|
+
// Decoupled page module registration for tree-shakable bundles.
|
|
3
|
+
const modules = new Map();
|
|
4
|
+
/**
|
|
5
|
+
* Register a page module by its route path.
|
|
6
|
+
*
|
|
7
|
+
* @param path - The route path (e.g., "/blog/:slug")
|
|
8
|
+
* @param module - The page module object with optional init()
|
|
9
|
+
* @returns Nothing
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* registerPage("/blog/:slug", PageModule);
|
|
13
|
+
*/
|
|
14
|
+
export function registerPage(path, module) {
|
|
15
|
+
modules.set(path, module);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Retrieve a registered page module.
|
|
19
|
+
*
|
|
20
|
+
* @param path - The route path to look up
|
|
21
|
+
* @returns The page module, or undefined if not registered
|
|
22
|
+
*/
|
|
23
|
+
export function getPageModule(path) {
|
|
24
|
+
return modules.get(path);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Run the init() function for a registered page module.
|
|
28
|
+
*
|
|
29
|
+
* @param path - The route path of the page to initialize
|
|
30
|
+
* @param ctx - The TinyFx context to pass to page init
|
|
31
|
+
* @returns Nothing
|
|
32
|
+
*/
|
|
33
|
+
export function runPageInit(path, ctx) {
|
|
34
|
+
const mod = modules.get(path);
|
|
35
|
+
if (typeof (mod === null || mod === void 0 ? void 0 : mod.init) === "function") {
|
|
36
|
+
mod.init(document.body, ctx);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { TinyFxContext } from "./context";
|
|
2
|
+
type ComponentFactory = (el: HTMLElement, ctx: TinyFxContext) => any;
|
|
3
|
+
/**
|
|
4
|
+
* Register a component factory by name.
|
|
5
|
+
*
|
|
6
|
+
* @param name - The component name (matches `data-component` attribute)
|
|
7
|
+
* @param factory - A function that mounts the component and returns an instance
|
|
8
|
+
* @returns Nothing
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* registerComponent("Button", (el, ctx) => {
|
|
12
|
+
* return ButtonModule.mount(el, ctx);
|
|
13
|
+
* });
|
|
14
|
+
*/
|
|
15
|
+
export declare function registerComponent(name: string, factory: ComponentFactory): void;
|
|
16
|
+
/**
|
|
17
|
+
* Retrieve a registered component factory.
|
|
18
|
+
*
|
|
19
|
+
* @param name - The component name to look up
|
|
20
|
+
* @returns The factory function, or undefined if not registered
|
|
21
|
+
*/
|
|
22
|
+
export declare function getComponentFactory(name: string): ComponentFactory | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Mount all registered components found within a root element.
|
|
25
|
+
*
|
|
26
|
+
* @param ctx - The TinyFx context to pass to each component
|
|
27
|
+
* @param root - The root element to scan (defaults to document)
|
|
28
|
+
* @returns Array of mounted component instances
|
|
29
|
+
*/
|
|
30
|
+
export declare function mountComponents(ctx: TinyFxContext, root?: Document | HTMLElement): any[];
|
|
31
|
+
export {};
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// @tinyfx/runtime — component registry
|
|
2
|
+
// Decoupled component registration for tree-shakable bundles.
|
|
3
|
+
const factories = new Map();
|
|
4
|
+
/**
|
|
5
|
+
* Register a component factory by name.
|
|
6
|
+
*
|
|
7
|
+
* @param name - The component name (matches `data-component` attribute)
|
|
8
|
+
* @param factory - A function that mounts the component and returns an instance
|
|
9
|
+
* @returns Nothing
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* registerComponent("Button", (el, ctx) => {
|
|
13
|
+
* return ButtonModule.mount(el, ctx);
|
|
14
|
+
* });
|
|
15
|
+
*/
|
|
16
|
+
export function registerComponent(name, factory) {
|
|
17
|
+
factories.set(name, factory);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Retrieve a registered component factory.
|
|
21
|
+
*
|
|
22
|
+
* @param name - The component name to look up
|
|
23
|
+
* @returns The factory function, or undefined if not registered
|
|
24
|
+
*/
|
|
25
|
+
export function getComponentFactory(name) {
|
|
26
|
+
return factories.get(name);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Mount all registered components found within a root element.
|
|
30
|
+
*
|
|
31
|
+
* @param ctx - The TinyFx context to pass to each component
|
|
32
|
+
* @param root - The root element to scan (defaults to document)
|
|
33
|
+
* @returns Array of mounted component instances
|
|
34
|
+
*/
|
|
35
|
+
export function mountComponents(ctx, root = document) {
|
|
36
|
+
const instances = [];
|
|
37
|
+
root.querySelectorAll("[data-component]").forEach((el) => {
|
|
38
|
+
const name = el.getAttribute("data-component");
|
|
39
|
+
if (!name)
|
|
40
|
+
return;
|
|
41
|
+
const factory = factories.get(name);
|
|
42
|
+
if (!factory)
|
|
43
|
+
return;
|
|
44
|
+
const inst = factory(el, ctx);
|
|
45
|
+
if (inst)
|
|
46
|
+
instances.push(inst);
|
|
47
|
+
});
|
|
48
|
+
return instances;
|
|
49
|
+
}
|
|
@@ -5,6 +5,9 @@
|
|
|
5
5
|
* Only the pathname portion of each href is compared (query strings and
|
|
6
6
|
* fragments are ignored).
|
|
7
7
|
*
|
|
8
|
+
* @param pathname - The pathname to compare each link against
|
|
9
|
+
* @returns Nothing
|
|
10
|
+
*
|
|
8
11
|
* @example
|
|
9
12
|
* // In a nav: <a href="/blog">Blog</a>
|
|
10
13
|
* // On the /blog page this element will get data-active="true"
|
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
* Only the pathname portion of each href is compared (query strings and
|
|
7
7
|
* fragments are ignored).
|
|
8
8
|
*
|
|
9
|
+
* @param pathname - The pathname to compare each link against
|
|
10
|
+
* @returns Nothing
|
|
11
|
+
*
|
|
9
12
|
* @example
|
|
10
13
|
* // In a nav: <a href="/blog">Blog</a>
|
|
11
14
|
* // On the /blog page this element will get data-active="true"
|
package/dist/router/index.d.ts
CHANGED
package/dist/router/index.js
CHANGED
|
@@ -4,21 +4,47 @@ type LifecycleCallback = () => void;
|
|
|
4
4
|
*
|
|
5
5
|
* - If the document is still loading, the callback runs after DOMContentLoaded.
|
|
6
6
|
* - If the document is already loaded, the callback runs immediately.
|
|
7
|
+
*
|
|
8
|
+
* @param fn - The callback to run on mount
|
|
9
|
+
* @returns Nothing
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* onMount(() => {
|
|
13
|
+
* console.log("page ready");
|
|
14
|
+
* });
|
|
7
15
|
*/
|
|
8
16
|
export declare function onMount(fn: LifecycleCallback): void;
|
|
9
17
|
/**
|
|
10
18
|
* Register a callback to run when the user navigates away from this page.
|
|
11
19
|
* Fires on the browser's `pagehide` event.
|
|
20
|
+
*
|
|
21
|
+
* @param fn - The callback to run during page teardown
|
|
22
|
+
* @returns Nothing
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* onDestroy(() => {
|
|
26
|
+
* console.log("page leaving");
|
|
27
|
+
* });
|
|
12
28
|
*/
|
|
13
29
|
export declare function onDestroy(fn: LifecycleCallback): void;
|
|
14
30
|
/**
|
|
15
31
|
* Trigger all registered `onMount` callbacks.
|
|
16
32
|
* Called by `initRouter` after matching the current route.
|
|
33
|
+
*
|
|
34
|
+
* @returns Nothing
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* flushMount();
|
|
17
38
|
*/
|
|
18
39
|
export declare function flushMount(): void;
|
|
19
40
|
/**
|
|
20
41
|
* Wire up registered `onDestroy` callbacks to the `pagehide` event.
|
|
21
42
|
* Called by `initRouter` after matching the current route.
|
|
43
|
+
*
|
|
44
|
+
* @returns Nothing
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* initLifecycle();
|
|
22
48
|
*/
|
|
23
49
|
export declare function initLifecycle(): void;
|
|
24
50
|
export {};
|
package/dist/router/lifecycle.js
CHANGED
|
@@ -6,6 +6,14 @@ const destroyCallbacks = [];
|
|
|
6
6
|
*
|
|
7
7
|
* - If the document is still loading, the callback runs after DOMContentLoaded.
|
|
8
8
|
* - If the document is already loaded, the callback runs immediately.
|
|
9
|
+
*
|
|
10
|
+
* @param fn - The callback to run on mount
|
|
11
|
+
* @returns Nothing
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* onMount(() => {
|
|
15
|
+
* console.log("page ready");
|
|
16
|
+
* });
|
|
9
17
|
*/
|
|
10
18
|
export function onMount(fn) {
|
|
11
19
|
if (document.readyState === "loading") {
|
|
@@ -17,6 +25,14 @@ export function onMount(fn) {
|
|
|
17
25
|
/**
|
|
18
26
|
* Register a callback to run when the user navigates away from this page.
|
|
19
27
|
* Fires on the browser's `pagehide` event.
|
|
28
|
+
*
|
|
29
|
+
* @param fn - The callback to run during page teardown
|
|
30
|
+
* @returns Nothing
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* onDestroy(() => {
|
|
34
|
+
* console.log("page leaving");
|
|
35
|
+
* });
|
|
20
36
|
*/
|
|
21
37
|
export function onDestroy(fn) {
|
|
22
38
|
destroyCallbacks.push(fn);
|
|
@@ -24,6 +40,11 @@ export function onDestroy(fn) {
|
|
|
24
40
|
/**
|
|
25
41
|
* Trigger all registered `onMount` callbacks.
|
|
26
42
|
* Called by `initRouter` after matching the current route.
|
|
43
|
+
*
|
|
44
|
+
* @returns Nothing
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* flushMount();
|
|
27
48
|
*/
|
|
28
49
|
export function flushMount() {
|
|
29
50
|
const run = () => mountCallbacks.forEach((fn) => fn());
|
|
@@ -37,6 +58,11 @@ export function flushMount() {
|
|
|
37
58
|
/**
|
|
38
59
|
* Wire up registered `onDestroy` callbacks to the `pagehide` event.
|
|
39
60
|
* Called by `initRouter` after matching the current route.
|
|
61
|
+
*
|
|
62
|
+
* @returns Nothing
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* initLifecycle();
|
|
40
66
|
*/
|
|
41
67
|
export function initLifecycle() {
|
|
42
68
|
flushMount();
|
|
@@ -2,11 +2,19 @@
|
|
|
2
2
|
* Navigate to the given path.
|
|
3
3
|
* This is a full browser navigation — not a DOM swap.
|
|
4
4
|
*
|
|
5
|
+
* @param path - The destination pathname or URL
|
|
6
|
+
* @returns Nothing
|
|
7
|
+
*
|
|
5
8
|
* @example
|
|
6
9
|
* navigate("/blog/hello-world");
|
|
7
10
|
*/
|
|
8
11
|
export declare function navigate(path: string): void;
|
|
9
12
|
/**
|
|
10
13
|
* Go back one step in the browser history.
|
|
14
|
+
*
|
|
15
|
+
* @returns Nothing
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* goBack();
|
|
11
19
|
*/
|
|
12
20
|
export declare function goBack(): void;
|
package/dist/router/navigate.js
CHANGED
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
* Navigate to the given path.
|
|
5
5
|
* This is a full browser navigation — not a DOM swap.
|
|
6
6
|
*
|
|
7
|
+
* @param path - The destination pathname or URL
|
|
8
|
+
* @returns Nothing
|
|
9
|
+
*
|
|
7
10
|
* @example
|
|
8
11
|
* navigate("/blog/hello-world");
|
|
9
12
|
*/
|
|
@@ -12,6 +15,11 @@ export function navigate(path) {
|
|
|
12
15
|
}
|
|
13
16
|
/**
|
|
14
17
|
* Go back one step in the browser history.
|
|
18
|
+
*
|
|
19
|
+
* @returns Nothing
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* goBack();
|
|
15
23
|
*/
|
|
16
24
|
export function goBack() {
|
|
17
25
|
window.history.go(-1);
|
package/dist/router/params.d.ts
CHANGED
|
@@ -6,14 +6,33 @@
|
|
|
6
6
|
*
|
|
7
7
|
* Static segments must match exactly; `:param` segments match any non-empty
|
|
8
8
|
* path segment.
|
|
9
|
+
*
|
|
10
|
+
* @param pattern - The route pattern produced by the compiler
|
|
11
|
+
* @param pathname - The current browser pathname
|
|
12
|
+
* @returns `true` when the pathname matches the pattern, otherwise `false`
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* resolveParams("/blog/:slug", "/blog/hello-world");
|
|
9
16
|
*/
|
|
10
17
|
export declare function resolveParams(pattern: string, pathname: string): boolean;
|
|
11
18
|
/**
|
|
12
19
|
* Retrieve a URL parameter resolved for the current page.
|
|
13
20
|
*
|
|
21
|
+
* @param key - The route parameter name to read
|
|
22
|
+
* @returns The decoded parameter value, or `undefined` when it is missing
|
|
23
|
+
*
|
|
14
24
|
* @example
|
|
15
25
|
* // On the page with route "/blog/:slug" and URL "/blog/hello-world"
|
|
16
26
|
* getParam("slug"); // → "hello-world"
|
|
17
27
|
*/
|
|
18
28
|
export declare function getParam(key: string): string | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Returns a shallow copy of all currently resolved route params.
|
|
31
|
+
*
|
|
32
|
+
* @returns An object containing the current route parameters
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const params = getParams();
|
|
36
|
+
* console.log(params.slug);
|
|
37
|
+
*/
|
|
19
38
|
export declare function getParams(): Record<string, string>;
|
package/dist/router/params.js
CHANGED
|
@@ -9,6 +9,13 @@ let currentParams = {};
|
|
|
9
9
|
*
|
|
10
10
|
* Static segments must match exactly; `:param` segments match any non-empty
|
|
11
11
|
* path segment.
|
|
12
|
+
*
|
|
13
|
+
* @param pattern - The route pattern produced by the compiler
|
|
14
|
+
* @param pathname - The current browser pathname
|
|
15
|
+
* @returns `true` when the pathname matches the pattern, otherwise `false`
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* resolveParams("/blog/:slug", "/blog/hello-world");
|
|
12
19
|
*/
|
|
13
20
|
export function resolveParams(pattern, pathname) {
|
|
14
21
|
const patternParts = pattern.split("/").filter(Boolean);
|
|
@@ -33,6 +40,9 @@ export function resolveParams(pattern, pathname) {
|
|
|
33
40
|
/**
|
|
34
41
|
* Retrieve a URL parameter resolved for the current page.
|
|
35
42
|
*
|
|
43
|
+
* @param key - The route parameter name to read
|
|
44
|
+
* @returns The decoded parameter value, or `undefined` when it is missing
|
|
45
|
+
*
|
|
36
46
|
* @example
|
|
37
47
|
* // On the page with route "/blog/:slug" and URL "/blog/hello-world"
|
|
38
48
|
* getParam("slug"); // → "hello-world"
|
|
@@ -40,6 +50,15 @@ export function resolveParams(pattern, pathname) {
|
|
|
40
50
|
export function getParam(key) {
|
|
41
51
|
return currentParams[key];
|
|
42
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns a shallow copy of all currently resolved route params.
|
|
55
|
+
*
|
|
56
|
+
* @returns An object containing the current route parameters
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* const params = getParams();
|
|
60
|
+
* console.log(params.slug);
|
|
61
|
+
*/
|
|
43
62
|
export function getParams() {
|
|
44
63
|
return Object.assign({}, currentParams);
|
|
45
64
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface RouteDef {
|
|
2
|
+
/** Static segment values (null for dynamic segments) */
|
|
3
|
+
staticSegments: (string | null)[];
|
|
4
|
+
/** Param names at dynamic positions */
|
|
5
|
+
paramNames: string[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Match a pre-compiled route definition against path segments.
|
|
9
|
+
*
|
|
10
|
+
* @param def - The route definition from the compiler
|
|
11
|
+
* @param pathSegments - Pre-split path segments from splitPath()
|
|
12
|
+
* @returns Captured params on match, null on mismatch
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const def = { staticSegments: ["blog", null], paramNames: ["slug"] };
|
|
16
|
+
* matchPath(def, ["blog", "hello-world"]);
|
|
17
|
+
* // Returns: { slug: "hello-world" }
|
|
18
|
+
*/
|
|
19
|
+
export declare function matchPath(def: RouteDef, pathSegments: string[]): Record<string, string> | null;
|
|
20
|
+
/**
|
|
21
|
+
* Split a pathname into segments for matching.
|
|
22
|
+
*
|
|
23
|
+
* @param pathname - The browser pathname (e.g., "/blog/hello")
|
|
24
|
+
* @returns Array of non-empty segments
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* splitPath("/blog/hello-world");
|
|
28
|
+
* // Returns: ["blog", "hello-world"]
|
|
29
|
+
*/
|
|
30
|
+
export declare function splitPath(pathname: string): string[];
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// @tinyfx/runtime — path matcher
|
|
2
|
+
// Unified path resolution with pre-split array support for compiler optimization.
|
|
3
|
+
/**
|
|
4
|
+
* Match a pre-compiled route definition against path segments.
|
|
5
|
+
*
|
|
6
|
+
* @param def - The route definition from the compiler
|
|
7
|
+
* @param pathSegments - Pre-split path segments from splitPath()
|
|
8
|
+
* @returns Captured params on match, null on mismatch
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const def = { staticSegments: ["blog", null], paramNames: ["slug"] };
|
|
12
|
+
* matchPath(def, ["blog", "hello-world"]);
|
|
13
|
+
* // Returns: { slug: "hello-world" }
|
|
14
|
+
*/
|
|
15
|
+
export function matchPath(def, pathSegments) {
|
|
16
|
+
const { staticSegments, paramNames } = def;
|
|
17
|
+
if (staticSegments.length !== pathSegments.length)
|
|
18
|
+
return null;
|
|
19
|
+
const params = {};
|
|
20
|
+
let paramIdx = 0;
|
|
21
|
+
for (let i = 0; i < staticSegments.length; i++) {
|
|
22
|
+
const expected = staticSegments[i];
|
|
23
|
+
const actual = pathSegments[i];
|
|
24
|
+
if (expected === null) {
|
|
25
|
+
params[paramNames[paramIdx++]] = decodeURIComponent(actual);
|
|
26
|
+
}
|
|
27
|
+
else if (expected !== actual) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return params;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Split a pathname into segments for matching.
|
|
35
|
+
*
|
|
36
|
+
* @param pathname - The browser pathname (e.g., "/blog/hello")
|
|
37
|
+
* @returns Array of non-empty segments
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* splitPath("/blog/hello-world");
|
|
41
|
+
* // Returns: ["blog", "hello-world"]
|
|
42
|
+
*/
|
|
43
|
+
export function splitPath(pathname) {
|
|
44
|
+
return pathname.split("/").filter(Boolean);
|
|
45
|
+
}
|
package/dist/router/types.d.ts
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Metadata for a single route produced by the compiler.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* const route: RouteMeta = { page: "blog-post", path: "/blog/:slug" };
|
|
6
|
+
*/
|
|
2
7
|
export interface RouteMeta {
|
|
3
8
|
/** The page name, derived from the source filename. */
|
|
4
9
|
page: string;
|
|
5
10
|
/** The declared URL pattern, e.g. "/blog/:slug". */
|
|
6
11
|
path: string;
|
|
7
12
|
}
|
|
8
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* The full route map emitted by `tinyfx build` into `tinyfx.gen.ts`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const routes: RouteMap = {
|
|
18
|
+
* "/": { page: "index", path: "/" },
|
|
19
|
+
* "/blog/:slug": { page: "blog-post", path: "/blog/:slug" },
|
|
20
|
+
* };
|
|
21
|
+
*/
|
|
9
22
|
export type RouteMap = Record<string, RouteMeta>;
|
package/dist/signals.d.ts
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactive value container used by TinyFX signals.
|
|
3
|
+
*
|
|
4
|
+
* Reading the value through {@link Signal.get} during an active effect records
|
|
5
|
+
* that effect as a subscriber. Writing through {@link Signal.set} re-runs the
|
|
6
|
+
* affected subscribers.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const count = new Signal(0);
|
|
10
|
+
* console.log(count.get());
|
|
11
|
+
* count.set(1);
|
|
12
|
+
* console.log(count.get());
|
|
13
|
+
*/
|
|
1
14
|
export declare class Signal<T> {
|
|
2
15
|
private value;
|
|
3
16
|
private subs;
|
|
@@ -5,7 +18,35 @@ export declare class Signal<T> {
|
|
|
5
18
|
get(): T;
|
|
6
19
|
set(v: T): void;
|
|
7
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Creates a reactive signal function.
|
|
23
|
+
*
|
|
24
|
+
* @param v - The initial value stored by the signal
|
|
25
|
+
* @returns A callable signal that reads the current value and exposes `.set()`
|
|
26
|
+
* to update it
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const count = signal(0);
|
|
30
|
+
* console.log(count());
|
|
31
|
+
* count.set(1);
|
|
32
|
+
*/
|
|
8
33
|
export declare function signal<T>(v: T): (() => T) & {
|
|
9
34
|
set(v: T): void;
|
|
10
35
|
};
|
|
36
|
+
/**
|
|
37
|
+
* Registers and runs a reactive effect.
|
|
38
|
+
*
|
|
39
|
+
* Every signal read while `fn` runs is tracked, and the effect is re-run when
|
|
40
|
+
* any of those signal values change.
|
|
41
|
+
*
|
|
42
|
+
* @param fn - The reactive callback to execute and subscribe
|
|
43
|
+
* @returns Nothing
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* const count = signal(0);
|
|
47
|
+
* effect(() => {
|
|
48
|
+
* console.log(`count = ${count()}`);
|
|
49
|
+
* });
|
|
50
|
+
* count.set(1);
|
|
51
|
+
*/
|
|
11
52
|
export declare function effect(fn: () => void): void;
|