@seahax/elemental 0.4.2 → 0.5.1

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 (99) hide show
  1. package/README.md +67 -14
  2. package/dist/{store.js → createStore.js} +3 -3
  3. package/dist/createStore.js.map +1 -0
  4. package/dist/{component.d.ts → defineComponent.d.ts} +11 -7
  5. package/dist/defineComponent.js +57 -0
  6. package/dist/defineComponent.js.map +1 -0
  7. package/dist/{router.d.ts → getRouter.d.ts} +3 -2
  8. package/dist/getRouter.js +33 -0
  9. package/dist/getRouter.js.map +1 -0
  10. package/dist/hooks/useAsync.d.ts +11 -0
  11. package/dist/hooks/{loading.js → useAsync.js} +5 -5
  12. package/dist/hooks/useAsync.js.map +1 -0
  13. package/dist/hooks/{attributes.d.ts → useAttributes.d.ts} +1 -1
  14. package/dist/hooks/useAttributes.js +37 -0
  15. package/dist/hooks/useAttributes.js.map +1 -0
  16. package/dist/hooks/useChildEffect.js +24 -0
  17. package/dist/hooks/useChildEffect.js.map +1 -0
  18. package/dist/hooks/useDisconnect.d.ts +2 -0
  19. package/dist/hooks/useDisconnect.js +9 -0
  20. package/dist/hooks/useDisconnect.js.map +1 -0
  21. package/dist/hooks/useDocument.d.ts +3 -0
  22. package/dist/hooks/useDocument.js +9 -0
  23. package/dist/hooks/useDocument.js.map +1 -0
  24. package/dist/hooks/useEffect.d.ts +3 -0
  25. package/dist/hooks/useEffect.js +17 -0
  26. package/dist/hooks/useEffect.js.map +1 -0
  27. package/dist/hooks/useElementInternals.d.ts +2 -0
  28. package/dist/hooks/useElementInternals.js +9 -0
  29. package/dist/hooks/useElementInternals.js.map +1 -0
  30. package/dist/hooks/useForm.d.ts +9 -0
  31. package/dist/hooks/useForm.js +23 -0
  32. package/dist/hooks/useForm.js.map +1 -0
  33. package/dist/hooks/{host.d.ts → useHost.d.ts} +1 -1
  34. package/dist/hooks/useHost.js +9 -0
  35. package/dist/hooks/useHost.js.map +1 -0
  36. package/dist/hooks/useInternalController.d.ts +3 -0
  37. package/dist/hooks/useInternalController.js +11 -0
  38. package/dist/hooks/useInternalController.js.map +1 -0
  39. package/dist/hooks/useParent.d.ts +3 -0
  40. package/dist/hooks/useParent.js +9 -0
  41. package/dist/hooks/useParent.js.map +1 -0
  42. package/dist/hooks/{ref.d.ts → useRef.d.ts} +1 -2
  43. package/dist/hooks/useRef.js +9 -0
  44. package/dist/hooks/useRef.js.map +1 -0
  45. package/dist/hooks/{route.d.ts → useRoute.d.ts} +3 -3
  46. package/dist/hooks/{route.js → useRoute.js} +6 -6
  47. package/dist/hooks/useRoute.js.map +1 -0
  48. package/dist/hooks/{store.d.ts → useStore.d.ts} +2 -2
  49. package/dist/hooks/{store.js → useStore.js} +4 -4
  50. package/dist/hooks/useStore.js.map +1 -0
  51. package/dist/html.d.ts +1 -1
  52. package/dist/html.js.map +1 -1
  53. package/dist/index.d.ts +15 -11
  54. package/dist/index.js +17 -13
  55. package/dist/internal/constants.d.ts +1 -0
  56. package/dist/internal/constants.js +6 -0
  57. package/dist/internal/constants.js.map +1 -0
  58. package/dist/internal/controllers.d.ts +2 -0
  59. package/dist/internal/controllers.js +6 -0
  60. package/dist/internal/controllers.js.map +1 -0
  61. package/dist/internal/{callbacks.js → createCallbacks.js} +2 -2
  62. package/dist/internal/createCallbacks.js.map +1 -0
  63. package/dist/internal/createController.d.ts +33 -0
  64. package/dist/internal/createController.js +73 -0
  65. package/dist/internal/createController.js.map +1 -0
  66. package/dist/internal/getOrInsert.d.ts +4 -0
  67. package/dist/internal/getOrInsert.js +10 -0
  68. package/dist/internal/getOrInsert.js.map +1 -0
  69. package/package.json +7 -3
  70. package/dist/component.js +0 -34
  71. package/dist/component.js.map +0 -1
  72. package/dist/hooks/attributes.js +0 -25
  73. package/dist/hooks/attributes.js.map +0 -1
  74. package/dist/hooks/child-effect.js +0 -14
  75. package/dist/hooks/child-effect.js.map +0 -1
  76. package/dist/hooks/context.d.ts +0 -3
  77. package/dist/hooks/context.js +0 -11
  78. package/dist/hooks/context.js.map +0 -1
  79. package/dist/hooks/effect.d.ts +0 -9
  80. package/dist/hooks/effect.js +0 -20
  81. package/dist/hooks/effect.js.map +0 -1
  82. package/dist/hooks/host.js +0 -9
  83. package/dist/hooks/host.js.map +0 -1
  84. package/dist/hooks/loading.d.ts +0 -11
  85. package/dist/hooks/loading.js.map +0 -1
  86. package/dist/hooks/ref.js +0 -19
  87. package/dist/hooks/ref.js.map +0 -1
  88. package/dist/hooks/route.js.map +0 -1
  89. package/dist/hooks/store.js.map +0 -1
  90. package/dist/internal/callbacks.js.map +0 -1
  91. package/dist/internal/context.d.ts +0 -13
  92. package/dist/internal/context.js +0 -32
  93. package/dist/internal/context.js.map +0 -1
  94. package/dist/router.js +0 -35
  95. package/dist/router.js.map +0 -1
  96. package/dist/store.js.map +0 -1
  97. /package/dist/{store.d.ts → createStore.d.ts} +0 -0
  98. /package/dist/hooks/{child-effect.d.ts → useChildEffect.d.ts} +0 -0
  99. /package/dist/internal/{callbacks.d.ts → createCallbacks.d.ts} +0 -0
@@ -1,20 +0,0 @@
1
- import { createCallbacks as e } from "../internal/callbacks.js";
2
- import { useContext as t } from "./context.js";
3
- //#region src/hooks/effect.ts
4
- function n(n, r) {
5
- let { onNotify: i, onDisconnect: a } = t(), o = e(), s = () => o.runAndClear(), c;
6
- i.push(() => {
7
- let e = n.map((e) => e.value);
8
- if (c?.length === e.length && c?.every((t, n) => t === e[n])) return;
9
- c = e, s();
10
- let t = r(...c);
11
- t && o.push(() => t());
12
- }), a.push(s);
13
- }
14
- function r(e) {
15
- n([], () => e);
16
- }
17
- //#endregion
18
- export { r as useDisconnectEffect, n as useEffect };
19
-
20
- //# sourceMappingURL=effect.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"effect.js","names":[],"sources":["../../src/hooks/effect.ts"],"sourcesContent":["import { createCallbacks } from '../internal/callbacks.ts';\nimport { useContext } from './context.ts';\nimport type { ReadonlyRef, RefValues } from './ref.ts';\n\n/** React to observable (reference) changes. */\nexport function useEffect<const TDeps extends readonly ReadonlyRef<any>[]>(\n deps: TDeps,\n callback: (...values: RefValues<TDeps>) => (() => void) | void,\n): void {\n const { onNotify, onDisconnect } = useContext();\n const cleanupCallback = createCallbacks();\n const cleanup = (): void => cleanupCallback.runAndClear();\n let values: any[] | undefined;\n\n onNotify.push((): void => {\n const newValues = deps.map((dep) => dep.value);\n if (values?.length === newValues.length && values?.every((value, i) => value === newValues[i])) return;\n values = newValues;\n cleanup();\n const maybeCleanup = callback(...(values as any));\n if (maybeCleanup) cleanupCallback.push(() => maybeCleanup());\n });\n\n onDisconnect.push(cleanup);\n}\n\n/**\n * React to document disconnection.\n *\n * Alias for: `useEffect([], () => callback)`\n */\nexport function useDisconnectEffect(callback: () => void): void {\n useEffect([], () => callback);\n}\n"],"mappings":";;;AAKA,SAAgB,EACd,GACA,GACM;CACN,IAAM,EAAE,aAAU,oBAAiB,GAAY,EACzC,IAAkB,GAAiB,EACnC,UAAsB,EAAgB,aAAa,EACrD;AAWJ,CATA,EAAS,WAAiB;EACxB,IAAM,IAAY,EAAK,KAAK,MAAQ,EAAI,MAAM;AAC9C,MAAI,GAAQ,WAAW,EAAU,UAAU,GAAQ,OAAO,GAAO,MAAM,MAAU,EAAU,GAAG,CAAE;AAEhG,EADA,IAAS,GACT,GAAS;EACT,IAAM,IAAe,EAAS,GAAI,EAAe;AACjD,EAAI,KAAc,EAAgB,WAAW,GAAc,CAAC;GAC5D,EAEF,EAAa,KAAK,EAAQ;;AAQ5B,SAAgB,EAAoB,GAA4B;AAC9D,GAAU,EAAE,QAAQ,EAAS"}
@@ -1,9 +0,0 @@
1
- import { useContext as e } from "./context.js";
2
- //#region src/hooks/host.ts
3
- function t() {
4
- return e().host;
5
- }
6
- //#endregion
7
- export { t as useHost };
8
-
9
- //# sourceMappingURL=host.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"host.js","names":[],"sources":["../../src/hooks/host.ts"],"sourcesContent":["import { useContext } from './context.ts';\n\n/** Get the component host element. */\nexport function useHost(): HTMLElement {\n return useContext().host;\n}\n"],"mappings":";;AAGA,SAAgB,IAAuB;AACrC,QAAO,GAAY,CAAC"}
@@ -1,11 +0,0 @@
1
- import { type ReadonlyRef, type Ref, type RefValues } from './ref.ts';
2
- export interface LoadingValue<TValue> {
3
- readonly value: TValue | undefined;
4
- readonly error: unknown;
5
- readonly isLoading: boolean;
6
- }
7
- export interface LoadingOptions {
8
- readonly debounceMs?: number;
9
- }
10
- /** Use a reference (reactive state) bound to an async loader function. */
11
- export declare function useLoading<const TDeps extends readonly ReadonlyRef<any>[], TValue>(deps: TDeps, callback: (signal: AbortSignal, ...values: RefValues<TDeps>) => Promise<TValue>, { debounceMs }?: LoadingOptions): Ref<LoadingValue<TValue>>;
@@ -1 +0,0 @@
1
- {"version":3,"file":"loading.js","names":[],"sources":["../../src/hooks/loading.ts"],"sourcesContent":["import { useEffect } from './effect.ts';\nimport { type ReadonlyRef, type Ref, type RefValues, useRef } from './ref.ts';\n\nexport interface LoadingValue<TValue> {\n readonly value: TValue | undefined;\n readonly error: unknown;\n readonly isLoading: boolean;\n}\n\nexport interface LoadingOptions {\n readonly debounceMs?: number;\n}\n\n/** Use a reference (reactive state) bound to an async loader function. */\nexport function useLoading<const TDeps extends readonly ReadonlyRef<any>[], TValue>(\n deps: TDeps,\n callback: (signal: AbortSignal, ...values: RefValues<TDeps>) => Promise<TValue>,\n { debounceMs }: LoadingOptions = {},\n): Ref<LoadingValue<TValue>> {\n const ref = useRef<LoadingValue<TValue>>({ value: undefined, error: undefined, isLoading: true });\n let skipDebounce = true;\n\n useEffect(deps, () => (...values) => {\n const ac = new AbortController();\n\n Promise.race(\n skipDebounce\n ? [Promise.resolve()]\n : [\n new Promise((resolve) => setTimeout(resolve, debounceMs)),\n new Promise((resolve) => ac.signal.addEventListener('abort', resolve, { once: true })),\n ],\n )\n .then(() => {\n if (ac.signal.aborted) return;\n return callback(ac.signal, ...(values as any));\n })\n .then((value) => {\n if (ac.signal.aborted) return;\n ref.value = { isLoading: false, value, error: undefined };\n })\n .catch((error: unknown) => {\n if (ac.signal.aborted) return;\n ref.value = { isLoading: false, value: undefined, error };\n });\n\n skipDebounce = false;\n return () => ac.abort();\n });\n\n // Skip the debounce again if the component unmounts.\n useEffect([], () => () => (skipDebounce = true));\n\n return ref;\n}\n"],"mappings":";;;AAcA,SAAgB,EACd,GACA,GACA,EAAE,kBAA+B,EAAE,EACR;CAC3B,IAAM,IAAM,EAA6B;EAAE,OAAO,KAAA;EAAW,OAAO,KAAA;EAAW,WAAW;EAAM,CAAC,EAC7F,IAAe;AAiCnB,QA/BA,EAAU,UAAa,GAAG,MAAW;EACnC,IAAM,IAAK,IAAI,iBAAiB;AAwBhC,SAtBA,QAAQ,KACN,IACI,CAAC,QAAQ,SAAS,CAAC,GACnB,CACE,IAAI,SAAS,MAAY,WAAW,GAAS,EAAW,CAAC,EACzD,IAAI,SAAS,MAAY,EAAG,OAAO,iBAAiB,SAAS,GAAS,EAAE,MAAM,IAAM,CAAC,CAAC,CACvF,CACN,CACE,WAAW;AACN,UAAG,OAAO,QACd,QAAO,EAAS,EAAG,QAAQ,GAAI,EAAe;IAC9C,CACD,MAAM,MAAU;AACX,KAAG,OAAO,YACd,EAAI,QAAQ;IAAE,WAAW;IAAO;IAAO,OAAO,KAAA;IAAW;IACzD,CACD,OAAO,MAAmB;AACrB,KAAG,OAAO,YACd,EAAI,QAAQ;IAAE,WAAW;IAAO,OAAO,KAAA;IAAW;IAAO;IACzD,EAEJ,IAAe,UACF,EAAG,OAAO;GACvB,EAGF,EAAU,EAAE,cAAe,IAAe,GAAM,EAEzC"}
package/dist/hooks/ref.js DELETED
@@ -1,19 +0,0 @@
1
- import { useContext as e } from "./context.js";
2
- //#region src/hooks/ref.ts
3
- var t = Symbol();
4
- function n(n, r) {
5
- let { notify: i } = e(), a = n;
6
- return {
7
- [t]: !0,
8
- get value() {
9
- return a;
10
- },
11
- set value(e) {
12
- e !== a && (a = e, r?.(a), i());
13
- }
14
- };
15
- }
16
- //#endregion
17
- export { n as useRef };
18
-
19
- //# sourceMappingURL=ref.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ref.js","names":[],"sources":["../../src/hooks/ref.ts"],"sourcesContent":["import { useContext } from './context.ts';\n\nexport interface Ref<T> extends ReadonlyRef<T> {\n value: T;\n}\n\nexport interface ReadonlyRef<T> {\n /** @hidden */\n [$$ref]: unknown;\n readonly value: T;\n}\n\nexport type RefValues<T> = T extends readonly any[]\n ? { [K in keyof T]: T[K] extends ReadonlyRef<infer V> ? V : never }\n : never;\n\nconst $$ref = Symbol();\n\n/** Use a reference (reactive state) value. */\nexport function useRef<T>(initialValue: T, onChange?: (value: T) => void): Ref<T> {\n const { notify } = useContext();\n let value = initialValue;\n\n return {\n [$$ref]: true,\n get value() {\n return value;\n },\n set value(newValue) {\n if (newValue === value) return;\n value = newValue;\n onChange?.(value);\n notify();\n },\n };\n}\n"],"mappings":";;AAgBA,IAAM,IAAQ,QAAQ;AAGtB,SAAgB,EAAU,GAAiB,GAAuC;CAChF,IAAM,EAAE,cAAW,GAAY,EAC3B,IAAQ;AAEZ,QAAO;GACJ,IAAQ;EACT,IAAI,QAAQ;AACV,UAAO;;EAET,IAAI,MAAM,GAAU;AACd,SAAa,MACjB,IAAQ,GACR,IAAW,EAAM,EACjB,GAAQ;;EAEX"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"route.js","names":[],"sources":["../../src/hooks/route.ts"],"sourcesContent":["import { getRouter } from '../router.ts';\nimport { useEffect } from './effect.ts';\nimport { type Ref, useRef } from './ref.ts';\nimport { useStore } from './store.ts';\n\nexport interface RouteOptions {\n readonly match?: 'prefix' | 'exact' | RegExp;\n readonly source?: 'pathname' | 'hash';\n}\n\nexport type RouteMatchArray = readonly [string, ...string[]] & { readonly groups: Record<string, string> };\n\n/** Use a reference (reactive state) bound to route matching. */\nexport function useRoute(\n path: string | readonly string[],\n { match = 'prefix', source = 'pathname' }: RouteOptions = {},\n): Ref<RouteMatchArray | null> {\n const matchRx = match === 'exact' ? /^$/u : match === 'prefix' ? /^.*$/u : match;\n const paths = Array.isArray(path) ? (path as readonly string[]) : [path as string];\n const refRoute = useStore(getRouter(), (state) => state[source]);\n const refMatch = useRef<RouteMatchArray | null>(getMatch(refRoute.value));\n\n useEffect([refRoute], (route) => {\n refMatch.value = getMatch(route);\n });\n\n return refMatch;\n\n function getMatch(route: string): RouteMatchArray | null {\n const prefix = paths.find((path) => route.endsWith(path)) ?? null;\n if (prefix == null) return null;\n route = route.slice(prefix.length);\n return route.match(matchRx) as RouteMatchArray | null;\n }\n}\n"],"mappings":";;;;;AAaA,SAAgB,EACd,GACA,EAAE,WAAQ,UAAU,YAAS,eAA6B,EAAE,EAC/B;CAC7B,IAAM,IAAU,MAAU,UAAU,QAAQ,MAAU,WAAW,UAAU,GACrE,IAAQ,MAAM,QAAQ,EAAK,GAAI,IAA6B,CAAC,EAAe,EAC5E,IAAW,EAAS,GAAW,GAAG,MAAU,EAAM,GAAQ,EAC1D,IAAW,EAA+B,EAAS,EAAS,MAAM,CAAC;AAMzE,QAJA,EAAU,CAAC,EAAS,GAAG,MAAU;AAC/B,IAAS,QAAQ,EAAS,EAAM;GAChC,EAEK;CAEP,SAAS,EAAS,GAAuC;EACvD,IAAM,IAAS,EAAM,MAAM,MAAS,EAAM,SAAS,EAAK,CAAC,IAAI;AAG7D,SAFI,KAAU,OAAa,QAC3B,IAAQ,EAAM,MAAM,EAAO,OAAO,EAC3B,EAAM,MAAM,EAAQ"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"store.js","names":[],"sources":["../../src/hooks/store.ts"],"sourcesContent":["import type { Store } from '../store.ts';\nimport { useEffect } from './effect.ts';\nimport { type ReadonlyRef, type Ref, useRef } from './ref.ts';\n\n/** Use a reference (reactive state) bound to a (shared) store. */\nexport function useStore<TState, TValue = TState>(\n store: Store<TState>,\n select: (state: TState) => TValue,\n mutate: (store: Store<TState>, value: TValue) => void,\n): Ref<TValue>;\nexport function useStore<TState, TValue = TState>(\n store: Store<TState>,\n select?: (state: TState) => TValue,\n): ReadonlyRef<TValue>;\nexport function useStore<TState, TValue = TState>(\n store: Store<TState>,\n select: (state: TState) => TValue = (state) => state as unknown as TValue,\n mutate?: (store: Store<TState>, value: TValue) => void,\n): Ref<TValue> {\n const ref = useRef(select(store.state), mutate && ((value) => mutate(store, value)));\n useEffect([], () => store.subscribe((state) => (ref.value = select(state))));\n return ref;\n}\n"],"mappings":";;;AAcA,SAAgB,EACd,GACA,KAAqC,MAAU,GAC/C,GACa;CACb,IAAM,IAAM,EAAO,EAAO,EAAM,MAAM,EAAE,OAAY,MAAU,EAAO,GAAO,EAAM,EAAE;AAEpF,QADA,EAAU,EAAE,QAAQ,EAAM,WAAW,MAAW,EAAI,QAAQ,EAAO,EAAM,CAAE,CAAC,EACrE"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"callbacks.js","names":[],"sources":["../../src/internal/callbacks.ts"],"sourcesContent":["export interface Callbacks<TArgs extends unknown[] = []> {\n readonly push: (callback: (...args: TArgs) => void) => () => void;\n readonly run: (...args: TArgs) => void;\n readonly runAndClear: (...args: TArgs) => void;\n readonly clear: () => void;\n}\n\nexport function createCallbacks<TArgs extends unknown[] = []>(): Callbacks<TArgs> {\n const callbacks = new Set<(...args: TArgs) => void>();\n\n const self: Callbacks<TArgs> = {\n push: (callback) => {\n callbacks.add(callback);\n return () => callbacks.delete(callback);\n },\n run: (...args) => {\n const errors: unknown[] = [];\n const callbacksCopy = [...callbacks];\n\n for (const callback of callbacksCopy) {\n try {\n callback(...args);\n } catch (error: unknown) {\n errors.push(error);\n }\n }\n\n if (errors.length > 0) throw new AggregateError(errors);\n },\n runAndClear: (...args: TArgs) => {\n try {\n self.run(...args);\n } finally {\n self.clear();\n }\n },\n clear: () => {\n callbacks.clear();\n },\n };\n\n return self;\n}\n"],"mappings":";AAOA,SAAgB,IAAkE;CAChF,IAAM,oBAAY,IAAI,KAA+B,EAE/C,IAAyB;EAC7B,OAAO,OACL,EAAU,IAAI,EAAS,QACV,EAAU,OAAO,EAAS;EAEzC,MAAM,GAAG,MAAS;GAChB,IAAM,IAAoB,EAAE,EACtB,IAAgB,CAAC,GAAG,EAAU;AAEpC,QAAK,IAAM,KAAY,EACrB,KAAI;AACF,MAAS,GAAG,EAAK;YACV,GAAgB;AACvB,MAAO,KAAK,EAAM;;AAItB,OAAI,EAAO,SAAS,EAAG,OAAU,eAAe,EAAO;;EAEzD,cAAc,GAAG,MAAgB;AAC/B,OAAI;AACF,MAAK,IAAI,GAAG,EAAK;aACT;AACR,MAAK,OAAO;;;EAGhB,aAAa;AACX,KAAU,OAAO;;EAEpB;AAED,QAAO"}
@@ -1,13 +0,0 @@
1
- import { type Callbacks } from './callbacks.ts';
2
- export interface Context {
3
- readonly host: HTMLElement;
4
- readonly onNotify: Callbacks;
5
- readonly onDisconnect: Callbacks;
6
- readonly notify: () => void;
7
- }
8
- export interface ContextController {
9
- readonly connect: (callback: () => void) => void;
10
- readonly disconnect: () => void;
11
- }
12
- export declare const contextStack: Context[];
13
- export declare function createContextController(host: HTMLElement): ContextController;
@@ -1,32 +0,0 @@
1
- import { createCallbacks as e } from "./callbacks.js";
2
- //#region src/internal/context.ts
3
- var t = [];
4
- function n(n) {
5
- let r = !1, i = {
6
- host: n,
7
- onNotify: e(),
8
- onDisconnect: e(),
9
- notify: () => {
10
- r || (r = !0, queueMicrotask(() => {
11
- r = !1, i.onNotify.run();
12
- }));
13
- }
14
- };
15
- return {
16
- connect: (e) => {
17
- try {
18
- t.push(i), e();
19
- } finally {
20
- t.pop();
21
- }
22
- i.onNotify.run();
23
- },
24
- disconnect: () => {
25
- i.onNotify.clear(), i.onDisconnect.runAndClear();
26
- }
27
- };
28
- }
29
- //#endregion
30
- export { t as contextStack, n as createContextController };
31
-
32
- //# sourceMappingURL=context.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"context.js","names":[],"sources":["../../src/internal/context.ts"],"sourcesContent":["import { type Callbacks, createCallbacks } from './callbacks.ts';\n\nexport interface Context {\n readonly host: HTMLElement;\n readonly onNotify: Callbacks;\n readonly onDisconnect: Callbacks;\n readonly notify: () => void;\n}\n\nexport interface ContextController {\n readonly connect: (callback: () => void) => void;\n readonly disconnect: () => void;\n}\n\nexport const contextStack: Context[] = [];\n\nexport function createContextController(host: HTMLElement): ContextController {\n let notifying = false;\n\n const context: Context = {\n host,\n onNotify: createCallbacks(),\n onDisconnect: createCallbacks(),\n notify: () => {\n if (notifying) return;\n notifying = true;\n\n queueMicrotask(() => {\n notifying = false;\n context.onNotify.run();\n });\n },\n };\n\n return {\n connect: (callback) => {\n try {\n contextStack.push(context);\n callback();\n } finally {\n contextStack.pop();\n }\n\n context.onNotify.run();\n },\n disconnect: () => {\n context.onNotify.clear();\n context.onDisconnect.runAndClear();\n },\n };\n}\n"],"mappings":";;AAcA,IAAa,IAA0B,EAAE;AAEzC,SAAgB,EAAwB,GAAsC;CAC5E,IAAI,IAAY,IAEV,IAAmB;EACvB;EACA,UAAU,GAAiB;EAC3B,cAAc,GAAiB;EAC/B,cAAc;AACR,SACJ,IAAY,IAEZ,qBAAqB;AAEnB,IADA,IAAY,IACZ,EAAQ,SAAS,KAAK;KACtB;;EAEL;AAED,QAAO;EACL,UAAU,MAAa;AACrB,OAAI;AAEF,IADA,EAAa,KAAK,EAAQ,EAC1B,GAAU;aACF;AACR,MAAa,KAAK;;AAGpB,KAAQ,SAAS,KAAK;;EAExB,kBAAkB;AAEhB,GADA,EAAQ,SAAS,OAAO,EACxB,EAAQ,aAAa,aAAa;;EAErC"}
package/dist/router.js DELETED
@@ -1,35 +0,0 @@
1
- import { createStore as e } from "./store.js";
2
- //#region src/router.ts
3
- function t() {
4
- return n ??= r();
5
- }
6
- var n;
7
- function r() {
8
- let t = e({
9
- pathname: window.location.pathname,
10
- hash: window.location.hash
11
- }), n = () => {
12
- let e = t.state;
13
- t.state.pathname !== window.location.pathname && (e = {
14
- ...e,
15
- pathname: window.location.pathname
16
- }), t.state.hash !== window.location.hash && (e = {
17
- ...e,
18
- hash: window.location.hash
19
- }), t.state = e;
20
- };
21
- return Object.defineProperties(history, Object.fromEntries(["pushState", "replaceState"].map((e) => {
22
- let t = history[e].bind(history);
23
- return [e, {
24
- value: (...e) => {
25
- t(...e), n();
26
- },
27
- enumerable: !0,
28
- configurable: !0
29
- }];
30
- }))), window.addEventListener("popstate", n), t;
31
- }
32
- //#endregion
33
- export { t as getRouter };
34
-
35
- //# sourceMappingURL=router.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"router.js","names":[],"sources":["../src/router.ts"],"sourcesContent":["import { createStore, type Store } from './store.ts';\n\nexport interface RouterState {\n readonly pathname: string;\n readonly hash: string;\n}\n\n/**\n * Get a router {@link Store} that is updated when the client side route (aka:\n * `History`) changes. This method returns the same store every time it is\n * called (ie. a singleton).\n */\nexport function getRouter(): Store<RouterState> {\n return (singleton ??= createRouter());\n}\n\nlet singleton: Store<RouterState> | undefined;\n\nfunction createRouter(): Store<RouterState> {\n const store = createStore<RouterState>({\n pathname: window.location.pathname,\n hash: window.location.hash,\n });\n\n const onUpdate = (): void => {\n let state = store.state;\n\n if (store.state.pathname !== window.location.pathname) {\n state = { ...state, pathname: window.location.pathname };\n }\n\n if (store.state.hash !== window.location.hash) {\n state = { ...state, hash: window.location.hash };\n }\n\n store.state = state;\n };\n\n Object.defineProperties(\n history,\n Object.fromEntries(\n (['pushState', 'replaceState'] as const).map((method: 'pushState' | 'replaceState') => {\n const original = history[method].bind(history) as History['pushState'] & History['replaceState'];\n const descriptor: PropertyDescriptor = {\n value: (...args: Parameters<typeof original>) => {\n original(...args);\n onUpdate();\n },\n enumerable: true,\n configurable: true,\n };\n\n return [method, descriptor];\n }),\n ),\n );\n\n window.addEventListener('popstate', onUpdate);\n\n return store;\n}\n"],"mappings":";;AAYA,SAAgB,IAAgC;AAC9C,QAAQ,MAAc,GAAc;;AAGtC,IAAI;AAEJ,SAAS,IAAmC;CAC1C,IAAM,IAAQ,EAAyB;EACrC,UAAU,OAAO,SAAS;EAC1B,MAAM,OAAO,SAAS;EACvB,CAAC,EAEI,UAAuB;EAC3B,IAAI,IAAQ,EAAM;AAUlB,EARI,EAAM,MAAM,aAAa,OAAO,SAAS,aAC3C,IAAQ;GAAE,GAAG;GAAO,UAAU,OAAO,SAAS;GAAU,GAGtD,EAAM,MAAM,SAAS,OAAO,SAAS,SACvC,IAAQ;GAAE,GAAG;GAAO,MAAM,OAAO,SAAS;GAAM,GAGlD,EAAM,QAAQ;;AAwBhB,QArBA,OAAO,iBACL,SACA,OAAO,YACJ,CAAC,aAAa,eAAe,CAAW,KAAK,MAAyC;EACrF,IAAM,IAAW,QAAQ,GAAQ,KAAK,QAAQ;AAU9C,SAAO,CAAC,GAAQ;GARd,QAAQ,GAAG,MAAsC;AAE/C,IADA,EAAS,GAAG,EAAK,EACjB,GAAU;;GAEZ,YAAY;GACZ,cAAc;GAGA,CAAW;GAC3B,CACH,CACF,EAED,OAAO,iBAAiB,YAAY,EAAS,EAEtC"}
package/dist/store.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"store.js","names":[],"sources":["../src/store.ts"],"sourcesContent":["import { createCallbacks } from './internal/callbacks.ts';\n\nexport interface Store<TState> {\n state: TState;\n subscribe: (callback: (state: TState) => void) => () => void;\n}\n\n/** Create a new store containing an observable state. */\nexport function createStore<TState>(initialState: TState): Store<TState> {\n const callbacks = createCallbacks();\n let state = initialState;\n\n return {\n get state() {\n return state;\n },\n set state(newState) {\n if (newState === state) return;\n state = newState;\n callbacks.run();\n },\n subscribe: (callback) => callbacks.push(() => callback(state)),\n };\n}\n"],"mappings":";;AAQA,SAAgB,EAAoB,GAAqC;CACvE,IAAM,IAAY,GAAiB,EAC/B,IAAQ;AAEZ,QAAO;EACL,IAAI,QAAQ;AACV,UAAO;;EAET,IAAI,MAAM,GAAU;AACd,SAAa,MACjB,IAAQ,GACR,EAAU,KAAK;;EAEjB,YAAY,MAAa,EAAU,WAAW,EAAS,EAAM,CAAC;EAC/D"}
File without changes