@atscript/ui 0.1.163 → 0.1.165

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 (164) hide show
  1. package/dist/client-factory.cjs +27 -0
  2. package/dist/client-factory.d.cts +58 -0
  3. package/dist/client-factory.d.mts +58 -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 +462 -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 +460 -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 +13 -0
  42. package/dist/form/labels.d.cts +11 -0
  43. package/dist/form/labels.d.mts +11 -0
  44. package/dist/form/labels.mjs +13 -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 +147 -0
  63. package/dist/form/types.d.mts +147 -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 +186 -0
  70. package/dist/form/validate.d.cts +81 -0
  71. package/dist/form/validate.d.mts +81 -0
  72. package/dist/form/validate.mjs +182 -0
  73. package/dist/i18n/ui-texts.cjs +135 -0
  74. package/dist/i18n/ui-texts.d.cts +81 -0
  75. package/dist/i18n/ui-texts.d.mts +81 -0
  76. package/dist/i18n/ui-texts.mjs +132 -0
  77. package/dist/index.cjs +223 -3360
  78. package/dist/index.d.cts +39 -1693
  79. package/dist/index.d.mts +39 -1693
  80. package/dist/index.mjs +36 -3179
  81. package/dist/messages.cjs +301 -0
  82. package/dist/messages.d.cts +302 -0
  83. package/dist/messages.d.mts +302 -0
  84. package/dist/messages.mjs +300 -0
  85. package/dist/nav/model-routes.cjs +59 -0
  86. package/dist/nav/model-routes.d.cts +33 -0
  87. package/dist/nav/model-routes.d.mts +33 -0
  88. package/dist/nav/model-routes.mjs +59 -0
  89. package/dist/plugin/annotations.cjs +592 -0
  90. package/dist/plugin/annotations.mjs +592 -0
  91. package/dist/plugin/primitives.cjs +30 -0
  92. package/dist/plugin/primitives.mjs +30 -0
  93. package/dist/plugin/value-help-validation.cjs +429 -0
  94. package/dist/plugin/value-help-validation.mjs +423 -0
  95. package/dist/plugin.cjs +4 -1001
  96. package/dist/plugin.mjs +2 -999
  97. package/dist/shared/annotation-keys.cjs +189 -0
  98. package/dist/shared/annotation-keys.d.cts +82 -0
  99. package/dist/shared/annotation-keys.d.mts +82 -0
  100. package/dist/shared/annotation-keys.mjs +97 -0
  101. package/dist/shared/field-resolver.cjs +123 -0
  102. package/dist/shared/field-resolver.d.cts +77 -0
  103. package/dist/shared/field-resolver.d.mts +77 -0
  104. package/dist/shared/field-resolver.mjs +110 -0
  105. package/dist/shared/meta-cache.cjs +282 -0
  106. package/dist/shared/meta-cache.d.cts +130 -0
  107. package/dist/shared/meta-cache.d.mts +130 -0
  108. package/dist/shared/meta-cache.mjs +275 -0
  109. package/dist/shared/str.cjs +19 -0
  110. package/dist/shared/str.d.cts +7 -0
  111. package/dist/shared/str.d.mts +7 -0
  112. package/dist/shared/str.mjs +17 -0
  113. package/dist/table/column-resolver.cjs +17 -0
  114. package/dist/table/column-resolver.d.cts +11 -0
  115. package/dist/table/column-resolver.d.mts +11 -0
  116. package/dist/table/column-resolver.mjs +15 -0
  117. package/dist/table/create-table-def.cjs +242 -0
  118. package/dist/table/create-table-def.d.cts +19 -0
  119. package/dist/table/create-table-def.d.mts +19 -0
  120. package/dist/table/create-table-def.mjs +242 -0
  121. package/dist/table/navigate-href.cjs +45 -0
  122. package/dist/table/navigate-href.d.cts +31 -0
  123. package/dist/table/navigate-href.d.mts +31 -0
  124. package/dist/table/navigate-href.mjs +45 -0
  125. package/dist/table/types.d.cts +290 -0
  126. package/dist/table/types.d.mts +290 -0
  127. package/dist/table/value-kind.cjs +20 -0
  128. package/dist/table/value-kind.d.cts +16 -0
  129. package/dist/table/value-kind.d.mts +16 -0
  130. package/dist/table/value-kind.mjs +19 -0
  131. package/dist/value-help/dict-paths.cjs +17 -0
  132. package/dist/value-help/dict-paths.d.cts +11 -0
  133. package/dist/value-help/dict-paths.d.mts +11 -0
  134. package/dist/value-help/dict-paths.mjs +17 -0
  135. package/dist/value-help/distinct-client.cjs +51 -0
  136. package/dist/value-help/distinct-client.d.cts +40 -0
  137. package/dist/value-help/distinct-client.d.mts +40 -0
  138. package/dist/value-help/distinct-client.mjs +50 -0
  139. package/dist/value-help/extract-literals.cjs +115 -0
  140. package/dist/value-help/extract-literals.d.cts +44 -0
  141. package/dist/value-help/extract-literals.d.mts +44 -0
  142. package/dist/value-help/extract-literals.mjs +111 -0
  143. package/dist/value-help/extract-ref.cjs +100 -0
  144. package/dist/value-help/extract-ref.d.cts +37 -0
  145. package/dist/value-help/extract-ref.d.mts +37 -0
  146. package/dist/value-help/extract-ref.mjs +100 -0
  147. package/dist/value-help/filter-tree.cjs +45 -0
  148. package/dist/value-help/filter-tree.mjs +45 -0
  149. package/dist/value-help/index.d.mts +8 -0
  150. package/dist/value-help/resolve-options.cjs +68 -0
  151. package/dist/value-help/resolve-options.d.cts +40 -0
  152. package/dist/value-help/resolve-options.d.mts +40 -0
  153. package/dist/value-help/resolve-options.mjs +64 -0
  154. package/dist/value-help/resolve.cjs +66 -0
  155. package/dist/value-help/resolve.d.cts +36 -0
  156. package/dist/value-help/resolve.d.mts +36 -0
  157. package/dist/value-help/resolve.mjs +65 -0
  158. package/dist/value-help/types.d.cts +41 -0
  159. package/dist/value-help/types.d.mts +41 -0
  160. package/dist/value-help/value-help-client.cjs +136 -0
  161. package/dist/value-help/value-help-client.d.cts +67 -0
  162. package/dist/value-help/value-help-client.d.mts +67 -0
  163. package/dist/value-help/value-help-client.mjs +134 -0
  164. package/package.json +15 -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,282 @@
1
+ const require_ui_texts = require("../i18n/ui-texts.cjs");
2
+ const require_value_help_client = require("../value-help/value-help-client.cjs");
3
+ const require_client_factory = require("../client-factory.cjs");
4
+ const require_create_table_def = require("../table/create-table-def.cjs");
5
+ let _atscript_typescript_utils = require("@atscript/typescript/utils");
6
+ let _atscript_db_client = require("@atscript/db-client");
7
+ //#region src/shared/meta-cache.ts
8
+ /**
9
+ * Enough for every URL a page touches — a table, the dictionaries its
10
+ * filters and forms pick from — plus the recent navigation history, so going
11
+ * back is instant; small enough to cap a long session at a few MB of parsed
12
+ * `/meta`. Twice the `@atscript/db-client` `MetaStore` body limit, since
13
+ * parametric URLs share bodies.
14
+ */
15
+ const DEFAULT_MAX_ENTRIES = 100;
16
+ let maxEntries = DEFAULT_MAX_ENTRIES;
17
+ /** Entries by {@link cacheKey}, least recently used first. Browser only. */
18
+ const cache = /* @__PURE__ */ new Map();
19
+ const slots = /* @__PURE__ */ new WeakMap();
20
+ /** Derived shapes per `ETag`, counted by the cached entries that use them. Browser only. */
21
+ const derivedByEtag = /* @__PURE__ */ new Map();
22
+ /** Custom `/meta` stores of cached clients, counted by the entries that use them. */
23
+ const customStores = /* @__PURE__ */ new Map();
24
+ /** `undefined` until the first `setMetaCacheIdentity` call; `null` = anonymous. */
25
+ let identity;
26
+ const resetListeners = /* @__PURE__ */ new Set();
27
+ /** The cache key of `url` in `locale`. */
28
+ function cacheKey(url, locale) {
29
+ return locale === void 0 ? url : `${url}\n${locale}`;
30
+ }
31
+ /**
32
+ * Get or create the cache entry for `url` in the UI locale of
33
+ * `options.texts`. First caller's `factory` (and `options.metaKey`) seeds the
34
+ * `Client`; subsequent callers reuse it. On `meta` rejection, the entry is
35
+ * evicted so the next call retries. A hit marks the entry most recently used;
36
+ * hold an entry you keep using with {@link retainMetaEntry}. Without a browser
37
+ * `window` nothing is cached and the client is asked for `metaStore: false`.
38
+ */
39
+ function getMetaEntry(url, factory, options) {
40
+ const texts = options?.texts ?? require_ui_texts.getUiTexts();
41
+ const locale = texts.locale();
42
+ const key = cacheKey(url, locale);
43
+ const browser = typeof window !== "undefined";
44
+ const existing = browser ? cache.get(key) : void 0;
45
+ if (existing) {
46
+ touch(key, existing);
47
+ mergeServedTexts(existing, texts, locale);
48
+ return existing;
49
+ }
50
+ const clientOptions = {};
51
+ if (!browser) clientOptions.metaStore = false;
52
+ else if (options?.metaKey) clientOptions.metaKey = options.metaKey;
53
+ if (locale !== void 0) clientOptions.locale = locale;
54
+ const f = factory ?? require_client_factory.getDefaultClientFactory();
55
+ const client = Object.keys(clientOptions).length > 0 ? f(url, clientOptions) : f(url);
56
+ const meta = client.meta().catch((err) => {
57
+ if (cache.get(key) === entry) evict(key, entry);
58
+ throw err;
59
+ });
60
+ const derived = meta.then((m) => derive(entry, client, m));
61
+ const type = derived.then((d) => d.type);
62
+ type.catch(() => {});
63
+ const entry = {
64
+ client,
65
+ meta,
66
+ type
67
+ };
68
+ const slot = {
69
+ key,
70
+ derived,
71
+ holds: 0,
72
+ tableDefs: /* @__PURE__ */ new Map(),
73
+ mergedInto: /* @__PURE__ */ new WeakSet()
74
+ };
75
+ slots.set(entry, slot);
76
+ if (browser) {
77
+ slot.store = customStoreOf(client);
78
+ if (slot.store) customStores.set(slot.store, (customStores.get(slot.store) ?? 0) + 1);
79
+ cache.set(key, entry);
80
+ trim(entry);
81
+ }
82
+ mergeServedTexts(entry, texts, locale);
83
+ return entry;
84
+ }
85
+ /**
86
+ * Merges the texts `entry`'s `/meta` serves (`type.$i18n`) into `texts` for
87
+ * `locale` — once per entry and texts, when `/meta` arrives.
88
+ */
89
+ function mergeServedTexts(entry, texts, locale) {
90
+ const slot = slots.get(entry);
91
+ if (slot.mergedInto.has(texts)) return;
92
+ slot.mergedInto.add(texts);
93
+ entry.meta.then((m) => texts.merge(locale, m.type?.$i18n)).catch(() => {});
94
+ }
95
+ /**
96
+ * Keep `entry` in the cache while it is in use: a held entry is never
97
+ * evicted by the size limit ({@link setMetaCacheMaxEntries}), so later
98
+ * `getMetaEntry` / `resolveValueHelp` calls for its URL keep returning it.
99
+ * Call the returned function (idempotent) once done — `useTable` and the
100
+ * form value-help do this for their component's lifetime. A reset still
101
+ * drops held entries. Since 0.1.154.
102
+ */
103
+ function retainMetaEntry(entry) {
104
+ const slot = slots.get(entry);
105
+ if (!slot) return () => {};
106
+ slot.holds++;
107
+ let released = false;
108
+ return () => {
109
+ if (released) return;
110
+ released = true;
111
+ slot.holds--;
112
+ if (slot.holds === 0 && cache.get(slot.key) === entry) {
113
+ touch(slot.key, entry);
114
+ trim();
115
+ }
116
+ };
117
+ }
118
+ /**
119
+ * How many entries (one per URL and UI locale) the meta cache keeps (default
120
+ * `100`). Least recently used entries beyond it are dropped — entries held
121
+ * with {@link retainMetaEntry} (every mounted table and form value-help)
122
+ * never are, and may exceed it.
123
+ * At least `1`; `Infinity` turns the limit off; no argument restores the
124
+ * default. Since 0.1.154.
125
+ */
126
+ function setMetaCacheMaxEntries(max) {
127
+ maxEntries = max === void 0 ? DEFAULT_MAX_ENTRIES : max >= 1 ? max : 1;
128
+ trim();
129
+ }
130
+ function touch(key, entry) {
131
+ cache.delete(key);
132
+ cache.set(key, entry);
133
+ }
134
+ /**
135
+ * Drop least recently used entries nothing holds until the cache fits.
136
+ * `spare` — the entry just handed out, about to be retained by its caller.
137
+ */
138
+ function trim(spare) {
139
+ if (cache.size <= maxEntries) return;
140
+ for (const [key, entry] of cache) {
141
+ if (entry !== spare && slots.get(entry).holds === 0) evict(key, entry);
142
+ if (cache.size <= maxEntries) return;
143
+ }
144
+ }
145
+ /**
146
+ * Remove `entry` and release what only it kept: its shared `Derived` and
147
+ * custom store. A reset no longer reaches its client, so the client's memoized
148
+ * `/meta` goes now — a factory that reuses clients per URL would otherwise
149
+ * hand it to the next viewer.
150
+ */
151
+ function evict(key, entry) {
152
+ cache.delete(key);
153
+ entry.client.invalidateMeta?.();
154
+ const slot = slots.get(entry);
155
+ if (slot.etag !== void 0 && --derivedByEtag.get(slot.etag).refs === 0) derivedByEtag.delete(slot.etag);
156
+ if (slot.store) {
157
+ const refs = customStores.get(slot.store);
158
+ if (refs > 1) customStores.set(slot.store, refs - 1);
159
+ else {
160
+ slot.store.clear();
161
+ customStores.delete(slot.store);
162
+ }
163
+ }
164
+ }
165
+ /**
166
+ * The `TableDef` for a cache entry in a UI locale, built once per entry and
167
+ * locale — and once per `/meta` `ETag` and locale across entries. Its labels
168
+ * (boolean `Yes` / `No`) are worded with `texts`: the active UI texts by
169
+ * default; a component passes its app's, so a server render and a remount
170
+ * in another locale get their own. Since 0.1.153; per locale since 0.1.165.
171
+ */
172
+ function getMetaTableDef(entry, texts = require_ui_texts.getUiTexts()) {
173
+ const slot = slots.get(entry);
174
+ const key = texts.locale() ?? "";
175
+ let def = slot?.tableDefs.get(key);
176
+ if (!def) {
177
+ def = Promise.all([entry.meta, slot?.derived ?? entry.type.then((type) => ({ type }))]).then(([meta, d]) => {
178
+ const defs = d.tableDefs ??= /* @__PURE__ */ new Map();
179
+ let tableDef = defs.get(key);
180
+ if (!tableDef) {
181
+ tableDef = require_create_table_def.createTableDef(meta, d.type, texts.t);
182
+ defs.set(key, tableDef);
183
+ }
184
+ return tableDef;
185
+ });
186
+ slot?.tableDefs.set(key, def);
187
+ }
188
+ return def;
189
+ }
190
+ /**
191
+ * Shares the `Derived` of one `ETag` among the cached entries that loaded it;
192
+ * an entry no longer cached (evicted, reset, server render) keeps its own.
193
+ */
194
+ function derive(entry, client, meta) {
195
+ const slot = slots.get(entry);
196
+ const etag = cache.get(slot.key) === entry ? client.metaEtag?.() : void 0;
197
+ if (etag === void 0) return { type: (0, _atscript_typescript_utils.deserializeAnnotatedType)(meta.type) };
198
+ let shared = derivedByEtag.get(etag);
199
+ if (shared) shared.refs++;
200
+ else {
201
+ shared = {
202
+ derived: { type: (0, _atscript_typescript_utils.deserializeAnnotatedType)(meta.type) },
203
+ refs: 1
204
+ };
205
+ derivedByEtag.set(etag, shared);
206
+ }
207
+ slot.etag = etag;
208
+ return shared.derived;
209
+ }
210
+ /**
211
+ * The custom `/meta` store a factory gave `client`, so a reset (and the
212
+ * eviction of its last cached client) can clear it. The shared
213
+ * `defaultMetaStore` is skipped: a reset clears it through
214
+ * `clearMetaStore()`, and an eviction must not drop the ETags of every
215
+ * other URL. A store-less client (`metaStore: false`) is skipped too.
216
+ */
217
+ function customStoreOf(client) {
218
+ const store = client.metaStore;
219
+ return store && store !== _atscript_db_client.defaultMetaStore ? store : void 0;
220
+ }
221
+ /**
222
+ * Drop every cached `/meta` — including the copy each cached `Client`
223
+ * memoizes and the `@atscript/db-client` meta stores used to revalidate it
224
+ * (the shared default one and any custom one a factory supplied) — the
225
+ * value-help searches shared under them, and everything registered through
226
+ * {@link onMetaCacheReset} (e.g. the table-presets cache). Entries already
227
+ * handed out keep working; the next `getMetaEntry` refetches.
228
+ */
229
+ function resetMetaCache() {
230
+ const entries = [...cache.values()];
231
+ cache.clear();
232
+ derivedByEtag.clear();
233
+ for (const entry of entries) entry.client.invalidateMeta?.();
234
+ (0, _atscript_db_client.clearMetaStore)();
235
+ for (const store of customStores.keys()) store.clear();
236
+ customStores.clear();
237
+ require_value_help_client.invalidateValueHelpCache();
238
+ for (const listener of resetListeners) listener();
239
+ }
240
+ /**
241
+ * Bind the meta cache to the current viewer. Call it whenever the signed-in
242
+ * user or their role may have changed — after login, after logout and on
243
+ * every reload of the current user. A key that differs from the previous
244
+ * one resets the cache (`resetMetaCache`); the same key keeps it. `null` /
245
+ * `undefined` = anonymous.
246
+ *
247
+ * Binding an identity also turns on caches that are only safe when the app
248
+ * reports sign-in changes, such as the table-presets cache.
249
+ */
250
+ function setMetaCacheIdentity(key) {
251
+ const next = key ?? null;
252
+ if (next === identity) return;
253
+ identity = next;
254
+ resetMetaCache();
255
+ }
256
+ /**
257
+ * The identity last passed to {@link setMetaCacheIdentity}: `undefined` when
258
+ * the app never bound one, `null` for anonymous. Since 0.1.153.
259
+ */
260
+ function getMetaCacheIdentity() {
261
+ return identity;
262
+ }
263
+ /**
264
+ * Run `listener` on every {@link resetMetaCache} (and so on every identity
265
+ * change). For caches that belong to the signed-in viewer. Returns an
266
+ * unsubscribe function. Since 0.1.153.
267
+ */
268
+ function onMetaCacheReset(listener) {
269
+ resetListeners.add(listener);
270
+ return () => {
271
+ resetListeners.delete(listener);
272
+ };
273
+ }
274
+ //#endregion
275
+ exports.getMetaCacheIdentity = getMetaCacheIdentity;
276
+ exports.getMetaEntry = getMetaEntry;
277
+ exports.getMetaTableDef = getMetaTableDef;
278
+ exports.onMetaCacheReset = onMetaCacheReset;
279
+ exports.resetMetaCache = resetMetaCache;
280
+ exports.retainMetaEntry = retainMetaEntry;
281
+ exports.setMetaCacheIdentity = setMetaCacheIdentity;
282
+ exports.setMetaCacheMaxEntries = setMetaCacheMaxEntries;
@@ -0,0 +1,130 @@
1
+ import { ClientFactory } from "../client-factory.cjs";
2
+ import { MetaResponse, TableDef } from "../table/types.cjs";
3
+ import { UiTexts } from "../i18n/ui-texts.cjs";
4
+ import { ResolvedValueHelp } from "../value-help/resolve.cjs";
5
+ import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
6
+ import { Client } from "@atscript/db-client";
7
+
8
+ //#region src/shared/meta-cache.d.ts
9
+ /**
10
+ * Shared per-URL-and-locale cache. A single entry holds the `Client` instance,
11
+ * the raw `/meta` promise, the deserialized type, and lazily-populated derived
12
+ * shapes (`ResolvedValueHelp` for value-help, `TableDef` for tables). Both
13
+ * `resolveValueHelp` and `useTable` route through it so a given URL triggers
14
+ * at most one network round-trip and one `deserializeAnnotatedType` per UI
15
+ * locale.
16
+ *
17
+ * Each entry belongs to one UI locale (see {@link MetaEntryOptions.texts}):
18
+ * its client sends it as `Accept-Language`, so `/meta` comes back translated,
19
+ * and the texts it serves (`type.$i18n`) are merged into the app's UI texts
20
+ * for that locale. After a locale switch, remounted views get the new
21
+ * locale's entry; switching back finds the previous one still cached.
22
+ *
23
+ * Byte-identical `/meta` bodies (same `ETag`, e.g. one parametric controller
24
+ * under different route params) also share the deserialized type and the
25
+ * `TableDef` across URLs.
26
+ *
27
+ * The cache keeps the {@link setMetaCacheMaxEntries} most recently used
28
+ * entries (one per URL and locale); an entry held with {@link retainMetaEntry}
29
+ * is never evicted.
30
+ *
31
+ * `/meta` is projected per viewer, so the cache belongs to one identity
32
+ * (`setMetaCacheIdentity`) and exists only in the browser: during server
33
+ * rendering one process serves many viewers, so every call builds a fresh,
34
+ * uncached entry whose client keeps no `/meta` store.
35
+ */
36
+ interface MetaCacheEntry {
37
+ client: Client;
38
+ meta: Promise<MetaResponse>;
39
+ type: Promise<TAtscriptAnnotatedType>;
40
+ resolved?: Promise<ResolvedValueHelp>;
41
+ }
42
+ /** Options for {@link getMetaEntry}. */
43
+ interface MetaEntryOptions {
44
+ /**
45
+ * Key under which the `/meta` revalidation store keeps this URL's `ETag`s
46
+ * (default: the `/meta` URL). Give every URL of one parametric mount the
47
+ * route template (e.g. `"/api/db/ticket-issue/:key"`): a new route param
48
+ * then revalidates against the body already loaded for another one and
49
+ * costs a `304` instead of the full `/meta`. Forwarded to the
50
+ * {@link ClientFactory} as `metaKey`; honoured only by the first call per
51
+ * URL, like the factory. Since 0.1.153.
52
+ */
53
+ metaKey?: string;
54
+ /**
55
+ * The app's UI texts (default: the active ones, see `setUiLocale`; the Vue
56
+ * components pass their app's). Their current locale picks the entry and is
57
+ * passed to the {@link ClientFactory} as `locale`; the texts `/meta` serves
58
+ * for it (`type.$i18n`) are merged into them. Pass them on a server render,
59
+ * where no app's texts are active. Since 0.1.165.
60
+ */
61
+ texts?: Pick<UiTexts, "locale" | "merge">;
62
+ }
63
+ /**
64
+ * Get or create the cache entry for `url` in the UI locale of
65
+ * `options.texts`. First caller's `factory` (and `options.metaKey`) seeds the
66
+ * `Client`; subsequent callers reuse it. On `meta` rejection, the entry is
67
+ * evicted so the next call retries. A hit marks the entry most recently used;
68
+ * hold an entry you keep using with {@link retainMetaEntry}. Without a browser
69
+ * `window` nothing is cached and the client is asked for `metaStore: false`.
70
+ */
71
+ declare function getMetaEntry(url: string, factory?: ClientFactory, options?: MetaEntryOptions): MetaCacheEntry;
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
+ declare function retainMetaEntry(entry: MetaCacheEntry): () => void;
81
+ /**
82
+ * How many entries (one per URL and UI locale) the meta cache keeps (default
83
+ * `100`). Least recently used entries beyond it are dropped — entries held
84
+ * with {@link retainMetaEntry} (every mounted table and form value-help)
85
+ * never are, and may exceed it.
86
+ * At least `1`; `Infinity` turns the limit off; no argument restores the
87
+ * default. Since 0.1.154.
88
+ */
89
+ declare function setMetaCacheMaxEntries(max?: number): void;
90
+ /**
91
+ * The `TableDef` for a cache entry in a UI locale, built once per entry and
92
+ * locale — and once per `/meta` `ETag` and locale across entries. Its labels
93
+ * (boolean `Yes` / `No`) are worded with `texts`: the active UI texts by
94
+ * default; a component passes its app's, so a server render and a remount
95
+ * in another locale get their own. Since 0.1.153; per locale since 0.1.165.
96
+ */
97
+ declare function getMetaTableDef(entry: MetaCacheEntry, texts?: Pick<UiTexts, "locale" | "t">): Promise<TableDef>;
98
+ /**
99
+ * Drop every cached `/meta` — including the copy each cached `Client`
100
+ * memoizes and the `@atscript/db-client` meta stores used to revalidate it
101
+ * (the shared default one and any custom one a factory supplied) — the
102
+ * value-help searches shared under them, and everything registered through
103
+ * {@link onMetaCacheReset} (e.g. the table-presets cache). Entries already
104
+ * handed out keep working; the next `getMetaEntry` refetches.
105
+ */
106
+ declare function resetMetaCache(): void;
107
+ /**
108
+ * Bind the meta cache to the current viewer. Call it whenever the signed-in
109
+ * user or their role may have changed — after login, after logout and on
110
+ * every reload of the current user. A key that differs from the previous
111
+ * one resets the cache (`resetMetaCache`); the same key keeps it. `null` /
112
+ * `undefined` = anonymous.
113
+ *
114
+ * Binding an identity also turns on caches that are only safe when the app
115
+ * reports sign-in changes, such as the table-presets cache.
116
+ */
117
+ declare function setMetaCacheIdentity(key: string | null | undefined): void;
118
+ /**
119
+ * The identity last passed to {@link setMetaCacheIdentity}: `undefined` when
120
+ * the app never bound one, `null` for anonymous. Since 0.1.153.
121
+ */
122
+ declare function getMetaCacheIdentity(): string | null | undefined;
123
+ /**
124
+ * Run `listener` on every {@link resetMetaCache} (and so on every identity
125
+ * change). For caches that belong to the signed-in viewer. Returns an
126
+ * unsubscribe function. Since 0.1.153.
127
+ */
128
+ declare function onMetaCacheReset(listener: () => void): () => void;
129
+ //#endregion
130
+ export { MetaCacheEntry, MetaEntryOptions, getMetaCacheIdentity, getMetaEntry, getMetaTableDef, onMetaCacheReset, resetMetaCache, retainMetaEntry, setMetaCacheIdentity, setMetaCacheMaxEntries };