@atscript/ui 0.1.164 → 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 (60) hide show
  1. package/dist/client-factory.d.cts +6 -1
  2. package/dist/client-factory.d.mts +6 -1
  3. package/dist/form/create-form-def.cjs +28 -6
  4. package/dist/form/create-form-def.mjs +29 -7
  5. package/dist/form/labels.cjs +7 -4
  6. package/dist/form/labels.d.cts +6 -2
  7. package/dist/form/labels.d.mts +6 -2
  8. package/dist/form/labels.mjs +7 -4
  9. package/dist/form/types.d.cts +14 -0
  10. package/dist/form/types.d.mts +14 -0
  11. package/dist/form/validate.cjs +59 -23
  12. package/dist/form/validate.d.cts +37 -6
  13. package/dist/form/validate.d.mts +37 -6
  14. package/dist/form/validate.mjs +58 -23
  15. package/dist/i18n/ui-texts.cjs +135 -0
  16. package/dist/i18n/ui-texts.d.cts +81 -0
  17. package/dist/i18n/ui-texts.d.mts +81 -0
  18. package/dist/i18n/ui-texts.mjs +132 -0
  19. package/dist/index.cjs +6 -0
  20. package/dist/index.d.cts +7 -6
  21. package/dist/index.d.mts +7 -6
  22. package/dist/index.mjs +3 -2
  23. package/dist/messages.cjs +301 -0
  24. package/dist/messages.d.cts +302 -0
  25. package/dist/messages.d.mts +302 -0
  26. package/dist/messages.mjs +300 -0
  27. package/dist/plugin/annotations.cjs +30 -15
  28. package/dist/plugin/annotations.mjs +30 -15
  29. package/dist/plugin/value-help-validation.cjs +17 -7
  30. package/dist/plugin/value-help-validation.mjs +17 -7
  31. package/dist/shared/meta-cache.cjs +75 -37
  32. package/dist/shared/meta-cache.d.cts +40 -20
  33. package/dist/shared/meta-cache.d.mts +40 -20
  34. package/dist/shared/meta-cache.mjs +75 -37
  35. package/dist/table/create-table-def.cjs +17 -8
  36. package/dist/table/create-table-def.d.cts +5 -1
  37. package/dist/table/create-table-def.d.mts +5 -1
  38. package/dist/table/create-table-def.mjs +18 -9
  39. package/dist/table/types.d.cts +5 -2
  40. package/dist/table/types.d.mts +5 -2
  41. package/dist/table/value-kind.cjs +6 -6
  42. package/dist/table/value-kind.d.cts +8 -7
  43. package/dist/table/value-kind.d.mts +8 -7
  44. package/dist/table/value-kind.mjs +6 -6
  45. package/dist/value-help/extract-literals.cjs +61 -22
  46. package/dist/value-help/extract-literals.d.cts +20 -8
  47. package/dist/value-help/extract-literals.d.mts +20 -8
  48. package/dist/value-help/extract-literals.mjs +60 -23
  49. package/dist/value-help/index.d.mts +1 -1
  50. package/dist/value-help/resolve-options.cjs +8 -4
  51. package/dist/value-help/resolve-options.d.cts +8 -3
  52. package/dist/value-help/resolve-options.d.mts +8 -3
  53. package/dist/value-help/resolve-options.mjs +9 -5
  54. package/dist/value-help/resolve.cjs +7 -3
  55. package/dist/value-help/resolve.d.cts +7 -2
  56. package/dist/value-help/resolve.d.mts +7 -2
  57. package/dist/value-help/resolve.mjs +7 -3
  58. package/dist/value-help/types.d.cts +5 -0
  59. package/dist/value-help/types.d.mts +5 -0
  60. package/package.json +15 -10
@@ -119,9 +119,16 @@ function hostDesign(token, doc) {
119
119
  const host = doc.annotatedDefinition(token.parentNode);
120
120
  return host ? designOf(host.def, host.doc) : void 0;
121
121
  }
122
- /** Collects the literal values of a pure literal union; `undefined` when it cannot be resolved. */
122
+ /** The values of a `boolean` host, as `@ui.literalLabel` names them. */
123
+ const BOOLEAN_VALUES = ["true", "false"];
124
+ /**
125
+ * Collects the values of a pure literal union or a `boolean` (`'true'` / `'false'`), or
126
+ * of an array of one; `null` / `undefined` members (`boolean | null`) have none. `false`
127
+ * when the host is anything else, `undefined` when it cannot be resolved.
128
+ */
123
129
  function collectLiterals(def, doc, out, depth = 0) {
124
130
  let unresolved = false;
131
+ const start = out.length;
125
132
  for (const leaf of leavesOf(def, doc, depth)) if (!leaf) unresolved = true;
126
133
  else if (leaf.def.entity === "const") {
127
134
  const text = leaf.def.token("identifier")?.text;
@@ -135,8 +142,10 @@ function collectLiterals(def, doc, out, depth = 0) {
135
142
  const ok = collectLiterals(leaf.def.getDefinition(), leaf.doc, out, depth + 1);
136
143
  if (ok === false) return false;
137
144
  if (ok === void 0) unresolved = true;
138
- } else return false;
139
- return unresolved ? void 0 : true;
145
+ } else if (leaf.def.entity === "primitive" && primitiveBaseType(leaf.def) === "boolean") for (const text of BOOLEAN_VALUES) out.push({ text });
146
+ else if (!isEmptyLeaf(leaf.def)) return false;
147
+ if (unresolved) return void 0;
148
+ return out.length > start;
140
149
  }
141
150
  /** {@link collectLiterals} of the type an annotated node stands for. */
142
151
  function collectHostLiterals(node, doc, out) {
@@ -346,7 +355,8 @@ function validateDistinct(token, _args, doc) {
346
355
  /**
347
356
  * `valueScope` of `@ui.literalLabel`'s `value`: the literals of the annotated
348
357
  * union (or array of one), each linked to the literal it declares — completion
349
- * offers them and go-to-definition on the value jumps to the literal.
358
+ * offers them and go-to-definition on the value jumps to the literal. A `boolean`
359
+ * host offers `'true'` / `'false'` (since 0.1.165).
350
360
  */
351
361
  function literalLabelValues(annotationToken, doc) {
352
362
  const found = [];
@@ -356,10 +366,10 @@ function literalLabelValues(annotationToken, doc) {
356
366
  for (const { text, def, doc: declaring } of found) {
357
367
  if (seen.has(text)) continue;
358
368
  seen.add(text);
359
- const token = def.token("identifier");
369
+ const token = def?.token("identifier");
360
370
  candidates.push({
361
371
  value: text,
362
- ...token && { definition: {
372
+ ...token && declaring && { definition: {
363
373
  doc: declaring,
364
374
  token
365
375
  } }
@@ -375,7 +385,7 @@ function validateLiteralLabel(token, args, doc) {
375
385
  const literals = found.map((l) => l.text);
376
386
  if (pure === false) {
377
387
  errors.push({
378
- message: "@ui.literalLabel requires a union of literal values (or an array of one)",
388
+ message: "@ui.literalLabel requires a union of literal values or a boolean (or an array of one)",
379
389
  severity: ERROR,
380
390
  range: token.range
381
391
  });
@@ -1,3 +1,4 @@
1
+ const require_ui_texts = require("../i18n/ui-texts.cjs");
1
2
  const require_value_help_client = require("../value-help/value-help-client.cjs");
2
3
  const require_client_factory = require("../client-factory.cjs");
3
4
  const require_create_table_def = require("../table/create-table-def.cjs");
@@ -13,7 +14,7 @@ let _atscript_db_client = require("@atscript/db-client");
13
14
  */
14
15
  const DEFAULT_MAX_ENTRIES = 100;
15
16
  let maxEntries = DEFAULT_MAX_ENTRIES;
16
- /** Per-URL entries, least recently used first. Browser only. */
17
+ /** Entries by {@link cacheKey}, least recently used first. Browser only. */
17
18
  const cache = /* @__PURE__ */ new Map();
18
19
  const slots = /* @__PURE__ */ new WeakMap();
19
20
  /** Derived shapes per `ETag`, counted by the cached entries that use them. Browser only. */
@@ -23,28 +24,37 @@ const customStores = /* @__PURE__ */ new Map();
23
24
  /** `undefined` until the first `setMetaCacheIdentity` call; `null` = anonymous. */
24
25
  let identity;
25
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
+ }
26
31
  /**
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`.
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`.
33
38
  */
34
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);
35
43
  const browser = typeof window !== "undefined";
36
- const existing = browser ? cache.get(url) : void 0;
44
+ const existing = browser ? cache.get(key) : void 0;
37
45
  if (existing) {
38
- touch(url, existing);
46
+ touch(key, existing);
47
+ mergeServedTexts(existing, texts, locale);
39
48
  return existing;
40
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;
41
54
  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);
55
+ const client = Object.keys(clientOptions).length > 0 ? f(url, clientOptions) : f(url);
46
56
  const meta = client.meta().catch((err) => {
47
- if (cache.get(url) === entry) evict(url, entry);
57
+ if (cache.get(key) === entry) evict(key, entry);
48
58
  throw err;
49
59
  });
50
60
  const derived = meta.then((m) => derive(entry, client, m));
@@ -56,20 +66,33 @@ function getMetaEntry(url, factory, options) {
56
66
  type
57
67
  };
58
68
  const slot = {
59
- url,
69
+ key,
60
70
  derived,
61
- holds: 0
71
+ holds: 0,
72
+ tableDefs: /* @__PURE__ */ new Map(),
73
+ mergedInto: /* @__PURE__ */ new WeakSet()
62
74
  };
63
75
  slots.set(entry, slot);
64
76
  if (browser) {
65
77
  slot.store = customStoreOf(client);
66
78
  if (slot.store) customStores.set(slot.store, (customStores.get(slot.store) ?? 0) + 1);
67
- cache.set(url, entry);
79
+ cache.set(key, entry);
68
80
  trim(entry);
69
81
  }
82
+ mergeServedTexts(entry, texts, locale);
70
83
  return entry;
71
84
  }
72
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
+ /**
73
96
  * Keep `entry` in the cache while it is in use: a held entry is never
74
97
  * evicted by the size limit ({@link setMetaCacheMaxEntries}), so later
75
98
  * `getMetaEntry` / `resolveValueHelp` calls for its URL keep returning it.
@@ -86,16 +109,17 @@ function retainMetaEntry(entry) {
86
109
  if (released) return;
87
110
  released = true;
88
111
  slot.holds--;
89
- if (slot.holds === 0 && cache.get(slot.url) === entry) {
90
- touch(slot.url, entry);
112
+ if (slot.holds === 0 && cache.get(slot.key) === entry) {
113
+ touch(slot.key, entry);
91
114
  trim();
92
115
  }
93
116
  };
94
117
  }
95
118
  /**
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.
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.
99
123
  * At least `1`; `Infinity` turns the limit off; no argument restores the
100
124
  * default. Since 0.1.154.
101
125
  */
@@ -103,9 +127,9 @@ function setMetaCacheMaxEntries(max) {
103
127
  maxEntries = max === void 0 ? DEFAULT_MAX_ENTRIES : max >= 1 ? max : 1;
104
128
  trim();
105
129
  }
106
- function touch(url, entry) {
107
- cache.delete(url);
108
- cache.set(url, entry);
130
+ function touch(key, entry) {
131
+ cache.delete(key);
132
+ cache.set(key, entry);
109
133
  }
110
134
  /**
111
135
  * Drop least recently used entries nothing holds until the cache fits.
@@ -113,8 +137,8 @@ function touch(url, entry) {
113
137
  */
114
138
  function trim(spare) {
115
139
  if (cache.size <= maxEntries) return;
116
- for (const [url, entry] of cache) {
117
- if (entry !== spare && slots.get(entry).holds === 0) evict(url, entry);
140
+ for (const [key, entry] of cache) {
141
+ if (entry !== spare && slots.get(entry).holds === 0) evict(key, entry);
118
142
  if (cache.size <= maxEntries) return;
119
143
  }
120
144
  }
@@ -124,8 +148,8 @@ function trim(spare) {
124
148
  * `/meta` goes now — a factory that reuses clients per URL would otherwise
125
149
  * hand it to the next viewer.
126
150
  */
127
- function evict(url, entry) {
128
- cache.delete(url);
151
+ function evict(key, entry) {
152
+ cache.delete(key);
129
153
  entry.client.invalidateMeta?.();
130
154
  const slot = slots.get(entry);
131
155
  if (slot.etag !== void 0 && --derivedByEtag.get(slot.etag).refs === 0) derivedByEtag.delete(slot.etag);
@@ -139,15 +163,29 @@ function evict(url, entry) {
139
163
  }
140
164
  }
141
165
  /**
142
- * The `TableDef` for a cache entry, built once per entry — and once per
143
- * `/meta` `ETag` across entries. Since 0.1.153.
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.
144
171
  */
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));
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);
149
187
  }
150
- return entry.tableDef;
188
+ return def;
151
189
  }
152
190
  /**
153
191
  * Shares the `Derived` of one `ETag` among the cached entries that loaded it;
@@ -155,7 +193,7 @@ function getMetaTableDef(entry) {
155
193
  */
156
194
  function derive(entry, client, meta) {
157
195
  const slot = slots.get(entry);
158
- const etag = cache.get(slot.url) === entry ? client.metaEtag?.() : void 0;
196
+ const etag = cache.get(slot.key) === entry ? client.metaEtag?.() : void 0;
159
197
  if (etag === void 0) return { type: (0, _atscript_typescript_utils.deserializeAnnotatedType)(meta.type) };
160
198
  let shared = derivedByEtag.get(etag);
161
199
  if (shared) shared.refs++;
@@ -1,23 +1,32 @@
1
- import { ResolvedValueHelp } from "../value-help/resolve.cjs";
2
- import { MetaResponse, TableDef } from "../table/types.cjs";
3
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";
4
5
  import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
5
6
  import { Client } from "@atscript/db-client";
6
7
 
7
8
  //#region src/shared/meta-cache.d.ts
8
9
  /**
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
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
12
13
  * `resolveValueHelp` and `useTable` route through it so a given URL triggers
13
- * at most one network round-trip and one `deserializeAnnotatedType`.
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.
14
22
  *
15
23
  * Byte-identical `/meta` bodies (same `ETag`, e.g. one parametric controller
16
24
  * under different route params) also share the deserialized type and the
17
25
  * `TableDef` across URLs.
18
26
  *
19
27
  * The cache keeps the {@link setMetaCacheMaxEntries} most recently used
20
- * URLs; an entry held with {@link retainMetaEntry} is never evicted.
28
+ * entries (one per URL and locale); an entry held with {@link retainMetaEntry}
29
+ * is never evicted.
21
30
  *
22
31
  * `/meta` is projected per viewer, so the cache belongs to one identity
23
32
  * (`setMetaCacheIdentity`) and exists only in the browser: during server
@@ -29,7 +38,6 @@ interface MetaCacheEntry {
29
38
  meta: Promise<MetaResponse>;
30
39
  type: Promise<TAtscriptAnnotatedType>;
31
40
  resolved?: Promise<ResolvedValueHelp>;
32
- tableDef?: Promise<TableDef>;
33
41
  }
34
42
  /** Options for {@link getMetaEntry}. */
35
43
  interface MetaEntryOptions {
@@ -43,14 +51,22 @@ interface MetaEntryOptions {
43
51
  * URL, like the factory. Since 0.1.153.
44
52
  */
45
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">;
46
62
  }
47
63
  /**
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`.
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`.
54
70
  */
55
71
  declare function getMetaEntry(url: string, factory?: ClientFactory, options?: MetaEntryOptions): MetaCacheEntry;
56
72
  /**
@@ -63,18 +79,22 @@ declare function getMetaEntry(url: string, factory?: ClientFactory, options?: Me
63
79
  */
64
80
  declare function retainMetaEntry(entry: MetaCacheEntry): () => void;
65
81
  /**
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.
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.
69
86
  * At least `1`; `Infinity` turns the limit off; no argument restores the
70
87
  * default. Since 0.1.154.
71
88
  */
72
89
  declare function setMetaCacheMaxEntries(max?: number): void;
73
90
  /**
74
- * The `TableDef` for a cache entry, built once per entry — and once per
75
- * `/meta` `ETag` across entries. Since 0.1.153.
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.
76
96
  */
77
- declare function getMetaTableDef(entry: MetaCacheEntry): Promise<TableDef>;
97
+ declare function getMetaTableDef(entry: MetaCacheEntry, texts?: Pick<UiTexts, "locale" | "t">): Promise<TableDef>;
78
98
  /**
79
99
  * Drop every cached `/meta` — including the copy each cached `Client`
80
100
  * memoizes and the `@atscript/db-client` meta stores used to revalidate it
@@ -1,23 +1,32 @@
1
- import { ResolvedValueHelp } from "../value-help/resolve.mjs";
2
- import { MetaResponse, TableDef } from "../table/types.mjs";
3
1
  import { ClientFactory } from "../client-factory.mjs";
2
+ import { MetaResponse, TableDef } from "../table/types.mjs";
3
+ import { UiTexts } from "../i18n/ui-texts.mjs";
4
+ import { ResolvedValueHelp } from "../value-help/resolve.mjs";
4
5
  import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
5
6
  import { Client } from "@atscript/db-client";
6
7
 
7
8
  //#region src/shared/meta-cache.d.ts
8
9
  /**
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
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
12
13
  * `resolveValueHelp` and `useTable` route through it so a given URL triggers
13
- * at most one network round-trip and one `deserializeAnnotatedType`.
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.
14
22
  *
15
23
  * Byte-identical `/meta` bodies (same `ETag`, e.g. one parametric controller
16
24
  * under different route params) also share the deserialized type and the
17
25
  * `TableDef` across URLs.
18
26
  *
19
27
  * The cache keeps the {@link setMetaCacheMaxEntries} most recently used
20
- * URLs; an entry held with {@link retainMetaEntry} is never evicted.
28
+ * entries (one per URL and locale); an entry held with {@link retainMetaEntry}
29
+ * is never evicted.
21
30
  *
22
31
  * `/meta` is projected per viewer, so the cache belongs to one identity
23
32
  * (`setMetaCacheIdentity`) and exists only in the browser: during server
@@ -29,7 +38,6 @@ interface MetaCacheEntry {
29
38
  meta: Promise<MetaResponse>;
30
39
  type: Promise<TAtscriptAnnotatedType>;
31
40
  resolved?: Promise<ResolvedValueHelp>;
32
- tableDef?: Promise<TableDef>;
33
41
  }
34
42
  /** Options for {@link getMetaEntry}. */
35
43
  interface MetaEntryOptions {
@@ -43,14 +51,22 @@ interface MetaEntryOptions {
43
51
  * URL, like the factory. Since 0.1.153.
44
52
  */
45
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">;
46
62
  }
47
63
  /**
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`.
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`.
54
70
  */
55
71
  declare function getMetaEntry(url: string, factory?: ClientFactory, options?: MetaEntryOptions): MetaCacheEntry;
56
72
  /**
@@ -63,18 +79,22 @@ declare function getMetaEntry(url: string, factory?: ClientFactory, options?: Me
63
79
  */
64
80
  declare function retainMetaEntry(entry: MetaCacheEntry): () => void;
65
81
  /**
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.
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.
69
86
  * At least `1`; `Infinity` turns the limit off; no argument restores the
70
87
  * default. Since 0.1.154.
71
88
  */
72
89
  declare function setMetaCacheMaxEntries(max?: number): void;
73
90
  /**
74
- * The `TableDef` for a cache entry, built once per entry — and once per
75
- * `/meta` `ETag` across entries. Since 0.1.153.
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.
76
96
  */
77
- declare function getMetaTableDef(entry: MetaCacheEntry): Promise<TableDef>;
97
+ declare function getMetaTableDef(entry: MetaCacheEntry, texts?: Pick<UiTexts, "locale" | "t">): Promise<TableDef>;
78
98
  /**
79
99
  * Drop every cached `/meta` — including the copy each cached `Client`
80
100
  * memoizes and the `@atscript/db-client` meta stores used to revalidate it