@atscript/ui 0.1.206 → 0.1.208

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 (127) hide show
  1. package/dist/form/create-form-def.cjs +16 -18
  2. package/dist/form/create-form-def.mjs +15 -17
  3. package/dist/form/decimal-format.cjs +8 -3
  4. package/dist/form/decimal-format.d.cts +8 -2
  5. package/dist/form/decimal-format.d.mts +8 -2
  6. package/dist/form/decimal-format.mjs +8 -3
  7. package/dist/form/form-actions.cjs +9 -0
  8. package/dist/form/form-actions.d.cts +7 -1
  9. package/dist/form/form-actions.d.mts +7 -1
  10. package/dist/form/form-actions.mjs +9 -1
  11. package/dist/form/measurement.cjs +58 -0
  12. package/dist/form/measurement.d.cts +36 -1
  13. package/dist/form/measurement.d.mts +36 -1
  14. package/dist/form/measurement.mjs +57 -2
  15. package/dist/form/omit-derived.cjs +11 -0
  16. package/dist/form/omit-derived.d.cts +8 -1
  17. package/dist/form/omit-derived.d.mts +8 -1
  18. package/dist/form/omit-derived.mjs +11 -1
  19. package/dist/form/optional-mode.cjs +7 -7
  20. package/dist/form/optional-mode.d.cts +2 -1
  21. package/dist/form/optional-mode.d.mts +2 -1
  22. package/dist/form/optional-mode.mjs +8 -8
  23. package/dist/form/path-utils.cjs +10 -0
  24. package/dist/form/path-utils.d.cts +8 -1
  25. package/dist/form/path-utils.d.mts +8 -1
  26. package/dist/form/path-utils.mjs +10 -1
  27. package/dist/form/types.cjs +5 -0
  28. package/dist/form/types.d.cts +14 -0
  29. package/dist/form/types.d.mts +14 -0
  30. package/dist/form/types.mjs +5 -1
  31. package/dist/form/validate.cjs +56 -5
  32. package/dist/form/validate.d.cts +30 -2
  33. package/dist/form/validate.d.mts +30 -2
  34. package/dist/form/validate.mjs +56 -6
  35. package/dist/index.cjs +42 -6
  36. package/dist/index.d.cts +20 -12
  37. package/dist/index.d.mts +20 -12
  38. package/dist/index.mjs +23 -15
  39. package/dist/plugin/annotations.cjs +33 -14
  40. package/dist/plugin/annotations.mjs +34 -15
  41. package/dist/plugin/value-help-validation.cjs +26 -6
  42. package/dist/plugin/value-help-validation.mjs +26 -7
  43. package/dist/shared/annotation-keys.cjs +3 -0
  44. package/dist/shared/annotation-keys.d.cts +2 -1
  45. package/dist/shared/annotation-keys.d.mts +2 -1
  46. package/dist/shared/annotation-keys.mjs +3 -1
  47. package/dist/shared/dev.cjs +22 -0
  48. package/dist/shared/dev.d.cts +4 -0
  49. package/dist/shared/dev.d.mts +4 -0
  50. package/dist/shared/dev.mjs +22 -0
  51. package/dist/shared/field-resolver.cjs +21 -1
  52. package/dist/shared/field-resolver.d.cts +15 -1
  53. package/dist/shared/field-resolver.d.mts +15 -1
  54. package/dist/shared/field-resolver.mjs +20 -2
  55. package/dist/shared/merge-filters.cjs +28 -0
  56. package/dist/shared/merge-filters.d.cts +13 -0
  57. package/dist/shared/merge-filters.d.mts +13 -0
  58. package/dist/shared/merge-filters.mjs +28 -0
  59. package/dist/shared/meta-cache.cjs +5 -4
  60. package/dist/shared/meta-cache.d.cts +3 -2
  61. package/dist/shared/meta-cache.d.mts +3 -2
  62. package/dist/shared/meta-cache.mjs +5 -4
  63. package/dist/shared/resolve-component.cjs +27 -0
  64. package/dist/shared/resolve-component.d.cts +22 -0
  65. package/dist/shared/resolve-component.d.mts +22 -0
  66. package/dist/shared/resolve-component.mjs +27 -0
  67. package/dist/shared/stable-key.cjs +28 -0
  68. package/dist/shared/stable-key.d.cts +13 -0
  69. package/dist/shared/stable-key.d.mts +13 -0
  70. package/dist/shared/stable-key.mjs +28 -0
  71. package/dist/shared/to-date.cjs +65 -0
  72. package/dist/shared/to-date.d.cts +35 -0
  73. package/dist/shared/to-date.d.mts +35 -0
  74. package/dist/shared/to-date.mjs +64 -0
  75. package/dist/shared/value-label.cjs +14 -2
  76. package/dist/shared/value-label.d.cts +23 -6
  77. package/dist/shared/value-label.d.mts +23 -6
  78. package/dist/shared/value-label.mjs +14 -3
  79. package/dist/table/create-table-def.cjs +53 -13
  80. package/dist/table/create-table-def.mjs +53 -15
  81. package/dist/table/types.d.cts +15 -13
  82. package/dist/table/types.d.mts +15 -13
  83. package/dist/value-help/client-store.cjs +43 -0
  84. package/dist/value-help/client-store.d.mts +1 -0
  85. package/dist/value-help/client-store.mjs +40 -0
  86. package/dist/value-help/display-value.cjs +30 -0
  87. package/dist/value-help/display-value.d.cts +45 -0
  88. package/dist/value-help/display-value.d.mts +45 -0
  89. package/dist/value-help/display-value.mjs +30 -0
  90. package/dist/value-help/distinct-client.cjs +80 -7
  91. package/dist/value-help/distinct-client.d.cts +31 -36
  92. package/dist/value-help/distinct-client.d.mts +31 -36
  93. package/dist/value-help/distinct-client.mjs +80 -7
  94. package/dist/value-help/extract-ref.cjs +15 -4
  95. package/dist/value-help/extract-ref.d.cts +6 -1
  96. package/dist/value-help/extract-ref.d.mts +6 -1
  97. package/dist/value-help/extract-ref.mjs +16 -6
  98. package/dist/value-help/index.d.mts +6 -3
  99. package/dist/value-help/options.cjs +70 -8
  100. package/dist/value-help/options.d.cts +57 -4
  101. package/dist/value-help/options.d.mts +57 -4
  102. package/dist/value-help/options.mjs +67 -9
  103. package/dist/value-help/resolve-options.cjs +9 -7
  104. package/dist/value-help/resolve-options.d.cts +8 -9
  105. package/dist/value-help/resolve-options.d.mts +8 -9
  106. package/dist/value-help/resolve-options.mjs +9 -7
  107. package/dist/value-help/resolve.cjs +29 -8
  108. package/dist/value-help/resolve.d.cts +22 -5
  109. package/dist/value-help/resolve.d.mts +22 -5
  110. package/dist/value-help/resolve.mjs +29 -8
  111. package/dist/value-help/scope.cjs +3 -7
  112. package/dist/value-help/scope.mjs +3 -7
  113. package/dist/value-help/source.cjs +72 -0
  114. package/dist/value-help/source.d.cts +55 -0
  115. package/dist/value-help/source.d.mts +55 -0
  116. package/dist/value-help/source.mjs +71 -0
  117. package/dist/value-help/texts.cjs +318 -0
  118. package/dist/value-help/texts.d.cts +53 -0
  119. package/dist/value-help/texts.d.mts +53 -0
  120. package/dist/value-help/texts.mjs +313 -0
  121. package/dist/value-help/types.d.cts +10 -0
  122. package/dist/value-help/types.d.mts +10 -0
  123. package/dist/value-help/value-help-client.cjs +24 -20
  124. package/dist/value-help/value-help-client.d.cts +10 -5
  125. package/dist/value-help/value-help-client.d.mts +10 -5
  126. package/dist/value-help/value-help-client.mjs +22 -18
  127. package/package.json +10 -10
@@ -1,3 +1,4 @@
1
+ import { mergeFilters } from "../shared/merge-filters.mjs";
1
2
  //#region src/value-help/scope.ts
2
3
  /**
3
4
  * A host value the picker can filter by: one scalar. Empty (`undefined`,
@@ -24,14 +25,9 @@ function valueHelpScope(info, read) {
24
25
  conditions.push({ [target]: value });
25
26
  if (!pinned.includes(target)) pinned.push(target);
26
27
  }
27
- if (conditions.length === 0) return {
28
- ...info.filter && { filter: info.filter },
29
- pinned
30
- };
31
- const base = info.filter;
32
- const all = base ? [...Array.isArray(base.$and) ? base.$and : [info.filter], ...conditions] : conditions;
28
+ const filter = mergeFilters(info.filter, ...conditions);
33
29
  return {
34
- filter: all.length === 1 ? all[0] : { $and: all },
30
+ ...filter && { filter },
35
31
  pinned
36
32
  };
37
33
  }
@@ -0,0 +1,72 @@
1
+ const require_dict_paths = require("./dict-paths.cjs");
2
+ const require_client_store = require("./client-store.cjs");
3
+ const require_meta_cache = require("../shared/meta-cache.cjs");
4
+ const require_resolve = require("./resolve.cjs");
5
+ const require_texts = require("./texts.cjs");
6
+ const require_value_help_client = require("./value-help-client.cjs");
7
+ //#region src/value-help/source.ts
8
+ /** Per meta-cache entry: the source of each binding object. */
9
+ const sources = /* @__PURE__ */ new WeakMap();
10
+ /**
11
+ * The dictionary of the binding `info` (since 0.1.207): the meta-cache entry
12
+ * of its URL in the UI locale of `options.texts` (one `/meta` per session), a
13
+ * client over it, and the dictionary resolved for the binding. Call it in the
14
+ * browser only: a server render reads no dictionary.
15
+ */
16
+ function valueHelpSource(info, options) {
17
+ const entry = require_meta_cache.getMetaEntry(info.url, options?.clientFactory, { texts: options?.texts });
18
+ let byInfo = sources.get(entry);
19
+ if (!byInfo) sources.set(entry, byInfo = /* @__PURE__ */ new WeakMap());
20
+ let source = byInfo.get(info);
21
+ if (!source) {
22
+ const client = new require_value_help_client.ValueHelpClient(entry.client);
23
+ /** Known once resolved: a search then goes out at once. */
24
+ let resolved;
25
+ const resolve = () => require_resolve.resolveValueHelp(info.url, {
26
+ ...options,
27
+ info
28
+ }).then((r) => resolved = r);
29
+ source = {
30
+ entry,
31
+ resolve,
32
+ search: (opts) => {
33
+ const store = require_client_store.clientStore(entry.client);
34
+ const read = (r) => client.search(r, {
35
+ ...opts,
36
+ info
37
+ }).then((result) => {
38
+ require_texts.feedValueHelpTexts(store, info, r, result.items);
39
+ return result;
40
+ });
41
+ return resolved ? read(resolved) : resolve().then(read);
42
+ },
43
+ pages: async (query, page, size) => {
44
+ const store = require_client_store.clientStore(entry.client);
45
+ const result = await entry.client.pages(query, page, size);
46
+ resolve().then((r) => require_texts.feedValueHelpTexts(store, info, r, result.data), () => {});
47
+ return result;
48
+ }
49
+ };
50
+ byInfo.set(info, source);
51
+ }
52
+ return source;
53
+ }
54
+ /**
55
+ * The fields a picker table lists, of the `available` ones, in their order
56
+ * (since 0.1.207) — the one rule of the table's filter dropdown, its dialog and
57
+ * the form's picker dialog: the binding's `@ui.valueHelp.column` fields, in
58
+ * the order it names them; else the dictionary view (`valueHelpDictPaths`)
59
+ * minus the fields the binding's scope pins with `=` (constant in the picker).
60
+ */
61
+ function valueHelpColumns(resolved, info, available) {
62
+ if (info.columns) {
63
+ const byPath = new Map(available.map((c) => [c.path, c]));
64
+ return info.columns.flatMap((path) => byPath.get(path) ?? []);
65
+ }
66
+ const paths = require_dict_paths.valueHelpDictPaths(resolved);
67
+ const pinned = new Set(info.pinned);
68
+ return available.filter((c) => paths.has(c.path) && !pinned.has(c.path));
69
+ }
70
+ //#endregion
71
+ exports.valueHelpColumns = valueHelpColumns;
72
+ exports.valueHelpSource = valueHelpSource;
@@ -0,0 +1,55 @@
1
+ import { ValueHelpInfo } from "./types.cjs";
2
+ import { ClientFactory } from "../client-factory.cjs";
3
+ import { MetaCacheEntry, MetaEntryOptions } from "../shared/meta-cache.cjs";
4
+ import { ResolvedValueHelp } from "./resolve.cjs";
5
+ import { ValueHelpResult, ValueHelpSearchOptions } from "./value-help-client.cjs";
6
+ import { PageResult, Uniquery } from "@atscript/db-client";
7
+
8
+ //#region src/value-help/source.d.ts
9
+ type Row = Record<string, unknown>;
10
+ /**
11
+ * Where a value help reads its dictionary (since 0.1.207): the app's UI texts
12
+ * (their locale picks the `/meta` entry) and the host's client factory (a
13
+ * form's or a table's `clientFactory`; default: the app-wide one).
14
+ */
15
+ type ValueHelpSourceOptions = Pick<MetaEntryOptions, "texts"> & {
16
+ clientFactory?: ClientFactory;
17
+ };
18
+ /**
19
+ * A binding's dictionary (since 0.1.207) — the one entry point of every
20
+ * reader: the pickers (form and table), the dropdown options and the texts of
21
+ * codes. Rows read through it (`search`, `pages`) fill the texts of the
22
+ * dictionary's codes (`valueHelpText`).
23
+ */
24
+ interface ValueHelpSource {
25
+ /** The dictionary's meta-cache entry: hold it while mounted (`retainMetaEntry`). */
26
+ readonly entry: MetaCacheEntry;
27
+ /**
28
+ * The dictionary resolved for the binding (`resolveValueHelp` with its
29
+ * `info`), once per entry and label. Rejects when its `/meta` fails.
30
+ */
31
+ readonly resolve: () => Promise<ResolvedValueHelp>;
32
+ /** A value-help search of the dictionary for the binding (`ValueHelpClient.search`). */
33
+ readonly search: (opts?: Omit<ValueHelpSearchOptions, "info">) => Promise<ValueHelpResult>;
34
+ /** One page of the dictionary, for a picker table (`useTable`'s `queryFn`). */
35
+ readonly pages: (query: Uniquery, page: number, size: number) => Promise<PageResult<Row>>;
36
+ }
37
+ /**
38
+ * The dictionary of the binding `info` (since 0.1.207): the meta-cache entry
39
+ * of its URL in the UI locale of `options.texts` (one `/meta` per session), a
40
+ * client over it, and the dictionary resolved for the binding. Call it in the
41
+ * browser only: a server render reads no dictionary.
42
+ */
43
+ declare function valueHelpSource(info: ValueHelpInfo, options?: ValueHelpSourceOptions): ValueHelpSource;
44
+ /**
45
+ * The fields a picker table lists, of the `available` ones, in their order
46
+ * (since 0.1.207) — the one rule of the table's filter dropdown, its dialog and
47
+ * the form's picker dialog: the binding's `@ui.valueHelp.column` fields, in
48
+ * the order it names them; else the dictionary view (`valueHelpDictPaths`)
49
+ * minus the fields the binding's scope pins with `=` (constant in the picker).
50
+ */
51
+ declare function valueHelpColumns<T extends {
52
+ path: string;
53
+ }>(resolved: ResolvedValueHelp, info: Pick<ValueHelpInfo, "columns" | "pinned">, available: readonly T[]): T[];
54
+ //#endregion
55
+ export { ValueHelpSource, ValueHelpSourceOptions, valueHelpColumns, valueHelpSource };
@@ -0,0 +1,55 @@
1
+ import { ValueHelpInfo } from "./types.mjs";
2
+ import { ClientFactory } from "../client-factory.mjs";
3
+ import { MetaCacheEntry, MetaEntryOptions } from "../shared/meta-cache.mjs";
4
+ import { ResolvedValueHelp } from "./resolve.mjs";
5
+ import { ValueHelpResult, ValueHelpSearchOptions } from "./value-help-client.mjs";
6
+ import { PageResult, Uniquery } from "@atscript/db-client";
7
+
8
+ //#region src/value-help/source.d.ts
9
+ type Row = Record<string, unknown>;
10
+ /**
11
+ * Where a value help reads its dictionary (since 0.1.207): the app's UI texts
12
+ * (their locale picks the `/meta` entry) and the host's client factory (a
13
+ * form's or a table's `clientFactory`; default: the app-wide one).
14
+ */
15
+ type ValueHelpSourceOptions = Pick<MetaEntryOptions, "texts"> & {
16
+ clientFactory?: ClientFactory;
17
+ };
18
+ /**
19
+ * A binding's dictionary (since 0.1.207) — the one entry point of every
20
+ * reader: the pickers (form and table), the dropdown options and the texts of
21
+ * codes. Rows read through it (`search`, `pages`) fill the texts of the
22
+ * dictionary's codes (`valueHelpText`).
23
+ */
24
+ interface ValueHelpSource {
25
+ /** The dictionary's meta-cache entry: hold it while mounted (`retainMetaEntry`). */
26
+ readonly entry: MetaCacheEntry;
27
+ /**
28
+ * The dictionary resolved for the binding (`resolveValueHelp` with its
29
+ * `info`), once per entry and label. Rejects when its `/meta` fails.
30
+ */
31
+ readonly resolve: () => Promise<ResolvedValueHelp>;
32
+ /** A value-help search of the dictionary for the binding (`ValueHelpClient.search`). */
33
+ readonly search: (opts?: Omit<ValueHelpSearchOptions, "info">) => Promise<ValueHelpResult>;
34
+ /** One page of the dictionary, for a picker table (`useTable`'s `queryFn`). */
35
+ readonly pages: (query: Uniquery, page: number, size: number) => Promise<PageResult<Row>>;
36
+ }
37
+ /**
38
+ * The dictionary of the binding `info` (since 0.1.207): the meta-cache entry
39
+ * of its URL in the UI locale of `options.texts` (one `/meta` per session), a
40
+ * client over it, and the dictionary resolved for the binding. Call it in the
41
+ * browser only: a server render reads no dictionary.
42
+ */
43
+ declare function valueHelpSource(info: ValueHelpInfo, options?: ValueHelpSourceOptions): ValueHelpSource;
44
+ /**
45
+ * The fields a picker table lists, of the `available` ones, in their order
46
+ * (since 0.1.207) — the one rule of the table's filter dropdown, its dialog and
47
+ * the form's picker dialog: the binding's `@ui.valueHelp.column` fields, in
48
+ * the order it names them; else the dictionary view (`valueHelpDictPaths`)
49
+ * minus the fields the binding's scope pins with `=` (constant in the picker).
50
+ */
51
+ declare function valueHelpColumns<T extends {
52
+ path: string;
53
+ }>(resolved: ResolvedValueHelp, info: Pick<ValueHelpInfo, "columns" | "pinned">, available: readonly T[]): T[];
54
+ //#endregion
55
+ export { ValueHelpSource, ValueHelpSourceOptions, valueHelpColumns, valueHelpSource };
@@ -0,0 +1,71 @@
1
+ import { valueHelpDictPaths } from "./dict-paths.mjs";
2
+ import { clientStore } from "./client-store.mjs";
3
+ import { getMetaEntry } from "../shared/meta-cache.mjs";
4
+ import { resolveValueHelp } from "./resolve.mjs";
5
+ import { feedValueHelpTexts } from "./texts.mjs";
6
+ import { ValueHelpClient } from "./value-help-client.mjs";
7
+ //#region src/value-help/source.ts
8
+ /** Per meta-cache entry: the source of each binding object. */
9
+ const sources = /* @__PURE__ */ new WeakMap();
10
+ /**
11
+ * The dictionary of the binding `info` (since 0.1.207): the meta-cache entry
12
+ * of its URL in the UI locale of `options.texts` (one `/meta` per session), a
13
+ * client over it, and the dictionary resolved for the binding. Call it in the
14
+ * browser only: a server render reads no dictionary.
15
+ */
16
+ function valueHelpSource(info, options) {
17
+ const entry = getMetaEntry(info.url, options?.clientFactory, { texts: options?.texts });
18
+ let byInfo = sources.get(entry);
19
+ if (!byInfo) sources.set(entry, byInfo = /* @__PURE__ */ new WeakMap());
20
+ let source = byInfo.get(info);
21
+ if (!source) {
22
+ const client = new ValueHelpClient(entry.client);
23
+ /** Known once resolved: a search then goes out at once. */
24
+ let resolved;
25
+ const resolve = () => resolveValueHelp(info.url, {
26
+ ...options,
27
+ info
28
+ }).then((r) => resolved = r);
29
+ source = {
30
+ entry,
31
+ resolve,
32
+ search: (opts) => {
33
+ const store = clientStore(entry.client);
34
+ const read = (r) => client.search(r, {
35
+ ...opts,
36
+ info
37
+ }).then((result) => {
38
+ feedValueHelpTexts(store, info, r, result.items);
39
+ return result;
40
+ });
41
+ return resolved ? read(resolved) : resolve().then(read);
42
+ },
43
+ pages: async (query, page, size) => {
44
+ const store = clientStore(entry.client);
45
+ const result = await entry.client.pages(query, page, size);
46
+ resolve().then((r) => feedValueHelpTexts(store, info, r, result.data), () => {});
47
+ return result;
48
+ }
49
+ };
50
+ byInfo.set(info, source);
51
+ }
52
+ return source;
53
+ }
54
+ /**
55
+ * The fields a picker table lists, of the `available` ones, in their order
56
+ * (since 0.1.207) — the one rule of the table's filter dropdown, its dialog and
57
+ * the form's picker dialog: the binding's `@ui.valueHelp.column` fields, in
58
+ * the order it names them; else the dictionary view (`valueHelpDictPaths`)
59
+ * minus the fields the binding's scope pins with `=` (constant in the picker).
60
+ */
61
+ function valueHelpColumns(resolved, info, available) {
62
+ if (info.columns) {
63
+ const byPath = new Map(available.map((c) => [c.path, c]));
64
+ return info.columns.flatMap((path) => byPath.get(path) ?? []);
65
+ }
66
+ const paths = valueHelpDictPaths(resolved);
67
+ const pinned = new Set(info.pinned);
68
+ return available.filter((c) => paths.has(c.path) && !pinned.has(c.path));
69
+ }
70
+ //#endregion
71
+ export { valueHelpColumns, valueHelpSource };
@@ -0,0 +1,318 @@
1
+ const require_extract_ref = require("./extract-ref.cjs");
2
+ const require_merge_filters = require("../shared/merge-filters.cjs");
3
+ const require_value_label = require("../shared/value-label.cjs");
4
+ const require_client_store = require("./client-store.cjs");
5
+ const require_options = require("./options.cjs");
6
+ const require_source = require("./source.cjs");
7
+ //#region src/value-help/texts.ts
8
+ /** Codes per request: a lookup is a GET, the codes travel in its URL. */
9
+ const LOOKUP_BATCH = 50;
10
+ /** Texts kept per dictionary, binding scope and label field; the least recently read go first. */
11
+ const MAX_TEXTS = 1e4;
12
+ /** A failed lookup (no access, no network) asks again after this long (ms). */
13
+ const RETRY_MS = 3e4;
14
+ /**
15
+ * Per binding (`labelId`): the label its codes show; `null` for none — also
16
+ * when the dictionary's `/meta` failed, so no read asks for it again until
17
+ * the cache is dropped; a promise while resolving.
18
+ */
19
+ let labels = /* @__PURE__ */ new Map();
20
+ /** The sources texts were read through: their `/meta` failing fails their binding's label. */
21
+ const watched = /* @__PURE__ */ new WeakSet();
22
+ const listeners = /* @__PURE__ */ new Set();
23
+ /** What a binding's label depends on: its dictionary and key, its own label, its host scope. */
24
+ function labelId(info) {
25
+ const { label } = info;
26
+ const host = info.hostFilter ? "host" : "";
27
+ return `${require_extract_ref.valueHelpKey(info)}\0${label?.field ?? ""}\0${label?.arrangement ?? ""}\0${host}`;
28
+ }
29
+ /** The texts of the codes `info` binds, in its scope, read through `store`'s client from `field`. */
30
+ function textsOf(store, info, field) {
31
+ const scope = info.filter ? JSON.stringify(info.filter) : "";
32
+ const id = `${info.targetField}\0${field}\0${scope}`;
33
+ let texts = store.texts.get(id);
34
+ if (!texts) {
35
+ texts = {
36
+ client: store.client,
37
+ binding: require_extract_ref.valueHelpKey(info),
38
+ key: info.targetField,
39
+ field,
40
+ filter: info.filter,
41
+ slots: /* @__PURE__ */ new Map(),
42
+ stale: store.stale?.get(id)?.slots
43
+ };
44
+ store.texts.set(id, texts);
45
+ }
46
+ return texts;
47
+ }
48
+ /**
49
+ * {@link textsOf} the client `info`'s source reads with. A dictionary entry
50
+ * made again (evicted, another UI locale) whose `/meta` fails ends the label
51
+ * of every binding read through it: their codes show alone until the next
52
+ * drop, never asked again on each read.
53
+ */
54
+ function textsFor(info, id, label, options) {
55
+ const source = require_source.valueHelpSource(info, options);
56
+ if (!watched.has(source)) {
57
+ watched.add(source);
58
+ const map = labels;
59
+ source.entry.meta.catch(() => map.set(id, null));
60
+ }
61
+ return textsOf(require_client_store.clientStore(source.entry.client), info, label.field);
62
+ }
63
+ /**
64
+ * The label a binding's codes show by key: none for a dictionary keyed by
65
+ * more than one field, nor for a binding scoped by host values
66
+ * (`ValueHelpInfo.hostFilter`) whose key is not the dictionary's single
67
+ * primary key — such a code names no one row a batched lookup can find.
68
+ */
69
+ function codeLabel(resolved, info) {
70
+ const keys = resolved.primaryKeys;
71
+ if (keys.length > 1 || info.hostFilter && keys[0] !== info.targetField) return void 0;
72
+ return resolved.label;
73
+ }
74
+ function notify(binding) {
75
+ for (const listener of listeners) listener(binding);
76
+ }
77
+ /**
78
+ * The label a binding's codes show, resolved once per binding: its
79
+ * `ResolvedValueHelp.label`; `null` without one, after a failed `/meta` read,
80
+ * or when its codes cannot be read by key (`codeLabel`).
81
+ */
82
+ function labelOf(info, id, options) {
83
+ let label = labels.get(id);
84
+ if (label === void 0) {
85
+ const pending = require_source.valueHelpSource(info, options).resolve().then((r) => codeLabel(r, info) ?? null, () => null);
86
+ labels.set(id, label = pending);
87
+ pending.then((resolved) => {
88
+ if (labels.get(id) !== pending) return;
89
+ labels.set(id, resolved);
90
+ if (resolved) notify(require_extract_ref.valueHelpKey(info));
91
+ });
92
+ }
93
+ return label;
94
+ }
95
+ /** Sets the slot of `code` as the most recently read. */
96
+ function touch(slots, code, slot) {
97
+ slots.delete(code);
98
+ slots.set(code, slot);
99
+ }
100
+ /** Drops the least recently read settled texts beyond {@link MAX_TEXTS}. */
101
+ function trim(slots) {
102
+ if (slots.size <= MAX_TEXTS) return;
103
+ for (const [code, slot] of slots) {
104
+ if (!(slot instanceof Promise)) slots.delete(code);
105
+ if (slots.size <= MAX_TEXTS) return;
106
+ }
107
+ }
108
+ /**
109
+ * The texts `rows` give their codes (`key`) from `field`: a code several rows
110
+ * give different texts is ambiguous and gets none (`null`) — never the first
111
+ * row's. A row without the field gives nothing.
112
+ */
113
+ function rowTexts(rows, key, field) {
114
+ const texts = /* @__PURE__ */ new Map();
115
+ for (const row of rows) {
116
+ const code = require_value_label.codeOf(row[key]);
117
+ if (code === void 0 || !(field in row)) continue;
118
+ const text = require_options.valueHelpRowText({ labelField: field }, row).label ?? null;
119
+ const known = texts.get(code);
120
+ texts.set(code, known === void 0 || known === text ? text : null);
121
+ }
122
+ return texts;
123
+ }
124
+ /**
125
+ * Ask for the text of `code`: it joins this tick's batch of `texts`, read
126
+ * once the tick's reads have queued their codes. Nothing while a failed
127
+ * lookup waits to retry. Returns what the code's slot waits on.
128
+ */
129
+ function ask(texts, code, value) {
130
+ if (texts.retry !== void 0) return void 0;
131
+ let batch = texts.batch;
132
+ if (!batch) {
133
+ let settle;
134
+ const done = new Promise((resolve) => settle = resolve);
135
+ const created = {
136
+ values: /* @__PURE__ */ new Map(),
137
+ done,
138
+ settle
139
+ };
140
+ texts.batch = batch = created;
141
+ queueMicrotask(() => {
142
+ texts.batch = void 0;
143
+ lookup(texts, created);
144
+ });
145
+ }
146
+ batch.values.set(code, value);
147
+ texts.slots.set(code, batch.done);
148
+ return batch.done;
149
+ }
150
+ async function lookup(texts, batch) {
151
+ const { client, key, field, filter, slots } = texts;
152
+ const { done } = batch;
153
+ const entries = [...batch.values];
154
+ const select = key === field ? [key] : [key, field];
155
+ let failed = false;
156
+ let landed = false;
157
+ const requests = [];
158
+ for (let i = 0; i < entries.length; i += LOOKUP_BATCH) {
159
+ const chunk = entries.slice(i, i + LOOKUP_BATCH);
160
+ const request = {
161
+ filter: require_merge_filters.mergeFilters(filter, { [key]: { $in: chunk.map(([, value]) => value) } }),
162
+ controls: {
163
+ $select: select,
164
+ $limit: chunk.length * 2
165
+ }
166
+ };
167
+ requests.push(Promise.resolve().then(() => client.query(request)).then((rows) => {
168
+ const found = rowTexts(rows, key, field);
169
+ for (const [code] of chunk) {
170
+ if (slots.get(code) !== done) continue;
171
+ const text = found.get(code) ?? null;
172
+ slots.set(code, text);
173
+ if (text !== null) landed = true;
174
+ }
175
+ }).catch(() => {
176
+ failed = true;
177
+ for (const [code] of chunk) if (slots.get(code) === done) slots.delete(code);
178
+ }));
179
+ }
180
+ await Promise.all(requests);
181
+ if (failed) texts.retry ??= setTimeout(() => {
182
+ texts.retry = void 0;
183
+ notify(texts.binding);
184
+ }, RETRY_MS);
185
+ trim(slots);
186
+ batch.settle();
187
+ if (landed) notify(texts.binding);
188
+ }
189
+ /**
190
+ * The dictionary text of a code bound by `info` (since 0.1.207): its row's
191
+ * value of the binding's label — `@ui.valueHelp.label`, else the
192
+ * dictionary's `@ui.dict.label` — in the binding's scope (its static
193
+ * `@ui.valueHelp` filter), with the arrangement to show it in
194
+ * (`displayValue` puts the two together). `undefined` while it loads, and for
195
+ * good when neither label is declared, the label field is not readable, the
196
+ * dictionary is keyed by more than one field, the binding's filter refers to
197
+ * host values and its key is not the dictionary's single primary key, its
198
+ * `/meta` cannot be read, no row holds the code, several rows hold it with
199
+ * different texts, or its text is empty: the code then shows alone. A failed
200
+ * lookup is asked again after a while.
201
+ *
202
+ * A miss asks for it: the misses of one tick are read in one request per
203
+ * dictionary and scope (`$filter` the key `$in` the codes, AND the scope;
204
+ * `$select` the key and the label field), through the binding's
205
+ * `valueHelpSource` (`options`); the rows the pickers read fill the same
206
+ * cache. `options.peek` reads only the texts already known: no lookup. After a
207
+ * data drop (`invalidateValueHelpCache`) a code keeps its text until read
208
+ * again. {@link onValueHelpTexts} reports arrivals, so a view can read again.
209
+ * In the browser only: a server render shows codes.
210
+ */
211
+ function valueHelpText(info, value, options) {
212
+ const code = require_value_label.codeOf(value);
213
+ if (code === void 0 || typeof window === "undefined") return void 0;
214
+ const id = labelId(info);
215
+ const label = labelOf(info, id, options);
216
+ if (!label || label instanceof Promise) return void 0;
217
+ const texts = textsFor(info, id, label, options);
218
+ const slot = texts.slots.get(code);
219
+ if (slot === void 0 && !options?.peek) ask(texts, code, value);
220
+ const text = slot === void 0 || slot instanceof Promise ? texts.stale?.get(code) : slot;
221
+ if (typeof text !== "string") return void 0;
222
+ if (text === slot) touch(texts.slots, code, slot);
223
+ return {
224
+ text,
225
+ arrangement: label.arrangement
226
+ };
227
+ }
228
+ /**
229
+ * Loads the texts of `values` bound by `info` in one batched read (since
230
+ * 0.1.207): resolves once {@link valueHelpText} knows each of them — before
231
+ * an export writes its rows, or as a table's rows land. Without a code among
232
+ * them it reads nothing, not even the dictionary's `/meta`. Never rejects.
233
+ */
234
+ async function loadValueHelpTexts(info, values, options) {
235
+ if (typeof window === "undefined") return;
236
+ const codes = /* @__PURE__ */ new Map();
237
+ for (const value of values) {
238
+ const code = require_value_label.codeOf(value);
239
+ if (code !== void 0) codes.set(code, value);
240
+ }
241
+ if (codes.size === 0) return;
242
+ const id = labelId(info);
243
+ const label = await labelOf(info, id, options);
244
+ if (!label) return;
245
+ const texts = textsFor(info, id, label, options);
246
+ const waits = /* @__PURE__ */ new Set();
247
+ for (const [code, value] of codes) {
248
+ const slot = texts.slots.get(code);
249
+ const wait = slot === void 0 ? ask(texts, code, value) : slot;
250
+ if (wait instanceof Promise) waits.add(wait);
251
+ }
252
+ await Promise.all(waits);
253
+ }
254
+ /**
255
+ * Run `listener` whenever texts arrive or are dropped, so a view reading
256
+ * {@link valueHelpText} reads again: with the binding whose texts arrived
257
+ * (`valueHelpKey`), with none when the cache was dropped. Returns the
258
+ * unsubscribe function. Since 0.1.207.
259
+ */
260
+ function onValueHelpTexts(listener) {
261
+ listeners.add(listener);
262
+ return () => {
263
+ listeners.delete(listener);
264
+ };
265
+ }
266
+ /**
267
+ * Picker rows fill the cache: the rows of a binding's search or picker page,
268
+ * with its label field, land in the texts of that binding's scope in `store`
269
+ * — the client's store when the read began, so rows read before a data drop
270
+ * land in the stale one.
271
+ *
272
+ * @internal
273
+ */
274
+ function feedValueHelpTexts(store, info, resolved, rows) {
275
+ const label = codeLabel(resolved, info);
276
+ if (!label || !Array.isArray(rows)) return;
277
+ const { slots } = textsOf(store, info, label.field);
278
+ let changed = false;
279
+ for (const [code, text] of rowTexts(rows, info.targetField, label.field)) {
280
+ const prev = slots.get(code);
281
+ if (prev === text) continue;
282
+ touch(slots, code, text);
283
+ if (prev !== void 0) changed = true;
284
+ }
285
+ trim(slots);
286
+ if (changed) notify(require_extract_ref.valueHelpKey(info));
287
+ }
288
+ /**
289
+ * The data of `client`'s dictionary, or of every one, changed
290
+ * (`invalidateValueHelpCache`): its store goes stale (`staleClientStores`:
291
+ * its searches go, its texts show until read again), the labels stay — a
292
+ * binding whose label failed is asked again; mounted views read again.
293
+ *
294
+ * @internal
295
+ */
296
+ function staleValueHelpTexts(client) {
297
+ require_client_store.staleClientStores(client);
298
+ for (const [id, label] of labels) if (label === null) labels.delete(id);
299
+ notify();
300
+ }
301
+ /**
302
+ * A reset (`resetMetaCache`: another viewer): every client's store and every
303
+ * binding's label go; mounted views read again.
304
+ *
305
+ * @internal
306
+ */
307
+ function resetValueHelpTexts() {
308
+ require_client_store.dropClientStores();
309
+ labels = /* @__PURE__ */ new Map();
310
+ notify();
311
+ }
312
+ //#endregion
313
+ exports.feedValueHelpTexts = feedValueHelpTexts;
314
+ exports.loadValueHelpTexts = loadValueHelpTexts;
315
+ exports.onValueHelpTexts = onValueHelpTexts;
316
+ exports.resetValueHelpTexts = resetValueHelpTexts;
317
+ exports.staleValueHelpTexts = staleValueHelpTexts;
318
+ exports.valueHelpText = valueHelpText;
@@ -0,0 +1,53 @@
1
+ import { ValueText } from "../shared/value-label.cjs";
2
+ import { ValueHelpInfo } from "./types.cjs";
3
+ import { ValueHelpSourceOptions } from "./source.cjs";
4
+ import { ClientLike, FilterExpr } from "@atscript/db-client";
5
+
6
+ //#region src/value-help/texts.d.ts
7
+ /** How {@link valueHelpText} reads (since 0.1.207). */
8
+ type ValueHelpTextOptions = ValueHelpSourceOptions & {
9
+ /**
10
+ * Only a text already known — no lookup: for a value still being typed (a
11
+ * draft filter chip). The rows the pickers read are known.
12
+ */
13
+ peek?: boolean;
14
+ };
15
+ /**
16
+ * The dictionary text of a code bound by `info` (since 0.1.207): its row's
17
+ * value of the binding's label — `@ui.valueHelp.label`, else the
18
+ * dictionary's `@ui.dict.label` — in the binding's scope (its static
19
+ * `@ui.valueHelp` filter), with the arrangement to show it in
20
+ * (`displayValue` puts the two together). `undefined` while it loads, and for
21
+ * good when neither label is declared, the label field is not readable, the
22
+ * dictionary is keyed by more than one field, the binding's filter refers to
23
+ * host values and its key is not the dictionary's single primary key, its
24
+ * `/meta` cannot be read, no row holds the code, several rows hold it with
25
+ * different texts, or its text is empty: the code then shows alone. A failed
26
+ * lookup is asked again after a while.
27
+ *
28
+ * A miss asks for it: the misses of one tick are read in one request per
29
+ * dictionary and scope (`$filter` the key `$in` the codes, AND the scope;
30
+ * `$select` the key and the label field), through the binding's
31
+ * `valueHelpSource` (`options`); the rows the pickers read fill the same
32
+ * cache. `options.peek` reads only the texts already known: no lookup. After a
33
+ * data drop (`invalidateValueHelpCache`) a code keeps its text until read
34
+ * again. {@link onValueHelpTexts} reports arrivals, so a view can read again.
35
+ * In the browser only: a server render shows codes.
36
+ */
37
+ declare function valueHelpText(info: ValueHelpInfo, value: unknown, options?: ValueHelpTextOptions): ValueText | undefined;
38
+ /**
39
+ * Loads the texts of `values` bound by `info` in one batched read (since
40
+ * 0.1.207): resolves once {@link valueHelpText} knows each of them — before
41
+ * an export writes its rows, or as a table's rows land. Without a code among
42
+ * them it reads nothing, not even the dictionary's `/meta`. Never rejects.
43
+ */
44
+ declare function loadValueHelpTexts(info: ValueHelpInfo, values: Iterable<unknown>, options?: ValueHelpSourceOptions): Promise<void>;
45
+ /**
46
+ * Run `listener` whenever texts arrive or are dropped, so a view reading
47
+ * {@link valueHelpText} reads again: with the binding whose texts arrived
48
+ * (`valueHelpKey`), with none when the cache was dropped. Returns the
49
+ * unsubscribe function. Since 0.1.207.
50
+ */
51
+ declare function onValueHelpTexts(listener: (binding?: string) => void): () => void;
52
+ //#endregion
53
+ export { ValueHelpTextOptions, loadValueHelpTexts, onValueHelpTexts, valueHelpText };