@orangecharts/wrun-chart-host 0.1.0

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 (198) hide show
  1. package/README.md +72 -0
  2. package/dist/constants/governed-exchanges.constants.d.ts +11 -0
  3. package/dist/constants/governed-exchanges.constants.js +11 -0
  4. package/dist/constants/indicator-sets.constants.d.ts +8 -0
  5. package/dist/constants/indicator-sets.constants.js +8 -0
  6. package/dist/constants/interval.constants.d.ts +34 -0
  7. package/dist/constants/interval.constants.js +121 -0
  8. package/dist/constants/scriptEditor/technicalIndicatorScriptKeys.constants.d.ts +28 -0
  9. package/dist/constants/scriptEditor/technicalIndicatorScriptKeys.constants.js +65 -0
  10. package/dist/constants/tpo-color-schemes.constants.d.ts +30 -0
  11. package/dist/constants/tpo-color-schemes.constants.js +32 -0
  12. package/dist/host-env.d.ts +102 -0
  13. package/dist/host-env.js +26 -0
  14. package/dist/index.d.ts +93 -0
  15. package/dist/index.js +96 -0
  16. package/dist/indicators/controls/tpo-letter.util.d.ts +2 -0
  17. package/dist/indicators/controls/tpo-letter.util.js +13 -0
  18. package/dist/indicators/controls/tpo-session.util.d.ts +99 -0
  19. package/dist/indicators/controls/tpo-session.util.js +389 -0
  20. package/dist/interfaces/kscript-diagnostics.interface.d.ts +64 -0
  21. package/dist/interfaces/kscript-diagnostics.interface.js +1 -0
  22. package/dist/interfaces/om-panel-data.interface.d.ts +103 -0
  23. package/dist/interfaces/om-panel-data.interface.js +7 -0
  24. package/dist/interfaces/wrun-runtime.interface.d.ts +2659 -0
  25. package/dist/interfaces/wrun-runtime.interface.js +55 -0
  26. package/dist/utility/chart/calendar-interval.util.d.ts +24 -0
  27. package/dist/utility/chart/calendar-interval.util.js +81 -0
  28. package/dist/utility/chart/candle-bar-end.util.d.ts +15 -0
  29. package/dist/utility/chart/candle-bar-end.util.js +23 -0
  30. package/dist/utility/chart/cme-realtime.util.d.ts +24 -0
  31. package/dist/utility/chart/cme-realtime.util.js +53 -0
  32. package/dist/utility/chart/interval-ladder.util.d.ts +27 -0
  33. package/dist/utility/chart/interval-ladder.util.js +92 -0
  34. package/dist/utility/chart/kscript-drawing-prep.util.d.ts +40 -0
  35. package/dist/utility/chart/kscript-drawing-prep.util.js +184 -0
  36. package/dist/utility/chart/kscript-drawing-safe-area.util.d.ts +95 -0
  37. package/dist/utility/chart/kscript-drawing-safe-area.util.js +805 -0
  38. package/dist/utility/chart/kscript-legend-entries.util.d.ts +166 -0
  39. package/dist/utility/chart/kscript-legend-entries.util.js +538 -0
  40. package/dist/utility/chart/kscript-output-role.util.d.ts +32 -0
  41. package/dist/utility/chart/kscript-output-role.util.js +215 -0
  42. package/dist/utility/chart/kscript-provenance-keys.util.d.ts +22 -0
  43. package/dist/utility/chart/kscript-provenance-keys.util.js +56 -0
  44. package/dist/utility/chart/kscript-run-result-normalize.util.d.ts +53 -0
  45. package/dist/utility/chart/kscript-run-result-normalize.util.js +476 -0
  46. package/dist/utility/chart/kscript-run-result-shape.util.d.ts +14 -0
  47. package/dist/utility/chart/kscript-run-result-shape.util.js +66 -0
  48. package/dist/utility/chart/kscript-titancharts-container.util.d.ts +29 -0
  49. package/dist/utility/chart/kscript-titancharts-container.util.js +151 -0
  50. package/dist/utility/chart/kscript-viewport-overlays.util.d.ts +83 -0
  51. package/dist/utility/chart/kscript-viewport-overlays.util.js +443 -0
  52. package/dist/utility/chart/om-panel-push-style.util.d.ts +78 -0
  53. package/dist/utility/chart/om-panel-push-style.util.js +301 -0
  54. package/dist/utility/chart/om-panel.util.d.ts +166 -0
  55. package/dist/utility/chart/om-panel.util.js +815 -0
  56. package/dist/utility/chart/om-series.util.d.ts +42 -0
  57. package/dist/utility/chart/om-series.util.js +330 -0
  58. package/dist/utility/chart/om-target.util.d.ts +11 -0
  59. package/dist/utility/chart/om-target.util.js +24 -0
  60. package/dist/utility/chart/om-widget.util.d.ts +148 -0
  61. package/dist/utility/chart/om-widget.util.js +413 -0
  62. package/dist/utility/chart/overlay-output-budget.util.d.ts +16 -0
  63. package/dist/utility/chart/overlay-output-budget.util.js +109 -0
  64. package/dist/utility/chart/overlay-paint-flags.util.d.ts +24 -0
  65. package/dist/utility/chart/overlay-paint-flags.util.js +55 -0
  66. package/dist/utility/chart/profile-row-height.util.d.ts +21 -0
  67. package/dist/utility/chart/profile-row-height.util.js +110 -0
  68. package/dist/utility/chart/session-interval.util.d.ts +1 -0
  69. package/dist/utility/chart/session-interval.util.js +35 -0
  70. package/dist/utility/chart/technical-script-legend-value.util.d.ts +18 -0
  71. package/dist/utility/chart/technical-script-legend-value.util.js +53 -0
  72. package/dist/utility/chart/trading-day.util.d.ts +70 -0
  73. package/dist/utility/chart/trading-day.util.js +238 -0
  74. package/dist/utility/chart/wrun-legend-name.util.d.ts +25 -0
  75. package/dist/utility/chart/wrun-legend-name.util.js +87 -0
  76. package/dist/utility/chart/wrun-run-result.util.d.ts +977 -0
  77. package/dist/utility/chart/wrun-run-result.util.js +5179 -0
  78. package/dist/utility/chart/wrun-visibility-chart.util.d.ts +25 -0
  79. package/dist/utility/chart/wrun-visibility-chart.util.js +22 -0
  80. package/dist/utility/indicators/heatmap-sensitivity.util.d.ts +62 -0
  81. package/dist/utility/indicators/heatmap-sensitivity.util.js +82 -0
  82. package/dist/utility/number-format.util.d.ts +68 -0
  83. package/dist/utility/number-format.util.js +188 -0
  84. package/dist/utility/v2/opra-governed.util.d.ts +1 -0
  85. package/dist/utility/v2/opra-governed.util.js +13 -0
  86. package/dist/utility/wrun/wrun-auto-row.util.d.ts +54 -0
  87. package/dist/utility/wrun/wrun-auto-row.util.js +96 -0
  88. package/dist/utility/wrun/wrun-batched-marks.util.d.ts +341 -0
  89. package/dist/utility/wrun/wrun-batched-marks.util.js +938 -0
  90. package/dist/utility/wrun/wrun-batched-readout.util.d.ts +75 -0
  91. package/dist/utility/wrun/wrun-batched-readout.util.js +171 -0
  92. package/dist/utility/wrun/wrun-box-segment.util.d.ts +185 -0
  93. package/dist/utility/wrun/wrun-box-segment.util.js +570 -0
  94. package/dist/utility/wrun/wrun-build-memo.util.d.ts +133 -0
  95. package/dist/utility/wrun/wrun-build-memo.util.js +301 -0
  96. package/dist/utility/wrun/wrun-builtin-twin-ports.util.d.ts +25 -0
  97. package/dist/utility/wrun/wrun-builtin-twin-ports.util.js +75 -0
  98. package/dist/utility/wrun/wrun-builtin-twin.util.d.ts +9 -0
  99. package/dist/utility/wrun/wrun-builtin-twin.util.js +40 -0
  100. package/dist/utility/wrun/wrun-canvas-cache.util.d.ts +28 -0
  101. package/dist/utility/wrun/wrun-canvas-cache.util.js +56 -0
  102. package/dist/utility/wrun/wrun-canvas-readout.util.d.ts +69 -0
  103. package/dist/utility/wrun/wrun-canvas-readout.util.js +53 -0
  104. package/dist/utility/wrun/wrun-canvases.util.d.ts +295 -0
  105. package/dist/utility/wrun/wrun-canvases.util.js +1432 -0
  106. package/dist/utility/wrun/wrun-cap-notice.util.d.ts +77 -0
  107. package/dist/utility/wrun/wrun-cap-notice.util.js +296 -0
  108. package/dist/utility/wrun/wrun-card-style.util.d.ts +154 -0
  109. package/dist/utility/wrun/wrun-card-style.util.js +539 -0
  110. package/dist/utility/wrun/wrun-chart-context.util.d.ts +89 -0
  111. package/dist/utility/wrun/wrun-chart-context.util.js +213 -0
  112. package/dist/utility/wrun/wrun-chart-names.util.d.ts +23 -0
  113. package/dist/utility/wrun/wrun-chart-names.util.js +33 -0
  114. package/dist/utility/wrun/wrun-chart-time.util.d.ts +12 -0
  115. package/dist/utility/wrun/wrun-chart-time.util.js +23 -0
  116. package/dist/utility/wrun/wrun-child-overlay-id.util.d.ts +13 -0
  117. package/dist/utility/wrun/wrun-child-overlay-id.util.js +44 -0
  118. package/dist/utility/wrun/wrun-color-facts.util.d.ts +40 -0
  119. package/dist/utility/wrun/wrun-color-facts.util.js +88 -0
  120. package/dist/utility/wrun/wrun-color-settings.lazy.d.ts +26 -0
  121. package/dist/utility/wrun/wrun-color-settings.lazy.js +9 -0
  122. package/dist/utility/wrun/wrun-color-settings.util.d.ts +351 -0
  123. package/dist/utility/wrun/wrun-color-settings.util.js +1363 -0
  124. package/dist/utility/wrun/wrun-footprint-modes.util.d.ts +23 -0
  125. package/dist/utility/wrun/wrun-footprint-modes.util.js +24 -0
  126. package/dist/utility/wrun/wrun-frames.d.ts +217 -0
  127. package/dist/utility/wrun/wrun-frames.js +1021 -0
  128. package/dist/utility/wrun/wrun-handles.util.d.ts +792 -0
  129. package/dist/utility/wrun/wrun-handles.util.js +2050 -0
  130. package/dist/utility/wrun/wrun-heatmap-cell-budget.util.d.ts +156 -0
  131. package/dist/utility/wrun/wrun-heatmap-cell-budget.util.js +342 -0
  132. package/dist/utility/wrun/wrun-hosted-wire-caps.d.ts +218 -0
  133. package/dist/utility/wrun/wrun-hosted-wire-caps.js +667 -0
  134. package/dist/utility/wrun/wrun-hosted-wire.util.d.ts +40 -0
  135. package/dist/utility/wrun/wrun-hosted-wire.util.js +27 -0
  136. package/dist/utility/wrun/wrun-hover.util.d.ts +182 -0
  137. package/dist/utility/wrun/wrun-hover.util.js +534 -0
  138. package/dist/utility/wrun/wrun-hud-looks.util.d.ts +246 -0
  139. package/dist/utility/wrun/wrun-hud-looks.util.js +587 -0
  140. package/dist/utility/wrun/wrun-integration-budget.util.d.ts +47 -0
  141. package/dist/utility/wrun/wrun-integration-budget.util.js +122 -0
  142. package/dist/utility/wrun/wrun-latest-snapshot.d.ts +19 -0
  143. package/dist/utility/wrun/wrun-latest-snapshot.js +7 -0
  144. package/dist/utility/wrun/wrun-levels-child.util.d.ts +131 -0
  145. package/dist/utility/wrun/wrun-levels-child.util.js +514 -0
  146. package/dist/utility/wrun/wrun-lifecycle.contract.d.ts +75 -0
  147. package/dist/utility/wrun/wrun-lifecycle.contract.js +9 -0
  148. package/dist/utility/wrun/wrun-market-facts.util.d.ts +116 -0
  149. package/dist/utility/wrun/wrun-market-facts.util.js +238 -0
  150. package/dist/utility/wrun/wrun-output-style.util.d.ts +344 -0
  151. package/dist/utility/wrun/wrun-output-style.util.js +1239 -0
  152. package/dist/utility/wrun/wrun-overlay-identity.util.d.ts +84 -0
  153. package/dist/utility/wrun/wrun-overlay-identity.util.js +142 -0
  154. package/dist/utility/wrun/wrun-overlay-result.util.d.ts +120 -0
  155. package/dist/utility/wrun/wrun-overlay-result.util.js +219 -0
  156. package/dist/utility/wrun/wrun-pack.util.d.ts +70 -0
  157. package/dist/utility/wrun/wrun-pack.util.js +301 -0
  158. package/dist/utility/wrun/wrun-pane-format.util.d.ts +180 -0
  159. package/dist/utility/wrun/wrun-pane-format.util.js +299 -0
  160. package/dist/utility/wrun/wrun-panel-style.lazy.d.ts +25 -0
  161. package/dist/utility/wrun/wrun-panel-style.lazy.js +15 -0
  162. package/dist/utility/wrun/wrun-panel-style.util.d.ts +174 -0
  163. package/dist/utility/wrun/wrun-panel-style.util.js +726 -0
  164. package/dist/utility/wrun/wrun-param-values.util.d.ts +69 -0
  165. package/dist/utility/wrun/wrun-param-values.util.js +230 -0
  166. package/dist/utility/wrun/wrun-refusal.util.d.ts +263 -0
  167. package/dist/utility/wrun/wrun-refusal.util.js +1259 -0
  168. package/dist/utility/wrun/wrun-registry-errors.util.d.ts +199 -0
  169. package/dist/utility/wrun/wrun-registry-errors.util.js +257 -0
  170. package/dist/utility/wrun/wrun-render-select.util.d.ts +336 -0
  171. package/dist/utility/wrun/wrun-render-select.util.js +1157 -0
  172. package/dist/utility/wrun/wrun-script-inputs.util.d.ts +434 -0
  173. package/dist/utility/wrun/wrun-script-inputs.util.js +1971 -0
  174. package/dist/utility/wrun/wrun-setting-words.lazy.d.ts +20 -0
  175. package/dist/utility/wrun/wrun-setting-words.lazy.js +9 -0
  176. package/dist/utility/wrun/wrun-setting-words.util.d.ts +79 -0
  177. package/dist/utility/wrun/wrun-setting-words.util.js +176 -0
  178. package/dist/utility/wrun/wrun-socket-row-store.d.ts +98 -0
  179. package/dist/utility/wrun/wrun-socket-row-store.js +417 -0
  180. package/dist/utility/wrun/wrun-style-flags.util.d.ts +18 -0
  181. package/dist/utility/wrun/wrun-style-flags.util.js +21 -0
  182. package/dist/utility/wrun/wrun-style-words.util.d.ts +148 -0
  183. package/dist/utility/wrun/wrun-style-words.util.js +439 -0
  184. package/dist/utility/wrun/wrun-table-look.util.d.ts +125 -0
  185. package/dist/utility/wrun/wrun-table-look.util.js +225 -0
  186. package/dist/utility/wrun/wrun-text-look.util.d.ts +51 -0
  187. package/dist/utility/wrun/wrun-text-look.util.js +227 -0
  188. package/dist/utility/wrun/wrun-text-params.util.d.ts +106 -0
  189. package/dist/utility/wrun/wrun-text-params.util.js +171 -0
  190. package/dist/utility/wrun/wrun-tpo-rows.util.d.ts +71 -0
  191. package/dist/utility/wrun/wrun-tpo-rows.util.js +438 -0
  192. package/dist/utility/wrun/wrun-twin-compute-inputs.util.d.ts +14 -0
  193. package/dist/utility/wrun/wrun-twin-compute-inputs.util.js +58 -0
  194. package/dist/utility/wrun/wrun-visibility-table.d.ts +437 -0
  195. package/dist/utility/wrun/wrun-visibility-table.js +237 -0
  196. package/dist/utility/wrun/wrun-volume-profile.util.d.ts +37 -0
  197. package/dist/utility/wrun/wrun-volume-profile.util.js +278 -0
  198. package/package.json +38 -0
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Browser WRUN runtime wire types: the packed request/response protocol
3
+ * between the main-thread host (`wrun-host.service.ts`) and the sandbox
4
+ * worker (`wrun-engine.worker.ts`), plus the parsed package metadata sheet.
5
+ *
6
+ * The protocol mirrors the om daemon's packed worker transport
7
+ * (openmarket docs/WRUN2_ABI.md, "Row transport and the packed worker
8
+ * protocol") so a package behaves identically under both hosts:
9
+ * - Request: `ts` (Float64Array), `argCounts` (Int32Array, each row's
10
+ * ORIGINAL argument count), `values` (Float64Array, row-major at a fixed
11
+ * stride), `cellIndexes` (declared celled slots), `cellLens`/`cellOffsets`
12
+ * (Int32Array, row-major over the celled slots, length -1 = missing
13
+ * block), `cells` (every block laid end to end), and `stringMaxBytes`
14
+ * when string slots are declared.
15
+ * - Response: `ready` (Uint8Array) + `outputs` (Float64Array, row-major at
16
+ * the output count), plus the packed string table when slots are declared.
17
+ * - Every buffer is transferred, never structured-cloned; the compiled
18
+ * `WebAssembly.Module` itself IS structured-cloned (browser spec allows
19
+ * sharing compiled modules across agents in one cluster).
20
+ */
21
+ const WRUN_ABI_ORDER = {
22
+ 'wrun-1': 1,
23
+ 'wrun-2': 2,
24
+ 'wrun-3': 3,
25
+ 'wrun-4': 4,
26
+ 'wrun-5': 5,
27
+ 'wrun-6': 6,
28
+ };
29
+ export function abiAtLeast(abi, floor) {
30
+ return WRUN_ABI_ORDER[abi] >= WRUN_ABI_ORDER[floor];
31
+ }
32
+ /** A live rows block was refused BEFORE any evaluation because its rows run
33
+ * backwards relative to the session watermark (or each other). The guest
34
+ * state is untouched: the session stays valid, so the worker answers a
35
+ * coded error without dropping it, and only genuine evaluation throws
36
+ * poison the session. Shared by the worker (thrown by the ABI host) and the
37
+ * main-thread host (rejected from a `code: 'live-order'` response). */
38
+ export class WrunLiveOrderError extends Error {
39
+ constructor(message) {
40
+ super(message);
41
+ this.name = 'WrunLiveOrderError';
42
+ }
43
+ }
44
+ const liveBlockStatsByRows = new WeakMap();
45
+ /** The live block's restore/snapshot accounting rides beside the rows it
46
+ * belongs to (like the handle set and the strategy output), keeping each
47
+ * transported row byte-compatible. Kept with the wire types so every
48
+ * reader (the host's unpack, the run pass, the tick) shares one registry
49
+ * whatever else a spec mocks. */
50
+ export function attachWrunLive(rows, live) {
51
+ liveBlockStatsByRows.set(rows, live);
52
+ }
53
+ export function wrunLiveOf(rows) {
54
+ return liveBlockStatsByRows.get(rows);
55
+ }
@@ -0,0 +1,24 @@
1
+ /** Months per bar of each calendar label. */
2
+ export declare const CALENDAR_INTERVAL_MONTHS: Readonly<Record<string, number>>;
3
+ /** Months per bar for a calendar length, null for a fixed-length interval. */
4
+ export declare function calendarMonthsOfMs(intervalMs: number): number | null;
5
+ export declare function isCalendarIntervalMs(intervalMs: number): boolean;
6
+ export declare function isCalendarIntervalLabel(label: unknown): boolean;
7
+ /** The UTC start of the `months`-month bar holding `tsMs`, months counted from January. */
8
+ export declare function calendarBarStartMs(tsMs: number, months: number): number;
9
+ /** The start of the bar holding `tsMs`: the calendar boundary at a calendar length, else the fixed UTC floor. */
10
+ export declare function barStartMs(tsMs: number, intervalMs: number): number;
11
+ /** When the bar opening at `startMs` closes: the next calendar boundary at a calendar length, else start + length. */
12
+ export declare function barEndMs(startMs: number, intervalMs: number): number;
13
+ /**
14
+ * The open of the bar before the one opening at `startMs`: one calendar bar back from its calendar
15
+ * start at a calendar length, else `startMs - intervalMs`. A fixed length never re-floors `startMs`
16
+ * on the epoch grid, so a Monday week or a 13:30 session lattice keeps its own phase.
17
+ */
18
+ export declare function prevBarStartMs(startMs: number, intervalMs: number): number;
19
+ /**
20
+ * The longest bar an interval can hold: 31, 92, 184 or 366 days at a calendar length, else the
21
+ * length itself. It sizes a whole bar's worth of finer bars (a 31-day month holds 744 hourly
22
+ * candles where the average month's 30.4375 days counts 731); a bar's close is `barEndMs`.
23
+ */
24
+ export declare function calendarLongestBarMs(intervalMs: number): number;
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Calendar bars (1M 3M 6M 1Y): where one starts and where it ends.
3
+ *
4
+ * The gateway buckets them on UTC calendar boundaries (kiyotaka-go
5
+ * GetIntervalStart, tsdb-gateway !2190): a month on the 1st at 00:00, quarters
6
+ * in January, April, July and October, halves in January and July, years on
7
+ * January 1. Their length varies (28 to 31 days a month), so the fixed floor
8
+ * `ts - ts % ms` on the chart's average-month length lands mid-month and splits
9
+ * the forming bar. Every writer that floors a timestamp onto a bar, or asks
10
+ * when a bar closes, goes through `barStartMs` / `barEndMs`; one that steps a
11
+ * bar back goes through `prevBarStartMs`, and one that sizes a whole bar's worth
12
+ * of finer bars through `calendarLongestBarMs`.
13
+ *
14
+ * Keyed on the exact lengths Intervals.map_unit gives the calendar labels; any
15
+ * other length (the legacy 28-day '1mm' included) is fixed-length. A leaf:
16
+ * workers and live writers read it.
17
+ */
18
+ import { candleBarEndMs } from './candle-bar-end.util.js';
19
+ const AVERAGE_MONTH_MS = 2_629_800_000;
20
+ const DAY_MS = 86_400_000;
21
+ const MONTHS_BY_LENGTH = new Map([
22
+ [AVERAGE_MONTH_MS, 1],
23
+ [3 * AVERAGE_MONTH_MS, 3],
24
+ [6 * AVERAGE_MONTH_MS, 6],
25
+ [12 * AVERAGE_MONTH_MS, 12],
26
+ ]);
27
+ /** The longest bar of each calendar length: a 31-day month, July to September (or October to
28
+ * December), July to December, a leap year. */
29
+ const LONGEST_BAR_MS = new Map([
30
+ [AVERAGE_MONTH_MS, 31 * DAY_MS],
31
+ [3 * AVERAGE_MONTH_MS, 92 * DAY_MS],
32
+ [6 * AVERAGE_MONTH_MS, 184 * DAY_MS],
33
+ [12 * AVERAGE_MONTH_MS, 366 * DAY_MS],
34
+ ]);
35
+ /** Months per bar of each calendar label. */
36
+ export const CALENDAR_INTERVAL_MONTHS = { '1M': 1, '3M': 3, '6M': 6, '1Y': 12 };
37
+ /** Months per bar for a calendar length, null for a fixed-length interval. */
38
+ export function calendarMonthsOfMs(intervalMs) {
39
+ return MONTHS_BY_LENGTH.get(intervalMs) ?? null;
40
+ }
41
+ export function isCalendarIntervalMs(intervalMs) {
42
+ return MONTHS_BY_LENGTH.has(intervalMs);
43
+ }
44
+ export function isCalendarIntervalLabel(label) {
45
+ return typeof label === 'string' && Object.prototype.hasOwnProperty.call(CALENDAR_INTERVAL_MONTHS, label);
46
+ }
47
+ /** The UTC start of the `months`-month bar holding `tsMs`, months counted from January. */
48
+ export function calendarBarStartMs(tsMs, months) {
49
+ const d = new Date(tsMs);
50
+ const month = d.getUTCMonth();
51
+ return Date.UTC(d.getUTCFullYear(), month - (month % months), 1);
52
+ }
53
+ /** The start of the bar holding `tsMs`: the calendar boundary at a calendar length, else the fixed UTC floor. */
54
+ export function barStartMs(tsMs, intervalMs) {
55
+ const months = calendarMonthsOfMs(intervalMs);
56
+ return months === null ? tsMs - (tsMs % intervalMs) : calendarBarStartMs(tsMs, months);
57
+ }
58
+ /** When the bar opening at `startMs` closes: the next calendar boundary at a calendar length, else start + length. */
59
+ export function barEndMs(startMs, intervalMs) {
60
+ return isCalendarIntervalMs(intervalMs) ? candleBarEndMs(startMs, intervalMs) : startMs + intervalMs;
61
+ }
62
+ /**
63
+ * The open of the bar before the one opening at `startMs`: one calendar bar back from its calendar
64
+ * start at a calendar length, else `startMs - intervalMs`. A fixed length never re-floors `startMs`
65
+ * on the epoch grid, so a Monday week or a 13:30 session lattice keeps its own phase.
66
+ */
67
+ export function prevBarStartMs(startMs, intervalMs) {
68
+ const months = calendarMonthsOfMs(intervalMs);
69
+ if (months === null)
70
+ return startMs - intervalMs;
71
+ const start = new Date(calendarBarStartMs(startMs, months));
72
+ return Date.UTC(start.getUTCFullYear(), start.getUTCMonth() - months, 1);
73
+ }
74
+ /**
75
+ * The longest bar an interval can hold: 31, 92, 184 or 366 days at a calendar length, else the
76
+ * length itself. It sizes a whole bar's worth of finer bars (a 31-day month holds 744 hourly
77
+ * candles where the average month's 30.4375 days counts 731); a bar's close is `barEndMs`.
78
+ */
79
+ export function calendarLongestBarMs(intervalMs) {
80
+ return LONGEST_BAR_MS.get(intervalMs) ?? intervalMs;
81
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * When a chart bar closes: the calendar rule month, quarter and year bars need.
3
+ *
4
+ * A leaf with no imports, so a light module can read the rule without the store that first
5
+ * held it: `candle-side-split.util` (the resident side split, which re-exports it) and the wrun
6
+ * daily-lane helper (`wrun-daily-lane.util`, on the row builder's import graph) share this one
7
+ * function.
8
+ */
9
+ /**
10
+ * When the bar that opens at `tsMs` closes. Sub-month grids are fixed-length;
11
+ * month, quarter and year bars follow the calendar (a 31-day month outlives
12
+ * the average-month `intervalMs` the chart carries, and a 28 or 30 day month
13
+ * ends before it).
14
+ */
15
+ export declare function candleBarEndMs(tsMs: number, intervalMs: number): number;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * When a chart bar closes: the calendar rule month, quarter and year bars need.
3
+ *
4
+ * A leaf with no imports, so a light module can read the rule without the store that first
5
+ * held it: `candle-side-split.util` (the resident side split, which re-exports it) and the wrun
6
+ * daily-lane helper (`wrun-daily-lane.util`, on the row builder's import graph) share this one
7
+ * function.
8
+ */
9
+ const MONTH_CLASS_MS = 28 * 86_400_000;
10
+ const AVERAGE_MONTH_MS = 2_629_800_000;
11
+ /**
12
+ * When the bar that opens at `tsMs` closes. Sub-month grids are fixed-length;
13
+ * month, quarter and year bars follow the calendar (a 31-day month outlives
14
+ * the average-month `intervalMs` the chart carries, and a 28 or 30 day month
15
+ * ends before it).
16
+ */
17
+ export function candleBarEndMs(tsMs, intervalMs) {
18
+ if (!(intervalMs >= MONTH_CLASS_MS))
19
+ return tsMs + intervalMs;
20
+ const months = Math.max(1, Math.round(intervalMs / AVERAGE_MONTH_MS));
21
+ const d = new Date(tsMs);
22
+ return Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + months, d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds());
23
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * CME-governed symbols stream with a fixed delay unless the account has the
3
+ * real-time add-on; exchange rules require disclosing that state wherever a
4
+ * governed price renders. Browser-safe AND worker-safe: zero Vue / store /
5
+ * i18n imports (the capability boundary in workers consults the governed set).
6
+ */
7
+ export declare const CME_DELAY_MINUTES = 10;
8
+ /**
9
+ * Venue codes reach the policy helpers from persisted overlay settings, which
10
+ * are user data: an `exchange` field can arrive as an object or a number from
11
+ * an older or foreign writer. Anything but a non-empty string means "no
12
+ * venue", never a throw (an uncaught `.toUpperCase` on an object aborted the
13
+ * whole indicator poll cycle for that chart). Returns the UPPERCASE code.
14
+ */
15
+ export declare function normalizeExchangeCode(exchange: unknown): string | null;
16
+ export declare function isCmeGoverned(exchange: string | undefined | null): boolean;
17
+ type TranslateFn = (key: string) => string;
18
+ /**
19
+ * Localized full exchange name for delay copy, falling back to the raw code.
20
+ * Takes the caller's `t` so this module stays worker-safe; every key is a
21
+ * full literal (no dynamic key construction).
22
+ */
23
+ export declare function getExchangeFullName(exchange: string | undefined | null, t: TranslateFn): string;
24
+ export {};
@@ -0,0 +1,53 @@
1
+ /**
2
+ * CME-governed symbols stream with a fixed delay unless the account has the
3
+ * real-time add-on; exchange rules require disclosing that state wherever a
4
+ * governed price renders. Browser-safe AND worker-safe: zero Vue / store /
5
+ * i18n imports (the capability boundary in workers consults the governed set).
6
+ */
7
+ import { CME_GOVERNED_EXCHANGES } from '../../constants/governed-exchanges.constants.js';
8
+ // Governed set derived from the one exchange catalog (src/interfaces/cme-realtime.interface.ts; the package's byte
9
+ // copy is pinned equal to it by tests/packages/wrun-chart-host/narrowed-constants.spec.ts).
10
+ // Codes use the UPPERCASE_UNDERSCORE convention the backend gRPC layer sends
11
+ // (see marketKind.util.ts).
12
+ const CME_GOVERNED_EXCHANGE_SET = new Set(CME_GOVERNED_EXCHANGES);
13
+ export const CME_DELAY_MINUTES = 10;
14
+ /**
15
+ * Venue codes reach the policy helpers from persisted overlay settings, which
16
+ * are user data: an `exchange` field can arrive as an object or a number from
17
+ * an older or foreign writer. Anything but a non-empty string means "no
18
+ * venue", never a throw (an uncaught `.toUpperCase` on an object aborted the
19
+ * whole indicator poll cycle for that chart). Returns the UPPERCASE code.
20
+ */
21
+ export function normalizeExchangeCode(exchange) {
22
+ return typeof exchange === 'string' && exchange.length > 0 ? exchange.toUpperCase() : null;
23
+ }
24
+ export function isCmeGoverned(exchange) {
25
+ const code = normalizeExchangeCode(exchange);
26
+ return code !== null && CME_GOVERNED_EXCHANGE_SET.has(code);
27
+ }
28
+ /**
29
+ * Localized full exchange name for delay copy, falling back to the raw code.
30
+ * Takes the caller's `t` so this module stays worker-safe; every key is a
31
+ * full literal (no dynamic key construction).
32
+ */
33
+ export function getExchangeFullName(exchange, t) {
34
+ const code = normalizeExchangeCode(exchange);
35
+ if (!code)
36
+ return '';
37
+ switch (code) {
38
+ case 'CME':
39
+ return t('cmeRealtimeData.exchanges.cme');
40
+ case 'CBOT':
41
+ return t('cmeRealtimeData.exchanges.cbot');
42
+ case 'COMEX':
43
+ return t('cmeRealtimeData.exchanges.comex');
44
+ case 'NYMEX':
45
+ return t('cmeRealtimeData.exchanges.nymex');
46
+ case 'GLOBEX':
47
+ return t('cmeRealtimeData.exchanges.globex');
48
+ case 'OPRA':
49
+ return t('cmeRealtimeData.exchanges.opra');
50
+ default:
51
+ return exchange;
52
+ }
53
+ }
@@ -0,0 +1,27 @@
1
+ declare const STANDARD_INTERVAL_LABELS: readonly ["1m", "5m", "15m", "1h", "4h", "1d", "1w", "1s", "30m", "1M", "3M", "1Y"];
2
+ export type StandardInterval = (typeof STANDARD_INTERVAL_LABELS)[number];
3
+ declare const CUSTOM_INTERVAL_LABELS: readonly ["2m", "3m", "10m", "45m", "2h", "6h", "8h", "12h", "3d"];
4
+ export type CustomInterval = (typeof CUSTOM_INTERVAL_LABELS)[number];
5
+ export declare const CUSTOM_INTERVALS: readonly CustomInterval[];
6
+ export declare function isCustomInterval(label: unknown): label is CustomInterval;
7
+ declare const EXTENDED_INTERVAL_LABELS: readonly ["5s", "15s", "30s", "6M"];
8
+ export type ExtendedInterval = (typeof EXTENDED_INTERVAL_LABELS)[number];
9
+ export declare const EXTENDED_INTERVALS: readonly ExtendedInterval[];
10
+ export declare function isExtendedInterval(label: unknown): label is ExtendedInterval;
11
+ /** Every label the resolver knows, in the wire map's order: the classic ladder, the presets, the extended rungs. */
12
+ export declare const RESOLVED_INTERVAL_LABELS: readonly string[];
13
+ export declare function intervalLabelForMs(ms: unknown): string | undefined;
14
+ export declare function intervalMs(label: unknown): number | undefined;
15
+ /**
16
+ * The standard label whose per-interval settings a label uses. Tuning tables
17
+ * (profile rows, footprint bars, heatmap depth, restrictions...) list only
18
+ * the classic ladder, so a preset or an extended rung takes the largest
19
+ * classic label that is no longer than it (5s 15s 30s -> 1s, 3m -> 1m,
20
+ * 10m -> 5m, 45m -> 30m, 6h -> 4h, 12h -> 4h, 3d -> 1d, 6M -> 3M). A classic
21
+ * or unknown label comes back unchanged. Never use this for a wire interval:
22
+ * a chart must not be served another interval's bars.
23
+ */
24
+ export declare function standardIntervalAtOrBelow(label: string): string;
25
+ /** `standardIntervalAtOrBelow` for a millisecond length: a preset or extended length maps to its standard's length. */
26
+ export declare function standardIntervalMsAtOrBelow(ms: number): number;
27
+ export {};
@@ -0,0 +1,92 @@
1
+ /**
2
+ * The chart's interval labels and their millisecond lengths: the label half of the interval resolver, pure and
3
+ * free of the wire enum. interval-registry.util pairs every label here with its wire interval (`AggregationInterval`)
4
+ * in maps typed by these label unions, so the compiler holds the two lists equal, and re-exports every name here.
5
+ * The wrun result reader reads this half without the gateway's protobuf package: it rides the
6
+ * `@orangecharts/wrun-chart-host` package (docs/features/kscript/wrun/wrun-chart-host.md).
7
+ */
8
+ import { Intervals } from '../../constants/interval.constants.js';
9
+ // The classic labels the wire map lists first, in its order (the reversed length map keeps the last label of a
10
+ // length, so the order is the resolver's).
11
+ const STANDARD_INTERVAL_LABELS = ['1m', '5m', '15m', '1h', '4h', '1d', '1w', '1s', '30m', '1M', '3M', '1Y'];
12
+ // The one declaration of the custom labels (the picker's rolled-up presets), by length. A label also needs its
13
+ // length in Intervals.map_unit and its wire interval in interval-registry.util.
14
+ const CUSTOM_INTERVAL_LABELS = ['2m', '3m', '10m', '45m', '2h', '6h', '8h', '12h', '3d'];
15
+ export const CUSTOM_INTERVALS = [...CUSTOM_INTERVAL_LABELS];
16
+ const CUSTOM_INTERVAL_SET = new Set(CUSTOM_INTERVALS);
17
+ export function isCustomInterval(label) {
18
+ return typeof label === 'string' && CUSTOM_INTERVAL_SET.has(label);
19
+ }
20
+ // The rungs every picker offers beside the classic ladder, with no flag: the
21
+ // gateway rolls them up like the presets (5s 15s 30s from the 1s tier where 1s
22
+ // is served, tsdb-gateway !2189; 6M from the monthly tier, !2190). Like a
23
+ // preset, each needs its length in Intervals.map_unit and has no row in the
24
+ // tuning tables (standardIntervalAtOrBelow).
25
+ const EXTENDED_INTERVAL_LABELS = ['5s', '15s', '30s', '6M'];
26
+ export const EXTENDED_INTERVALS = [...EXTENDED_INTERVAL_LABELS];
27
+ const EXTENDED_INTERVAL_SET = new Set(EXTENDED_INTERVALS);
28
+ export function isExtendedInterval(label) {
29
+ return typeof label === 'string' && EXTENDED_INTERVAL_SET.has(label);
30
+ }
31
+ /** Every label the resolver knows, in the wire map's order: the classic ladder, the presets, the extended rungs. */
32
+ export const RESOLVED_INTERVAL_LABELS = [
33
+ ...STANDARD_INTERVAL_LABELS,
34
+ ...CUSTOM_INTERVAL_LABELS,
35
+ ...EXTENDED_INTERVAL_LABELS,
36
+ ];
37
+ const hasOwn = (record, key) => Object.prototype.hasOwnProperty.call(record, key);
38
+ // map_unit reversed over every label this resolver knows, so a label added
39
+ // to it reads back from its length with no second list.
40
+ const LABEL_BY_MS = Object.fromEntries(RESOLVED_INTERVAL_LABELS.flatMap((label) => {
41
+ const ms = intervalMs(label);
42
+ return ms === undefined ? [] : [[String(ms), label]];
43
+ }));
44
+ export function intervalLabelForMs(ms) {
45
+ if (typeof ms !== 'number' && typeof ms !== 'string')
46
+ return undefined;
47
+ const key = String(ms);
48
+ return hasOwn(LABEL_BY_MS, key) ? LABEL_BY_MS[key] : undefined;
49
+ }
50
+ export function intervalMs(label) {
51
+ if (typeof label !== 'string')
52
+ return undefined;
53
+ const mapUnit = Intervals.map_unit;
54
+ return hasOwn(mapUnit, label) ? mapUnit[label] : undefined;
55
+ }
56
+ // The classic ladder, ascending: the labels the tuning tables were written for.
57
+ // The scan keeps the last (largest) match.
58
+ const TUNING_STANDARD_INTERVALS = Intervals.INTERVAL_ORDER;
59
+ /** A label with no tuning rows of its own: a preset or an extended rung. */
60
+ function readsTuningAtOrBelow(label) {
61
+ return isCustomInterval(label) || isExtendedInterval(label);
62
+ }
63
+ /**
64
+ * The standard label whose per-interval settings a label uses. Tuning tables
65
+ * (profile rows, footprint bars, heatmap depth, restrictions...) list only
66
+ * the classic ladder, so a preset or an extended rung takes the largest
67
+ * classic label that is no longer than it (5s 15s 30s -> 1s, 3m -> 1m,
68
+ * 10m -> 5m, 45m -> 30m, 6h -> 4h, 12h -> 4h, 3d -> 1d, 6M -> 3M). A classic
69
+ * or unknown label comes back unchanged. Never use this for a wire interval:
70
+ * a chart must not be served another interval's bars.
71
+ */
72
+ export function standardIntervalAtOrBelow(label) {
73
+ if (!readsTuningAtOrBelow(label))
74
+ return label;
75
+ const ms = intervalMs(label);
76
+ if (ms === undefined)
77
+ return label;
78
+ let standard = label;
79
+ for (const candidate of TUNING_STANDARD_INTERVALS) {
80
+ const candidateMs = intervalMs(candidate);
81
+ if (candidateMs !== undefined && candidateMs <= ms)
82
+ standard = candidate;
83
+ }
84
+ return standard;
85
+ }
86
+ /** `standardIntervalAtOrBelow` for a millisecond length: a preset or extended length maps to its standard's length. */
87
+ export function standardIntervalMsAtOrBelow(ms) {
88
+ const label = intervalLabelForMs(ms);
89
+ if (label === undefined || !readsTuningAtOrBelow(label))
90
+ return ms;
91
+ return intervalMs(standardIntervalAtOrBelow(label)) ?? ms;
92
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * The pure steps of the kScript drawing door (kscript-drawings.util) a host runs on a wrun run's drawing records
3
+ * before it mounts them: the hidden Style targets' records dropped (the visibility table's `drawing` group) and a
4
+ * card, feed, meter or ladder record's colour and font words resolved against the chart's palette (WRUN-STYLE DW11,
5
+ * DW38). Split out of the door, which reads the owner's settings and the palette off the chart and passes them in;
6
+ * no store, no chart instance here. It rides the `@orangecharts/wrun-chart-host` package
7
+ * (docs/features/kscript/wrun/wrun-chart-host.md).
8
+ */
9
+ import { type WrunColorTheme } from '../wrun/wrun-style-words.util.js';
10
+ /**
11
+ * A wrun run's drawing records less those of a Style target the viewer switched off: the visibility
12
+ * table's `drawing` group (wrun-visibility-table.ts: a sheet drawing, a segment, a draw-API handle kind and
13
+ * its line fills), whose records the run result stamps with their `styleTarget`, hidden by the owning
14
+ * Indicator's `__style__<target>__visible` false. Dropped before the caps and the linefill closure assemble
15
+ * the set, so a hidden record never takes a cap slot, and a set left empty mounts as an explicit empty set
16
+ * (an absent `drawings` would leave the last mount on the chart). A kScript record carries no target and
17
+ * always stays; the input itself when nothing is hidden. `ownerSettings` reads the owning Indicator's settings
18
+ * (null when there are none), at the first record that names a target and at most once.
19
+ */
20
+ export declare function wrunDrawingsWithoutHidden(drawings: unknown[], ownerSettings: () => Record<string, unknown> | null): unknown[];
21
+ /** A drawing set as one comparable string (null when it cannot be serialized). */
22
+ export declare function drawingSignature(drawings: unknown[]): string | null;
23
+ export declare const KSCRIPT_WIDGET_KINDS: Set<string>;
24
+ /** A font word as its CSS stack; a CSS string (or anything else) is itself. */
25
+ export declare function paintFontWord(value: unknown): unknown;
26
+ /** `record` with `key` set to `next`, the same object when nothing moved. */
27
+ export declare function withKey(record: Record<string, unknown>, key: string, next: unknown): Record<string, unknown>;
28
+ /** `list` with every entry mapped, the same array when nothing moved. */
29
+ export declare function withEntries(list: unknown[], map: (entry: unknown) => unknown): unknown[];
30
+ /**
31
+ * A card, feed, meter or ladder record at the door: resolved from its raw
32
+ * lineage (the words) against the current palette. The same props come back
33
+ * when the words resolve to what the record already holds (a theme apply
34
+ * that moved nothing keeps the mounted set's identity and signature); a
35
+ * fresh copy, remembered with its lineage, when a colour moved.
36
+ */
37
+ export declare function paintWidgetRecord(kind: string, props: Record<string, unknown>, theme: WrunColorTheme): {
38
+ props: Record<string, unknown>;
39
+ changed: boolean;
40
+ };
@@ -0,0 +1,184 @@
1
+ /**
2
+ * The pure steps of the kScript drawing door (kscript-drawings.util) a host runs on a wrun run's drawing records
3
+ * before it mounts them: the hidden Style targets' records dropped (the visibility table's `drawing` group) and a
4
+ * card, feed, meter or ladder record's colour and font words resolved against the chart's palette (WRUN-STYLE DW11,
5
+ * DW38). Split out of the door, which reads the owner's settings and the palette off the chart and passes them in;
6
+ * no store, no chart instance here. It rides the `@orangecharts/wrun-chart-host` package
7
+ * (docs/features/kscript/wrun/wrun-chart-host.md).
8
+ */
9
+ import { parseWrunThemeToken, resolveWrunColorWord, wrunFontFamilyCss, } from '../wrun/wrun-style-words.util.js';
10
+ import { wrunVisibleRowKey } from '../wrun/wrun-visibility-table.js';
11
+ function isRecord(value) {
12
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
13
+ }
14
+ /**
15
+ * A wrun run's drawing records less those of a Style target the viewer switched off: the visibility
16
+ * table's `drawing` group (wrun-visibility-table.ts: a sheet drawing, a segment, a draw-API handle kind and
17
+ * its line fills), whose records the run result stamps with their `styleTarget`, hidden by the owning
18
+ * Indicator's `__style__<target>__visible` false. Dropped before the caps and the linefill closure assemble
19
+ * the set, so a hidden record never takes a cap slot, and a set left empty mounts as an explicit empty set
20
+ * (an absent `drawings` would leave the last mount on the chart). A kScript record carries no target and
21
+ * always stays; the input itself when nothing is hidden. `ownerSettings` reads the owning Indicator's settings
22
+ * (null when there are none), at the first record that names a target and at most once.
23
+ */
24
+ export function wrunDrawingsWithoutHidden(drawings, ownerSettings) {
25
+ let settings;
26
+ let shown;
27
+ for (let at = 0; at < drawings.length; at += 1) {
28
+ const drawing = drawings[at];
29
+ const target = isRecord(drawing) ? drawing.styleTarget : undefined;
30
+ if (typeof target === 'string') {
31
+ if (settings === undefined)
32
+ settings = ownerSettings();
33
+ if (settings?.[wrunVisibleRowKey(target)] === false) {
34
+ shown ??= drawings.slice(0, at);
35
+ continue;
36
+ }
37
+ }
38
+ shown?.push(drawing);
39
+ }
40
+ return shown ?? drawings;
41
+ }
42
+ /** A drawing set as one comparable string (null when it cannot be serialized). */
43
+ export function drawingSignature(drawings) {
44
+ try {
45
+ return JSON.stringify(drawings);
46
+ }
47
+ catch {
48
+ return null;
49
+ }
50
+ }
51
+ // ---------------------------------------------------------------------------
52
+ // WRUN-STYLE: colour words and font words at the drawing door (DW11, DW38,
53
+ // TH-theme-tokens, X14). A card, feed, meter or ladder record paints its own
54
+ // chrome on titan's card layer: it is never contrast-guarded, and its colour
55
+ // words (a theme token such as "theme.bg", a "#rrggbbaa") resolve here
56
+ // against the chart's palette, so titan only ever sees a CSS colour. The
57
+ // unresolved record stays behind the resolved copy as its raw lineage
58
+ // (`widgetRawProps`), so a theme switch re-resolves the words with no run:
59
+ // reguardKScriptDrawingsForOverlay re-enters this door with the remembered
60
+ // (resolved) record and the words are read back from the lineage. Font words
61
+ // ("ui", "mono", "serif", "rounded") become their CSS stacks on every kind.
62
+ // ---------------------------------------------------------------------------
63
+ export const KSCRIPT_WIDGET_KINDS = new Set(['card', 'feed', 'meter', 'ladder']);
64
+ /** DrawingCardStyle colour keys (a card, feed or meter `style`). */
65
+ const WIDGET_STYLE_COLOR_KEYS = [
66
+ 'background',
67
+ 'borderColor',
68
+ 'textColor',
69
+ 'labelColor',
70
+ 'titleColor',
71
+ 'accentColor',
72
+ 'ruleColor',
73
+ ];
74
+ /** DrawingCardStyle colour lists. */
75
+ const WIDGET_STYLE_COLOR_LIST_KEYS = ['backgroundGradient', 'stateColors'];
76
+ /** DrawingLadderStyle colour keys. */
77
+ const LADDER_STYLE_COLOR_KEYS = ['color', 'textColor', 'dividerColor'];
78
+ /** The unresolved props behind a widget record this door resolved, by the
79
+ * resolved props object (a WeakMap: a dropped record leaves nothing). */
80
+ const widgetRawProps = new WeakMap();
81
+ /** A colour word resolved against the palette: a theme token becomes the
82
+ * chart's colour; any other value (a CSS colour, a non-string) is itself. */
83
+ function paintColorWord(value, theme) {
84
+ if (typeof value !== 'string' || parseWrunThemeToken(value) === null)
85
+ return value;
86
+ return resolveWrunColorWord(value, theme);
87
+ }
88
+ /** A font word as its CSS stack; a CSS string (or anything else) is itself. */
89
+ export function paintFontWord(value) {
90
+ return wrunFontFamilyCss(value) ?? value;
91
+ }
92
+ /** `record` with `key` set to `next`, the same object when nothing moved. */
93
+ export function withKey(record, key, next) {
94
+ return Object.is(record[key], next) ? record : { ...record, [key]: next };
95
+ }
96
+ /** `list` with every entry mapped, the same array when nothing moved. */
97
+ export function withEntries(list, map) {
98
+ let next = null;
99
+ list.forEach((entry, index) => {
100
+ const painted = map(entry);
101
+ if (Object.is(painted, entry))
102
+ return;
103
+ if (!next)
104
+ next = [...list];
105
+ next[index] = painted;
106
+ });
107
+ return next ?? list;
108
+ }
109
+ /** A tuple row's colour cell (ladder rows[][3], feed lines[][2]) resolved. */
110
+ function paintTupleColor(row, cell, theme) {
111
+ if (!Array.isArray(row) || row.length <= cell)
112
+ return row;
113
+ const painted = paintColorWord(row[cell], theme);
114
+ if (Object.is(painted, row[cell]))
115
+ return row;
116
+ const next = [...row];
117
+ next[cell] = painted;
118
+ return next;
119
+ }
120
+ /** A card-family or ladder `style` block with its colour words resolved and
121
+ * its font word as CSS. */
122
+ function paintWidgetStyle(style, colorKeys, listKeys, theme) {
123
+ let next = style;
124
+ for (const key of colorKeys)
125
+ next = withKey(next, key, paintColorWord(style[key], theme));
126
+ for (const key of listKeys) {
127
+ if (Array.isArray(style[key]))
128
+ next = withKey(next, key, withEntries(style[key], (c) => paintColorWord(c, theme)));
129
+ }
130
+ return withKey(next, 'fontFamily', paintFontWord(style.fontFamily));
131
+ }
132
+ /** A widget record's props painted for the chart: every colour word the
133
+ * contract names (DW11) resolved against the palette, the style's font
134
+ * word as CSS. Returns `raw` itself when nothing moved. */
135
+ function paintWidgetProps(kind, raw, theme) {
136
+ let next = raw;
137
+ if (isRecord(raw.style)) {
138
+ next = withKey(next, 'style', kind === 'ladder'
139
+ ? paintWidgetStyle(raw.style, LADDER_STYLE_COLOR_KEYS, [], theme)
140
+ : paintWidgetStyle(raw.style, WIDGET_STYLE_COLOR_KEYS, WIDGET_STYLE_COLOR_LIST_KEYS, theme));
141
+ }
142
+ if (kind === 'card') {
143
+ if (isRecord(raw.headline)) {
144
+ next = withKey(next, 'headline', withKey(raw.headline, 'color', paintColorWord(raw.headline.color, theme)));
145
+ }
146
+ if (Array.isArray(raw.rows)) {
147
+ next = withKey(next, 'rows', withEntries(raw.rows, (row) => (isRecord(row) ? withKey(row, 'color', paintColorWord(row.color, theme)) : row)));
148
+ }
149
+ }
150
+ else if (kind === 'ladder') {
151
+ if (Array.isArray(raw.rows)) {
152
+ next = withKey(next, 'rows', withEntries(raw.rows, (row) => paintTupleColor(row, 3, theme)));
153
+ }
154
+ }
155
+ else if (kind === 'feed') {
156
+ if (Array.isArray(raw.lines)) {
157
+ next = withKey(next, 'lines', withEntries(raw.lines, (line) => paintTupleColor(line, 2, theme)));
158
+ }
159
+ }
160
+ else if (kind === 'meter') {
161
+ if (Array.isArray(raw.ramp))
162
+ next = withKey(next, 'ramp', withEntries(raw.ramp, (c) => paintColorWord(c, theme)));
163
+ next = withKey(next, 'trackColor', paintColorWord(raw.trackColor, theme));
164
+ }
165
+ return next;
166
+ }
167
+ /**
168
+ * A card, feed, meter or ladder record at the door: resolved from its raw
169
+ * lineage (the words) against the current palette. The same props come back
170
+ * when the words resolve to what the record already holds (a theme apply
171
+ * that moved nothing keeps the mounted set's identity and signature); a
172
+ * fresh copy, remembered with its lineage, when a colour moved.
173
+ */
174
+ export function paintWidgetRecord(kind, props, theme) {
175
+ const raw = widgetRawProps.get(props) ?? props;
176
+ const painted = paintWidgetProps(kind, raw, theme);
177
+ if (painted === raw)
178
+ return { props, changed: false };
179
+ if (painted !== props && props !== raw && drawingSignature([painted]) === drawingSignature([props])) {
180
+ return { props, changed: false };
181
+ }
182
+ widgetRawProps.set(painted, raw);
183
+ return { props: painted, changed: true };
184
+ }