@tinyfx/runtime 0.1.6 → 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.
Files changed (49) hide show
  1. package/README.md +48 -121
  2. package/dist/__tests__/mount-state.test.d.ts +1 -0
  3. package/dist/__tests__/mount-state.test.js +30 -0
  4. package/dist/__tests__/page-registry.test.d.ts +1 -0
  5. package/dist/__tests__/page-registry.test.js +38 -0
  6. package/dist/__tests__/path-matcher.test.d.ts +1 -0
  7. package/dist/__tests__/path-matcher.test.js +43 -0
  8. package/dist/__tests__/registry.test.d.ts +1 -0
  9. package/dist/__tests__/registry.test.js +67 -0
  10. package/dist/context.d.ts +19 -0
  11. package/dist/context.js +1 -0
  12. package/dist/dom.d.ts +35 -0
  13. package/dist/dom.js +35 -0
  14. package/dist/each.d.ts +10 -0
  15. package/dist/each.js +24 -0
  16. package/dist/http/data.d.ts +76 -0
  17. package/dist/http/data.js +36 -0
  18. package/dist/http/helper.d.ts +20 -0
  19. package/dist/http/helper.js +20 -0
  20. package/dist/http/http.d.ts +13 -0
  21. package/dist/http/http.js +13 -0
  22. package/dist/http.d.ts +2 -10
  23. package/dist/http.js +1 -38
  24. package/dist/index.d.ts +12 -8
  25. package/dist/index.js +11 -7
  26. package/dist/init.d.ts +12 -0
  27. package/dist/init.js +40 -0
  28. package/dist/mount-state.d.ts +17 -0
  29. package/dist/mount-state.js +27 -0
  30. package/dist/page-registry.d.ts +31 -0
  31. package/dist/page-registry.js +38 -0
  32. package/dist/registry.d.ts +31 -0
  33. package/dist/registry.js +49 -0
  34. package/dist/router/active-links.d.ts +3 -0
  35. package/dist/router/active-links.js +3 -0
  36. package/dist/router/index.d.ts +4 -0
  37. package/dist/router/index.js +4 -1
  38. package/dist/router/lifecycle.d.ts +26 -0
  39. package/dist/router/lifecycle.js +31 -1
  40. package/dist/router/navigate.d.ts +8 -0
  41. package/dist/router/navigate.js +8 -0
  42. package/dist/router/params.d.ts +20 -0
  43. package/dist/router/params.js +22 -0
  44. package/dist/router/path-matcher.d.ts +30 -0
  45. package/dist/router/path-matcher.js +45 -0
  46. package/dist/router/types.d.ts +15 -2
  47. package/dist/signals.d.ts +41 -0
  48. package/dist/signals.js +41 -0
  49. package/package.json +6 -3
@@ -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 {};
@@ -6,13 +6,33 @@ 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
- mountCallbacks.push(fn);
19
+ if (document.readyState === "loading") {
20
+ mountCallbacks.push(fn);
21
+ return;
22
+ }
23
+ fn();
12
24
  }
13
25
  /**
14
26
  * Register a callback to run when the user navigates away from this page.
15
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
+ * });
16
36
  */
17
37
  export function onDestroy(fn) {
18
38
  destroyCallbacks.push(fn);
@@ -20,6 +40,11 @@ export function onDestroy(fn) {
20
40
  /**
21
41
  * Trigger all registered `onMount` callbacks.
22
42
  * Called by `initRouter` after matching the current route.
43
+ *
44
+ * @returns Nothing
45
+ *
46
+ * @example
47
+ * flushMount();
23
48
  */
24
49
  export function flushMount() {
25
50
  const run = () => mountCallbacks.forEach((fn) => fn());
@@ -33,6 +58,11 @@ export function flushMount() {
33
58
  /**
34
59
  * Wire up registered `onDestroy` callbacks to the `pagehide` event.
35
60
  * Called by `initRouter` after matching the current route.
61
+ *
62
+ * @returns Nothing
63
+ *
64
+ * @example
65
+ * initLifecycle();
36
66
  */
37
67
  export function initLifecycle() {
38
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;
@@ -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);
@@ -6,13 +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
+ */
38
+ export declare function getParams(): Record<string, string>;
@@ -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,3 +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
+ */
62
+ export function getParams() {
63
+ return Object.assign({}, currentParams);
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
+ }
@@ -1,9 +1,22 @@
1
- /** Metadata for a single route, produced by the compiler. */
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
- /** The full route map emitted by `tinyfx build` into tinyfx.gen.ts. */
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;
package/dist/signals.js CHANGED
@@ -1,6 +1,19 @@
1
1
  // @tinyfx/runtime — signals
2
2
  // Minimal, explicit reactivity — no proxies, no magic.
3
3
  const effectStack = [];
4
+ /**
5
+ * Reactive value container used by TinyFX signals.
6
+ *
7
+ * Reading the value through {@link Signal.get} during an active effect records
8
+ * that effect as a subscriber. Writing through {@link Signal.set} re-runs the
9
+ * affected subscribers.
10
+ *
11
+ * @example
12
+ * const count = new Signal(0);
13
+ * console.log(count.get());
14
+ * count.set(1);
15
+ * console.log(count.get());
16
+ */
4
17
  export class Signal {
5
18
  constructor(v) {
6
19
  this.subs = new Set();
@@ -20,12 +33,40 @@ export class Signal {
20
33
  this.subs.forEach((fn) => fn());
21
34
  }
22
35
  }
36
+ /**
37
+ * Creates a reactive signal function.
38
+ *
39
+ * @param v - The initial value stored by the signal
40
+ * @returns A callable signal that reads the current value and exposes `.set()`
41
+ * to update it
42
+ *
43
+ * @example
44
+ * const count = signal(0);
45
+ * console.log(count());
46
+ * count.set(1);
47
+ */
23
48
  export function signal(v) {
24
49
  const s = new Signal(v);
25
50
  const fn = () => s.get();
26
51
  fn.set = (v) => s.set(v);
27
52
  return fn;
28
53
  }
54
+ /**
55
+ * Registers and runs a reactive effect.
56
+ *
57
+ * Every signal read while `fn` runs is tracked, and the effect is re-run when
58
+ * any of those signal values change.
59
+ *
60
+ * @param fn - The reactive callback to execute and subscribe
61
+ * @returns Nothing
62
+ *
63
+ * @example
64
+ * const count = signal(0);
65
+ * effect(() => {
66
+ * console.log(`count = ${count()}`);
67
+ * });
68
+ * count.set(1);
69
+ */
29
70
  export function effect(fn) {
30
71
  const run = () => {
31
72
  effectStack.push(run);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinyfx/runtime",
3
- "version": "0.1.6",
3
+ "version": "0.1.9",
4
4
  "description": "Minimal frontend runtime — signals, DOM helpers, typed HTTP, DTO mapping",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -10,7 +10,6 @@
10
10
  "./signals": "./dist/signals.js",
11
11
  "./dom": "./dist/dom.js",
12
12
  "./http": "./dist/http.js",
13
- "./di": "./dist/di.js",
14
13
  "./router": "./dist/router/index.js"
15
14
  },
16
15
  "files": [
@@ -18,10 +17,14 @@
18
17
  ],
19
18
  "scripts": {
20
19
  "build": "tsc",
20
+ "test": "vitest run",
21
21
  "prepublishOnly": "npm run build"
22
22
  },
23
23
  "devDependencies": {
24
- "typescript": "^5.9.3"
24
+ "@vitest/coverage-v8": "^4.1.2",
25
+ "jsdom": "^29.0.1",
26
+ "typescript": "^5.9.3",
27
+ "vitest": "^4.1.2"
25
28
  },
26
29
  "license": "MIT"
27
30
  }