@atscript/ui 0.1.162 → 0.1.164

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 (156) hide show
  1. package/dist/client-factory.cjs +27 -0
  2. package/dist/client-factory.d.cts +53 -0
  3. package/dist/client-factory.d.mts +53 -0
  4. package/dist/client-factory.mjs +25 -0
  5. package/dist/form/apply.cjs +38 -0
  6. package/dist/form/apply.d.cts +36 -0
  7. package/dist/form/apply.d.mts +36 -0
  8. package/dist/form/apply.mjs +38 -0
  9. package/dist/form/clone.cjs +30 -0
  10. package/dist/form/clone.d.cts +23 -0
  11. package/dist/form/clone.d.mts +23 -0
  12. package/dist/form/clone.mjs +30 -0
  13. package/dist/form/create-form-def.cjs +440 -0
  14. package/dist/form/create-form-def.d.cts +67 -0
  15. package/dist/form/create-form-def.d.mts +67 -0
  16. package/dist/form/create-form-def.mjs +438 -0
  17. package/dist/form/decimal-format.cjs +324 -0
  18. package/dist/form/decimal-format.d.cts +86 -0
  19. package/dist/form/decimal-format.d.mts +86 -0
  20. package/dist/form/decimal-format.mjs +315 -0
  21. package/dist/form/diff.cjs +458 -0
  22. package/dist/form/diff.d.cts +80 -0
  23. package/dist/form/diff.d.mts +80 -0
  24. package/dist/form/diff.mjs +457 -0
  25. package/dist/form/dirty.cjs +65 -0
  26. package/dist/form/dirty.d.cts +47 -0
  27. package/dist/form/dirty.d.mts +47 -0
  28. package/dist/form/dirty.mjs +64 -0
  29. package/dist/form/error-utils.cjs +97 -0
  30. package/dist/form/error-utils.d.cts +58 -0
  31. package/dist/form/error-utils.d.mts +58 -0
  32. package/dist/form/error-utils.mjs +92 -0
  33. package/dist/form/form-actions.cjs +30 -0
  34. package/dist/form/form-actions.d.cts +17 -0
  35. package/dist/form/form-actions.d.mts +17 -0
  36. package/dist/form/form-actions.mjs +30 -0
  37. package/dist/form/grid.cjs +69 -0
  38. package/dist/form/grid.d.cts +49 -0
  39. package/dist/form/grid.d.mts +49 -0
  40. package/dist/form/grid.mjs +64 -0
  41. package/dist/form/labels.cjs +10 -0
  42. package/dist/form/labels.d.cts +7 -0
  43. package/dist/form/labels.d.mts +7 -0
  44. package/dist/form/labels.mjs +10 -0
  45. package/dist/form/measurement.cjs +20 -0
  46. package/dist/form/measurement.d.cts +29 -0
  47. package/dist/form/measurement.d.mts +29 -0
  48. package/dist/form/measurement.mjs +20 -0
  49. package/dist/form/path-utils.cjs +182 -0
  50. package/dist/form/path-utils.d.cts +47 -0
  51. package/dist/form/path-utils.d.mts +47 -0
  52. package/dist/form/path-utils.mjs +175 -0
  53. package/dist/form/rebase.cjs +89 -0
  54. package/dist/form/rebase.d.cts +61 -0
  55. package/dist/form/rebase.d.mts +61 -0
  56. package/dist/form/rebase.mjs +89 -0
  57. package/dist/form/skipped-nav.cjs +100 -0
  58. package/dist/form/skipped-nav.d.cts +13 -0
  59. package/dist/form/skipped-nav.d.mts +13 -0
  60. package/dist/form/skipped-nav.mjs +98 -0
  61. package/dist/form/types.cjs +40 -0
  62. package/dist/form/types.d.cts +133 -0
  63. package/dist/form/types.d.mts +133 -0
  64. package/dist/form/types.mjs +36 -0
  65. package/dist/form/union-detect.cjs +46 -0
  66. package/dist/form/union-detect.d.cts +22 -0
  67. package/dist/form/union-detect.d.mts +22 -0
  68. package/dist/form/union-detect.mjs +46 -0
  69. package/dist/form/validate.cjs +150 -0
  70. package/dist/form/validate.d.cts +50 -0
  71. package/dist/form/validate.d.mts +50 -0
  72. package/dist/form/validate.mjs +147 -0
  73. package/dist/index.cjs +217 -3360
  74. package/dist/index.d.cts +38 -1693
  75. package/dist/index.d.mts +38 -1693
  76. package/dist/index.mjs +35 -3179
  77. package/dist/nav/model-routes.cjs +59 -0
  78. package/dist/nav/model-routes.d.cts +33 -0
  79. package/dist/nav/model-routes.d.mts +33 -0
  80. package/dist/nav/model-routes.mjs +59 -0
  81. package/dist/plugin/annotations.cjs +577 -0
  82. package/dist/plugin/annotations.mjs +577 -0
  83. package/dist/plugin/primitives.cjs +30 -0
  84. package/dist/plugin/primitives.mjs +30 -0
  85. package/dist/plugin/value-help-validation.cjs +419 -0
  86. package/dist/plugin/value-help-validation.mjs +413 -0
  87. package/dist/plugin.cjs +4 -1001
  88. package/dist/plugin.mjs +2 -999
  89. package/dist/shared/annotation-keys.cjs +189 -0
  90. package/dist/shared/annotation-keys.d.cts +82 -0
  91. package/dist/shared/annotation-keys.d.mts +82 -0
  92. package/dist/shared/annotation-keys.mjs +97 -0
  93. package/dist/shared/field-resolver.cjs +123 -0
  94. package/dist/shared/field-resolver.d.cts +77 -0
  95. package/dist/shared/field-resolver.d.mts +77 -0
  96. package/dist/shared/field-resolver.mjs +110 -0
  97. package/dist/shared/meta-cache.cjs +244 -0
  98. package/dist/shared/meta-cache.d.cts +110 -0
  99. package/dist/shared/meta-cache.d.mts +110 -0
  100. package/dist/shared/meta-cache.mjs +237 -0
  101. package/dist/shared/str.cjs +19 -0
  102. package/dist/shared/str.d.cts +7 -0
  103. package/dist/shared/str.d.mts +7 -0
  104. package/dist/shared/str.mjs +17 -0
  105. package/dist/table/column-resolver.cjs +17 -0
  106. package/dist/table/column-resolver.d.cts +11 -0
  107. package/dist/table/column-resolver.d.mts +11 -0
  108. package/dist/table/column-resolver.mjs +15 -0
  109. package/dist/table/create-table-def.cjs +233 -0
  110. package/dist/table/create-table-def.d.cts +15 -0
  111. package/dist/table/create-table-def.d.mts +15 -0
  112. package/dist/table/create-table-def.mjs +233 -0
  113. package/dist/table/navigate-href.cjs +45 -0
  114. package/dist/table/navigate-href.d.cts +31 -0
  115. package/dist/table/navigate-href.d.mts +31 -0
  116. package/dist/table/navigate-href.mjs +45 -0
  117. package/dist/table/types.d.cts +287 -0
  118. package/dist/table/types.d.mts +287 -0
  119. package/dist/table/value-kind.cjs +20 -0
  120. package/dist/table/value-kind.d.cts +15 -0
  121. package/dist/table/value-kind.d.mts +15 -0
  122. package/dist/table/value-kind.mjs +19 -0
  123. package/dist/value-help/dict-paths.cjs +17 -0
  124. package/dist/value-help/dict-paths.d.cts +11 -0
  125. package/dist/value-help/dict-paths.d.mts +11 -0
  126. package/dist/value-help/dict-paths.mjs +17 -0
  127. package/dist/value-help/distinct-client.cjs +51 -0
  128. package/dist/value-help/distinct-client.d.cts +40 -0
  129. package/dist/value-help/distinct-client.d.mts +40 -0
  130. package/dist/value-help/distinct-client.mjs +50 -0
  131. package/dist/value-help/extract-literals.cjs +76 -0
  132. package/dist/value-help/extract-literals.d.cts +32 -0
  133. package/dist/value-help/extract-literals.d.mts +32 -0
  134. package/dist/value-help/extract-literals.mjs +74 -0
  135. package/dist/value-help/extract-ref.cjs +100 -0
  136. package/dist/value-help/extract-ref.d.cts +37 -0
  137. package/dist/value-help/extract-ref.d.mts +37 -0
  138. package/dist/value-help/extract-ref.mjs +100 -0
  139. package/dist/value-help/filter-tree.cjs +45 -0
  140. package/dist/value-help/filter-tree.mjs +45 -0
  141. package/dist/value-help/index.d.mts +8 -0
  142. package/dist/value-help/resolve-options.cjs +64 -0
  143. package/dist/value-help/resolve-options.d.cts +35 -0
  144. package/dist/value-help/resolve-options.d.mts +35 -0
  145. package/dist/value-help/resolve-options.mjs +60 -0
  146. package/dist/value-help/resolve.cjs +62 -0
  147. package/dist/value-help/resolve.d.cts +31 -0
  148. package/dist/value-help/resolve.d.mts +31 -0
  149. package/dist/value-help/resolve.mjs +61 -0
  150. package/dist/value-help/types.d.cts +36 -0
  151. package/dist/value-help/types.d.mts +36 -0
  152. package/dist/value-help/value-help-client.cjs +136 -0
  153. package/dist/value-help/value-help-client.d.cts +67 -0
  154. package/dist/value-help/value-help-client.d.mts +67 -0
  155. package/dist/value-help/value-help-client.mjs +134 -0
  156. package/package.json +10 -10
@@ -0,0 +1,77 @@
1
+ import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
2
+
3
+ //#region src/shared/field-resolver.d.ts
4
+ /** Options for field and form property resolution. */
5
+ interface TResolveOptions<T> {
6
+ /** When true, any non-undefined static value is returned as `true` (for boolean flags like ui.disabled). */
7
+ staticAsBoolean?: boolean;
8
+ /** Transform the raw static value before returning. */
9
+ transform?: (raw: unknown) => T;
10
+ }
11
+ /**
12
+ * Pluggable resolver interface for field/form metadata.
13
+ * @atscript/ui provides a static implementation (reads only static annotation values).
14
+ * ui-fns extends it with dynamic `new Function` compilation for `ui.fn.*` keys.
15
+ */
16
+ interface FieldResolver {
17
+ /** Resolve a field-level metadata property. */
18
+ resolveFieldProp<T>(prop: TAtscriptAnnotatedType, fnKey: string, staticKey: string | undefined, scope: Record<string, unknown>, opts?: TResolveOptions<T>): T | undefined;
19
+ /** Resolve a form-level metadata property. */
20
+ resolveFormProp<T>(type: TAtscriptAnnotatedType, fnKey: string, staticKey: string | undefined, scope: Record<string, unknown>, opts?: TResolveOptions<T>): T | undefined;
21
+ /** Check if a prop has dynamic annotations (ui.fn.*). */
22
+ hasComputedAnnotations(prop: TAtscriptAnnotatedType): boolean;
23
+ }
24
+ /** Static resolver — ignores fn keys, reads only static metadata. */
25
+ declare class StaticFieldResolver implements FieldResolver {
26
+ resolveFieldProp<T>(prop: TAtscriptAnnotatedType, _fnKey: string, staticKey: string | undefined, _scope: Record<string, unknown>, opts?: TResolveOptions<T>): T | undefined;
27
+ resolveFormProp<T>(type: TAtscriptAnnotatedType, _fnKey: string, staticKey: string | undefined, _scope: Record<string, unknown>, opts?: TResolveOptions<T>): T | undefined;
28
+ hasComputedAnnotations(_prop: TAtscriptAnnotatedType): boolean;
29
+ }
30
+ /** Resolves a static metadata value. Exported for reuse by dynamic resolvers. */
31
+ declare function resolveStatic<T>(metadata: TAtscriptAnnotatedType["metadata"], staticKey: string | undefined, opts?: TResolveOptions<T>): T | undefined;
32
+ /** Default static resolver instance. */
33
+ declare const defaultResolver: StaticFieldResolver;
34
+ /** Replace the active resolver (called by ui-fns to install dynamic resolution). */
35
+ declare function setResolver(resolver: FieldResolver): void;
36
+ /** Get the current active resolver. */
37
+ declare function getResolver(): FieldResolver;
38
+ /** Resolve a field-level metadata property via the active resolver. */
39
+ declare function resolveFieldProp<T>(prop: TAtscriptAnnotatedType, fnKey: string, staticKey: string | undefined, scope: Record<string, unknown>, opts?: TResolveOptions<T>): T | undefined;
40
+ /** Resolve a form-level metadata property via the active resolver. */
41
+ declare function resolveFormProp<T>(type: TAtscriptAnnotatedType, fnKey: string, staticKey: string | undefined, scope: Record<string, unknown>, opts?: TResolveOptions<T>): T | undefined;
42
+ /** Check if a prop has dynamic annotations via the active resolver. */
43
+ declare function hasComputedAnnotations(prop: TAtscriptAnnotatedType): boolean;
44
+ /**
45
+ * SSOT for field hidden resolution — resolves `@ui.form.fn.hidden`
46
+ * (dynamic, via the active resolver) with static `@ui.form.hidden`
47
+ * presence as the fallback. Absent both → `false` (visible).
48
+ */
49
+ declare function isFieldHidden(prop: TAtscriptAnnotatedType, scope: Record<string, unknown>): boolean;
50
+ /**
51
+ * Reads a static metadata value from an ATScript prop.
52
+ * Typed overload for known AtscriptMetadata keys; falls back to `unknown` for other keys.
53
+ */
54
+ declare function getFieldMeta<K extends keyof AtscriptMetadata>(prop: TAtscriptAnnotatedType, key: K): AtscriptMetadata[K] | undefined;
55
+ declare function getFieldMeta(prop: TAtscriptAnnotatedType, key: string): unknown;
56
+ /** Checks whether a metadata key is present on an ATScript prop. */
57
+ declare function hasFieldMeta(prop: TAtscriptAnnotatedType, key: string): boolean;
58
+ /** Ensures a value is an array — returns as-is if already one, wraps in `[x]` otherwise. */
59
+ declare function asArray<T>(x: T | T[]): T[];
60
+ /**
61
+ * Parses static `ui.attr` metadata into a key-value record.
62
+ * Exported so ui-fns can reuse this without duplicating the parsing logic.
63
+ */
64
+ declare function parseStaticAttrs(staticAttrs: unknown): Record<string, unknown> | undefined;
65
+ /** Per-surface attr key pair — defaults to the form-side keys. */
66
+ interface TResolveAttrsKeys {
67
+ staticKey?: string;
68
+ fnKey?: string;
69
+ }
70
+ /**
71
+ * Resolves `<staticKey>` + `<fnKey>` attr metadata on demand.
72
+ * Defaults read `ui.form.attr` + `ui.form.fn.attr`; pass `{ staticKey, fnKey }` to read
73
+ * the table-side pair (`ui.table.attr` + `ui.table.fn.attr`) or any other surface.
74
+ */
75
+ declare function resolveAttrs(prop: TAtscriptAnnotatedType, scope: Record<string, unknown>, keys?: TResolveAttrsKeys): Record<string, unknown> | undefined;
76
+ //#endregion
77
+ export { FieldResolver, StaticFieldResolver, TResolveOptions, asArray, defaultResolver, getFieldMeta, getResolver, hasComputedAnnotations, hasFieldMeta, isFieldHidden, parseStaticAttrs, resolveAttrs, resolveFieldProp, resolveFormProp, resolveStatic, setResolver };
@@ -0,0 +1,110 @@
1
+ import "./annotation-keys.mjs";
2
+ //#region src/shared/field-resolver.ts
3
+ /** Static resolver — ignores fn keys, reads only static metadata. */
4
+ var StaticFieldResolver = class {
5
+ resolveFieldProp(prop, _fnKey, staticKey, _scope, opts) {
6
+ return resolveStatic(prop.metadata, staticKey, opts);
7
+ }
8
+ resolveFormProp(type, _fnKey, staticKey, _scope, opts) {
9
+ return resolveStatic(type.metadata, staticKey, opts);
10
+ }
11
+ hasComputedAnnotations(_prop) {
12
+ return false;
13
+ }
14
+ };
15
+ /** Resolves a static metadata value. Exported for reuse by dynamic resolvers. */
16
+ function resolveStatic(metadata, staticKey, opts) {
17
+ if (staticKey === void 0) return void 0;
18
+ const staticVal = metadata.get(staticKey);
19
+ if (staticVal !== void 0) {
20
+ if (opts?.staticAsBoolean) return true;
21
+ if (opts?.transform) return opts.transform(staticVal);
22
+ return staticVal;
23
+ }
24
+ }
25
+ /** Default static resolver instance. */
26
+ const defaultResolver = new StaticFieldResolver();
27
+ let activeResolver = defaultResolver;
28
+ /** Replace the active resolver (called by ui-fns to install dynamic resolution). */
29
+ function setResolver(resolver) {
30
+ activeResolver = resolver;
31
+ }
32
+ /** Get the current active resolver. */
33
+ function getResolver() {
34
+ return activeResolver;
35
+ }
36
+ /** Resolve a field-level metadata property via the active resolver. */
37
+ function resolveFieldProp(prop, fnKey, staticKey, scope, opts) {
38
+ return activeResolver.resolveFieldProp(prop, fnKey, staticKey, scope, opts);
39
+ }
40
+ /** Resolve a form-level metadata property via the active resolver. */
41
+ function resolveFormProp(type, fnKey, staticKey, scope, opts) {
42
+ return activeResolver.resolveFormProp(type, fnKey, staticKey, scope, opts);
43
+ }
44
+ /** Check if a prop has dynamic annotations via the active resolver. */
45
+ function hasComputedAnnotations(prop) {
46
+ return activeResolver.hasComputedAnnotations(prop);
47
+ }
48
+ /**
49
+ * SSOT for field hidden resolution — resolves `@ui.form.fn.hidden`
50
+ * (dynamic, via the active resolver) with static `@ui.form.hidden`
51
+ * presence as the fallback. Absent both → `false` (visible).
52
+ */
53
+ function isFieldHidden(prop, scope) {
54
+ return resolveFieldProp(prop, "ui.form.fn.hidden", "ui.form.hidden", scope, { staticAsBoolean: true }) ?? false;
55
+ }
56
+ function getFieldMeta(prop, key) {
57
+ return prop.metadata.get(key);
58
+ }
59
+ /** Checks whether a metadata key is present on an ATScript prop. */
60
+ function hasFieldMeta(prop, key) {
61
+ return prop.metadata.has(key);
62
+ }
63
+ /** Ensures a value is an array — returns as-is if already one, wraps in `[x]` otherwise. */
64
+ function asArray(x) {
65
+ return Array.isArray(x) ? x : [x];
66
+ }
67
+ /**
68
+ * Parses static `ui.attr` metadata into a key-value record.
69
+ * Exported so ui-fns can reuse this without duplicating the parsing logic.
70
+ */
71
+ function parseStaticAttrs(staticAttrs) {
72
+ if (!staticAttrs) return void 0;
73
+ const result = {};
74
+ let hasAttrs = false;
75
+ for (const item of asArray(staticAttrs)) if (typeof item === "object" && item !== null && "name" in item && "value" in item) {
76
+ const { name, value } = item;
77
+ result[name] = value;
78
+ hasAttrs = true;
79
+ }
80
+ return hasAttrs ? result : void 0;
81
+ }
82
+ /**
83
+ * Resolves `<staticKey>` + `<fnKey>` attr metadata on demand.
84
+ * Defaults read `ui.form.attr` + `ui.form.fn.attr`; pass `{ staticKey, fnKey }` to read
85
+ * the table-side pair (`ui.table.attr` + `ui.table.fn.attr`) or any other surface.
86
+ */
87
+ function resolveAttrs(prop, scope, keys = {}) {
88
+ const staticKey = keys.staticKey ?? "ui.form.attr";
89
+ const fnKey = keys.fnKey ?? "ui.form.fn.attr";
90
+ const staticAttrs = getFieldMeta(prop, staticKey);
91
+ const fnAttrs = getFieldMeta(prop, fnKey);
92
+ if (!staticAttrs && !fnAttrs) return void 0;
93
+ const result = {};
94
+ let hasAttrs = false;
95
+ const parsed = parseStaticAttrs(staticAttrs);
96
+ if (parsed) {
97
+ Object.assign(result, parsed);
98
+ hasAttrs = true;
99
+ }
100
+ if (fnAttrs) {
101
+ const resolved = resolveFieldProp(prop, fnKey, void 0, scope);
102
+ if (resolved) {
103
+ Object.assign(result, resolved);
104
+ hasAttrs = true;
105
+ }
106
+ }
107
+ return hasAttrs ? result : void 0;
108
+ }
109
+ //#endregion
110
+ export { StaticFieldResolver, asArray, defaultResolver, getFieldMeta, getResolver, hasComputedAnnotations, hasFieldMeta, isFieldHidden, parseStaticAttrs, resolveAttrs, resolveFieldProp, resolveFormProp, resolveStatic, setResolver };
@@ -0,0 +1,244 @@
1
+ const require_value_help_client = require("../value-help/value-help-client.cjs");
2
+ const require_client_factory = require("../client-factory.cjs");
3
+ const require_create_table_def = require("../table/create-table-def.cjs");
4
+ let _atscript_typescript_utils = require("@atscript/typescript/utils");
5
+ let _atscript_db_client = require("@atscript/db-client");
6
+ //#region src/shared/meta-cache.ts
7
+ /**
8
+ * Enough for every URL a page touches — a table, the dictionaries its
9
+ * filters and forms pick from — plus the recent navigation history, so going
10
+ * back is instant; small enough to cap a long session at a few MB of parsed
11
+ * `/meta`. Twice the `@atscript/db-client` `MetaStore` body limit, since
12
+ * parametric URLs share bodies.
13
+ */
14
+ const DEFAULT_MAX_ENTRIES = 100;
15
+ let maxEntries = DEFAULT_MAX_ENTRIES;
16
+ /** Per-URL entries, least recently used first. Browser only. */
17
+ const cache = /* @__PURE__ */ new Map();
18
+ const slots = /* @__PURE__ */ new WeakMap();
19
+ /** Derived shapes per `ETag`, counted by the cached entries that use them. Browser only. */
20
+ const derivedByEtag = /* @__PURE__ */ new Map();
21
+ /** Custom `/meta` stores of cached clients, counted by the entries that use them. */
22
+ const customStores = /* @__PURE__ */ new Map();
23
+ /** `undefined` until the first `setMetaCacheIdentity` call; `null` = anonymous. */
24
+ let identity;
25
+ const resetListeners = /* @__PURE__ */ new Set();
26
+ /**
27
+ * Get or create the cache entry for `url`. First caller's `factory` (and
28
+ * `options.metaKey`) seeds the `Client`; subsequent callers reuse it. On
29
+ * `meta` rejection, the entry is evicted so the next call retries. A hit
30
+ * marks the URL most recently used; hold an entry you keep using with
31
+ * {@link retainMetaEntry}. Without a browser `window` nothing is cached and
32
+ * the client is asked for `metaStore: false`.
33
+ */
34
+ function getMetaEntry(url, factory, options) {
35
+ const browser = typeof window !== "undefined";
36
+ const existing = browser ? cache.get(url) : void 0;
37
+ if (existing) {
38
+ touch(url, existing);
39
+ return existing;
40
+ }
41
+ const f = factory ?? require_client_factory.getDefaultClientFactory();
42
+ let client;
43
+ if (!browser) client = f(url, { metaStore: false });
44
+ else if (options?.metaKey) client = f(url, { metaKey: options.metaKey });
45
+ else client = f(url);
46
+ const meta = client.meta().catch((err) => {
47
+ if (cache.get(url) === entry) evict(url, entry);
48
+ throw err;
49
+ });
50
+ const derived = meta.then((m) => derive(entry, client, m));
51
+ const type = derived.then((d) => d.type);
52
+ type.catch(() => {});
53
+ const entry = {
54
+ client,
55
+ meta,
56
+ type
57
+ };
58
+ const slot = {
59
+ url,
60
+ derived,
61
+ holds: 0
62
+ };
63
+ slots.set(entry, slot);
64
+ if (browser) {
65
+ slot.store = customStoreOf(client);
66
+ if (slot.store) customStores.set(slot.store, (customStores.get(slot.store) ?? 0) + 1);
67
+ cache.set(url, entry);
68
+ trim(entry);
69
+ }
70
+ return entry;
71
+ }
72
+ /**
73
+ * Keep `entry` in the cache while it is in use: a held entry is never
74
+ * evicted by the size limit ({@link setMetaCacheMaxEntries}), so later
75
+ * `getMetaEntry` / `resolveValueHelp` calls for its URL keep returning it.
76
+ * Call the returned function (idempotent) once done — `useTable` and the
77
+ * form value-help do this for their component's lifetime. A reset still
78
+ * drops held entries. Since 0.1.154.
79
+ */
80
+ function retainMetaEntry(entry) {
81
+ const slot = slots.get(entry);
82
+ if (!slot) return () => {};
83
+ slot.holds++;
84
+ let released = false;
85
+ return () => {
86
+ if (released) return;
87
+ released = true;
88
+ slot.holds--;
89
+ if (slot.holds === 0 && cache.get(slot.url) === entry) {
90
+ touch(slot.url, entry);
91
+ trim();
92
+ }
93
+ };
94
+ }
95
+ /**
96
+ * How many URLs the meta cache keeps (default `100`). Least recently used
97
+ * entries beyond it are dropped — entries held with {@link retainMetaEntry}
98
+ * (every mounted table and form value-help) never are, and may exceed it.
99
+ * At least `1`; `Infinity` turns the limit off; no argument restores the
100
+ * default. Since 0.1.154.
101
+ */
102
+ function setMetaCacheMaxEntries(max) {
103
+ maxEntries = max === void 0 ? DEFAULT_MAX_ENTRIES : max >= 1 ? max : 1;
104
+ trim();
105
+ }
106
+ function touch(url, entry) {
107
+ cache.delete(url);
108
+ cache.set(url, entry);
109
+ }
110
+ /**
111
+ * Drop least recently used entries nothing holds until the cache fits.
112
+ * `spare` — the entry just handed out, about to be retained by its caller.
113
+ */
114
+ function trim(spare) {
115
+ if (cache.size <= maxEntries) return;
116
+ for (const [url, entry] of cache) {
117
+ if (entry !== spare && slots.get(entry).holds === 0) evict(url, entry);
118
+ if (cache.size <= maxEntries) return;
119
+ }
120
+ }
121
+ /**
122
+ * Remove `entry` and release what only it kept: its shared `Derived` and
123
+ * custom store. A reset no longer reaches its client, so the client's memoized
124
+ * `/meta` goes now — a factory that reuses clients per URL would otherwise
125
+ * hand it to the next viewer.
126
+ */
127
+ function evict(url, entry) {
128
+ cache.delete(url);
129
+ entry.client.invalidateMeta?.();
130
+ const slot = slots.get(entry);
131
+ if (slot.etag !== void 0 && --derivedByEtag.get(slot.etag).refs === 0) derivedByEtag.delete(slot.etag);
132
+ if (slot.store) {
133
+ const refs = customStores.get(slot.store);
134
+ if (refs > 1) customStores.set(slot.store, refs - 1);
135
+ else {
136
+ slot.store.clear();
137
+ customStores.delete(slot.store);
138
+ }
139
+ }
140
+ }
141
+ /**
142
+ * The `TableDef` for a cache entry, built once per entry — and once per
143
+ * `/meta` `ETag` across entries. Since 0.1.153.
144
+ */
145
+ function getMetaTableDef(entry) {
146
+ if (!entry.tableDef) {
147
+ const derived = slots.get(entry)?.derived;
148
+ entry.tableDef = derived ? Promise.all([entry.meta, derived]).then(([meta, d]) => d.tableDef ??= require_create_table_def.createTableDef(meta, d.type)) : Promise.all([entry.meta, entry.type]).then(([meta, type]) => require_create_table_def.createTableDef(meta, type));
149
+ }
150
+ return entry.tableDef;
151
+ }
152
+ /**
153
+ * Shares the `Derived` of one `ETag` among the cached entries that loaded it;
154
+ * an entry no longer cached (evicted, reset, server render) keeps its own.
155
+ */
156
+ function derive(entry, client, meta) {
157
+ const slot = slots.get(entry);
158
+ const etag = cache.get(slot.url) === entry ? client.metaEtag?.() : void 0;
159
+ if (etag === void 0) return { type: (0, _atscript_typescript_utils.deserializeAnnotatedType)(meta.type) };
160
+ let shared = derivedByEtag.get(etag);
161
+ if (shared) shared.refs++;
162
+ else {
163
+ shared = {
164
+ derived: { type: (0, _atscript_typescript_utils.deserializeAnnotatedType)(meta.type) },
165
+ refs: 1
166
+ };
167
+ derivedByEtag.set(etag, shared);
168
+ }
169
+ slot.etag = etag;
170
+ return shared.derived;
171
+ }
172
+ /**
173
+ * The custom `/meta` store a factory gave `client`, so a reset (and the
174
+ * eviction of its last cached client) can clear it. The shared
175
+ * `defaultMetaStore` is skipped: a reset clears it through
176
+ * `clearMetaStore()`, and an eviction must not drop the ETags of every
177
+ * other URL. A store-less client (`metaStore: false`) is skipped too.
178
+ */
179
+ function customStoreOf(client) {
180
+ const store = client.metaStore;
181
+ return store && store !== _atscript_db_client.defaultMetaStore ? store : void 0;
182
+ }
183
+ /**
184
+ * Drop every cached `/meta` — including the copy each cached `Client`
185
+ * memoizes and the `@atscript/db-client` meta stores used to revalidate it
186
+ * (the shared default one and any custom one a factory supplied) — the
187
+ * value-help searches shared under them, and everything registered through
188
+ * {@link onMetaCacheReset} (e.g. the table-presets cache). Entries already
189
+ * handed out keep working; the next `getMetaEntry` refetches.
190
+ */
191
+ function resetMetaCache() {
192
+ const entries = [...cache.values()];
193
+ cache.clear();
194
+ derivedByEtag.clear();
195
+ for (const entry of entries) entry.client.invalidateMeta?.();
196
+ (0, _atscript_db_client.clearMetaStore)();
197
+ for (const store of customStores.keys()) store.clear();
198
+ customStores.clear();
199
+ require_value_help_client.invalidateValueHelpCache();
200
+ for (const listener of resetListeners) listener();
201
+ }
202
+ /**
203
+ * Bind the meta cache to the current viewer. Call it whenever the signed-in
204
+ * user or their role may have changed — after login, after logout and on
205
+ * every reload of the current user. A key that differs from the previous
206
+ * one resets the cache (`resetMetaCache`); the same key keeps it. `null` /
207
+ * `undefined` = anonymous.
208
+ *
209
+ * Binding an identity also turns on caches that are only safe when the app
210
+ * reports sign-in changes, such as the table-presets cache.
211
+ */
212
+ function setMetaCacheIdentity(key) {
213
+ const next = key ?? null;
214
+ if (next === identity) return;
215
+ identity = next;
216
+ resetMetaCache();
217
+ }
218
+ /**
219
+ * The identity last passed to {@link setMetaCacheIdentity}: `undefined` when
220
+ * the app never bound one, `null` for anonymous. Since 0.1.153.
221
+ */
222
+ function getMetaCacheIdentity() {
223
+ return identity;
224
+ }
225
+ /**
226
+ * Run `listener` on every {@link resetMetaCache} (and so on every identity
227
+ * change). For caches that belong to the signed-in viewer. Returns an
228
+ * unsubscribe function. Since 0.1.153.
229
+ */
230
+ function onMetaCacheReset(listener) {
231
+ resetListeners.add(listener);
232
+ return () => {
233
+ resetListeners.delete(listener);
234
+ };
235
+ }
236
+ //#endregion
237
+ exports.getMetaCacheIdentity = getMetaCacheIdentity;
238
+ exports.getMetaEntry = getMetaEntry;
239
+ exports.getMetaTableDef = getMetaTableDef;
240
+ exports.onMetaCacheReset = onMetaCacheReset;
241
+ exports.resetMetaCache = resetMetaCache;
242
+ exports.retainMetaEntry = retainMetaEntry;
243
+ exports.setMetaCacheIdentity = setMetaCacheIdentity;
244
+ exports.setMetaCacheMaxEntries = setMetaCacheMaxEntries;
@@ -0,0 +1,110 @@
1
+ import { ResolvedValueHelp } from "../value-help/resolve.cjs";
2
+ import { MetaResponse, TableDef } from "../table/types.cjs";
3
+ import { ClientFactory } from "../client-factory.cjs";
4
+ import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
5
+ import { Client } from "@atscript/db-client";
6
+
7
+ //#region src/shared/meta-cache.d.ts
8
+ /**
9
+ * Shared per-URL cache. A single entry holds the `Client` instance, the raw
10
+ * `/meta` promise, the deserialized type, and lazily-populated derived shapes
11
+ * (`ResolvedValueHelp` for value-help, `TableDef` for tables). Both
12
+ * `resolveValueHelp` and `useTable` route through it so a given URL triggers
13
+ * at most one network round-trip and one `deserializeAnnotatedType`.
14
+ *
15
+ * Byte-identical `/meta` bodies (same `ETag`, e.g. one parametric controller
16
+ * under different route params) also share the deserialized type and the
17
+ * `TableDef` across URLs.
18
+ *
19
+ * The cache keeps the {@link setMetaCacheMaxEntries} most recently used
20
+ * URLs; an entry held with {@link retainMetaEntry} is never evicted.
21
+ *
22
+ * `/meta` is projected per viewer, so the cache belongs to one identity
23
+ * (`setMetaCacheIdentity`) and exists only in the browser: during server
24
+ * rendering one process serves many viewers, so every call builds a fresh,
25
+ * uncached entry whose client keeps no `/meta` store.
26
+ */
27
+ interface MetaCacheEntry {
28
+ client: Client;
29
+ meta: Promise<MetaResponse>;
30
+ type: Promise<TAtscriptAnnotatedType>;
31
+ resolved?: Promise<ResolvedValueHelp>;
32
+ tableDef?: Promise<TableDef>;
33
+ }
34
+ /** Options for {@link getMetaEntry}. */
35
+ interface MetaEntryOptions {
36
+ /**
37
+ * Key under which the `/meta` revalidation store keeps this URL's `ETag`s
38
+ * (default: the `/meta` URL). Give every URL of one parametric mount the
39
+ * route template (e.g. `"/api/db/ticket-issue/:key"`): a new route param
40
+ * then revalidates against the body already loaded for another one and
41
+ * costs a `304` instead of the full `/meta`. Forwarded to the
42
+ * {@link ClientFactory} as `metaKey`; honoured only by the first call per
43
+ * URL, like the factory. Since 0.1.153.
44
+ */
45
+ metaKey?: string;
46
+ }
47
+ /**
48
+ * Get or create the cache entry for `url`. First caller's `factory` (and
49
+ * `options.metaKey`) seeds the `Client`; subsequent callers reuse it. On
50
+ * `meta` rejection, the entry is evicted so the next call retries. A hit
51
+ * marks the URL most recently used; hold an entry you keep using with
52
+ * {@link retainMetaEntry}. Without a browser `window` nothing is cached and
53
+ * the client is asked for `metaStore: false`.
54
+ */
55
+ declare function getMetaEntry(url: string, factory?: ClientFactory, options?: MetaEntryOptions): MetaCacheEntry;
56
+ /**
57
+ * Keep `entry` in the cache while it is in use: a held entry is never
58
+ * evicted by the size limit ({@link setMetaCacheMaxEntries}), so later
59
+ * `getMetaEntry` / `resolveValueHelp` calls for its URL keep returning it.
60
+ * Call the returned function (idempotent) once done — `useTable` and the
61
+ * form value-help do this for their component's lifetime. A reset still
62
+ * drops held entries. Since 0.1.154.
63
+ */
64
+ declare function retainMetaEntry(entry: MetaCacheEntry): () => void;
65
+ /**
66
+ * How many URLs the meta cache keeps (default `100`). Least recently used
67
+ * entries beyond it are dropped — entries held with {@link retainMetaEntry}
68
+ * (every mounted table and form value-help) never are, and may exceed it.
69
+ * At least `1`; `Infinity` turns the limit off; no argument restores the
70
+ * default. Since 0.1.154.
71
+ */
72
+ declare function setMetaCacheMaxEntries(max?: number): void;
73
+ /**
74
+ * The `TableDef` for a cache entry, built once per entry — and once per
75
+ * `/meta` `ETag` across entries. Since 0.1.153.
76
+ */
77
+ declare function getMetaTableDef(entry: MetaCacheEntry): Promise<TableDef>;
78
+ /**
79
+ * Drop every cached `/meta` — including the copy each cached `Client`
80
+ * memoizes and the `@atscript/db-client` meta stores used to revalidate it
81
+ * (the shared default one and any custom one a factory supplied) — the
82
+ * value-help searches shared under them, and everything registered through
83
+ * {@link onMetaCacheReset} (e.g. the table-presets cache). Entries already
84
+ * handed out keep working; the next `getMetaEntry` refetches.
85
+ */
86
+ declare function resetMetaCache(): void;
87
+ /**
88
+ * Bind the meta cache to the current viewer. Call it whenever the signed-in
89
+ * user or their role may have changed — after login, after logout and on
90
+ * every reload of the current user. A key that differs from the previous
91
+ * one resets the cache (`resetMetaCache`); the same key keeps it. `null` /
92
+ * `undefined` = anonymous.
93
+ *
94
+ * Binding an identity also turns on caches that are only safe when the app
95
+ * reports sign-in changes, such as the table-presets cache.
96
+ */
97
+ declare function setMetaCacheIdentity(key: string | null | undefined): void;
98
+ /**
99
+ * The identity last passed to {@link setMetaCacheIdentity}: `undefined` when
100
+ * the app never bound one, `null` for anonymous. Since 0.1.153.
101
+ */
102
+ declare function getMetaCacheIdentity(): string | null | undefined;
103
+ /**
104
+ * Run `listener` on every {@link resetMetaCache} (and so on every identity
105
+ * change). For caches that belong to the signed-in viewer. Returns an
106
+ * unsubscribe function. Since 0.1.153.
107
+ */
108
+ declare function onMetaCacheReset(listener: () => void): () => void;
109
+ //#endregion
110
+ export { MetaCacheEntry, MetaEntryOptions, getMetaCacheIdentity, getMetaEntry, getMetaTableDef, onMetaCacheReset, resetMetaCache, retainMetaEntry, setMetaCacheIdentity, setMetaCacheMaxEntries };
@@ -0,0 +1,110 @@
1
+ import { ResolvedValueHelp } from "../value-help/resolve.mjs";
2
+ import { MetaResponse, TableDef } from "../table/types.mjs";
3
+ import { ClientFactory } from "../client-factory.mjs";
4
+ import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
5
+ import { Client } from "@atscript/db-client";
6
+
7
+ //#region src/shared/meta-cache.d.ts
8
+ /**
9
+ * Shared per-URL cache. A single entry holds the `Client` instance, the raw
10
+ * `/meta` promise, the deserialized type, and lazily-populated derived shapes
11
+ * (`ResolvedValueHelp` for value-help, `TableDef` for tables). Both
12
+ * `resolveValueHelp` and `useTable` route through it so a given URL triggers
13
+ * at most one network round-trip and one `deserializeAnnotatedType`.
14
+ *
15
+ * Byte-identical `/meta` bodies (same `ETag`, e.g. one parametric controller
16
+ * under different route params) also share the deserialized type and the
17
+ * `TableDef` across URLs.
18
+ *
19
+ * The cache keeps the {@link setMetaCacheMaxEntries} most recently used
20
+ * URLs; an entry held with {@link retainMetaEntry} is never evicted.
21
+ *
22
+ * `/meta` is projected per viewer, so the cache belongs to one identity
23
+ * (`setMetaCacheIdentity`) and exists only in the browser: during server
24
+ * rendering one process serves many viewers, so every call builds a fresh,
25
+ * uncached entry whose client keeps no `/meta` store.
26
+ */
27
+ interface MetaCacheEntry {
28
+ client: Client;
29
+ meta: Promise<MetaResponse>;
30
+ type: Promise<TAtscriptAnnotatedType>;
31
+ resolved?: Promise<ResolvedValueHelp>;
32
+ tableDef?: Promise<TableDef>;
33
+ }
34
+ /** Options for {@link getMetaEntry}. */
35
+ interface MetaEntryOptions {
36
+ /**
37
+ * Key under which the `/meta` revalidation store keeps this URL's `ETag`s
38
+ * (default: the `/meta` URL). Give every URL of one parametric mount the
39
+ * route template (e.g. `"/api/db/ticket-issue/:key"`): a new route param
40
+ * then revalidates against the body already loaded for another one and
41
+ * costs a `304` instead of the full `/meta`. Forwarded to the
42
+ * {@link ClientFactory} as `metaKey`; honoured only by the first call per
43
+ * URL, like the factory. Since 0.1.153.
44
+ */
45
+ metaKey?: string;
46
+ }
47
+ /**
48
+ * Get or create the cache entry for `url`. First caller's `factory` (and
49
+ * `options.metaKey`) seeds the `Client`; subsequent callers reuse it. On
50
+ * `meta` rejection, the entry is evicted so the next call retries. A hit
51
+ * marks the URL most recently used; hold an entry you keep using with
52
+ * {@link retainMetaEntry}. Without a browser `window` nothing is cached and
53
+ * the client is asked for `metaStore: false`.
54
+ */
55
+ declare function getMetaEntry(url: string, factory?: ClientFactory, options?: MetaEntryOptions): MetaCacheEntry;
56
+ /**
57
+ * Keep `entry` in the cache while it is in use: a held entry is never
58
+ * evicted by the size limit ({@link setMetaCacheMaxEntries}), so later
59
+ * `getMetaEntry` / `resolveValueHelp` calls for its URL keep returning it.
60
+ * Call the returned function (idempotent) once done — `useTable` and the
61
+ * form value-help do this for their component's lifetime. A reset still
62
+ * drops held entries. Since 0.1.154.
63
+ */
64
+ declare function retainMetaEntry(entry: MetaCacheEntry): () => void;
65
+ /**
66
+ * How many URLs the meta cache keeps (default `100`). Least recently used
67
+ * entries beyond it are dropped — entries held with {@link retainMetaEntry}
68
+ * (every mounted table and form value-help) never are, and may exceed it.
69
+ * At least `1`; `Infinity` turns the limit off; no argument restores the
70
+ * default. Since 0.1.154.
71
+ */
72
+ declare function setMetaCacheMaxEntries(max?: number): void;
73
+ /**
74
+ * The `TableDef` for a cache entry, built once per entry — and once per
75
+ * `/meta` `ETag` across entries. Since 0.1.153.
76
+ */
77
+ declare function getMetaTableDef(entry: MetaCacheEntry): Promise<TableDef>;
78
+ /**
79
+ * Drop every cached `/meta` — including the copy each cached `Client`
80
+ * memoizes and the `@atscript/db-client` meta stores used to revalidate it
81
+ * (the shared default one and any custom one a factory supplied) — the
82
+ * value-help searches shared under them, and everything registered through
83
+ * {@link onMetaCacheReset} (e.g. the table-presets cache). Entries already
84
+ * handed out keep working; the next `getMetaEntry` refetches.
85
+ */
86
+ declare function resetMetaCache(): void;
87
+ /**
88
+ * Bind the meta cache to the current viewer. Call it whenever the signed-in
89
+ * user or their role may have changed — after login, after logout and on
90
+ * every reload of the current user. A key that differs from the previous
91
+ * one resets the cache (`resetMetaCache`); the same key keeps it. `null` /
92
+ * `undefined` = anonymous.
93
+ *
94
+ * Binding an identity also turns on caches that are only safe when the app
95
+ * reports sign-in changes, such as the table-presets cache.
96
+ */
97
+ declare function setMetaCacheIdentity(key: string | null | undefined): void;
98
+ /**
99
+ * The identity last passed to {@link setMetaCacheIdentity}: `undefined` when
100
+ * the app never bound one, `null` for anonymous. Since 0.1.153.
101
+ */
102
+ declare function getMetaCacheIdentity(): string | null | undefined;
103
+ /**
104
+ * Run `listener` on every {@link resetMetaCache} (and so on every identity
105
+ * change). For caches that belong to the signed-in viewer. Returns an
106
+ * unsubscribe function. Since 0.1.153.
107
+ */
108
+ declare function onMetaCacheReset(listener: () => void): () => void;
109
+ //#endregion
110
+ export { MetaCacheEntry, MetaEntryOptions, getMetaCacheIdentity, getMetaEntry, getMetaTableDef, onMetaCacheReset, resetMetaCache, retainMetaEntry, setMetaCacheIdentity, setMetaCacheMaxEntries };