@tidal-ts/terminal 0.23.0 → 0.25.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.
package/dist/context.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { type ReactNode } from 'react';
2
2
  import type { InspectionEvent, Observer } from 'xstate';
3
3
  import type { DeriveOp, PairOp } from '@tidal-ts/core';
4
- import type { AxisRange, PairLensOp, SeriesConfig } from '@tidal-ts/chart';
4
+ import type { AxisRange, CompareWith, PairLensOp, SeriesConfig } from '@tidal-ts/chart';
5
5
  import { type MetricEntry, type PairLegInput, type PresetSet, type PresetStorage, type RowState, type TerminalPreset } from './terminalMachine.js';
6
6
  export interface TerminalProviderProps {
7
7
  initialRows: RowState[];
@@ -56,6 +56,8 @@ export declare function useAddPair(): (a: PairLegInput, b: PairLegInput, op: Pai
56
56
  export declare function useCompareAll(): () => void;
57
57
  /** Set or clear (`null`) the function a compare applies to a metric. */
58
58
  export declare function useSetCompareFn(): (id: string, fn: PairOp | null) => void;
59
+ /** Wire a compare's other side by hand, or `null` for automatic. */
60
+ export declare function useSetCompareWith(): (id: string, other: CompareWith | null) => void;
59
61
  export declare function useSetStudyParam(): (id: string, name: string, value: number) => void;
60
62
  /** Switch which output of a multi-output source a study reads (the output
61
63
  * picker). Pair it with `outputAllowed` to render refused choices disabled. */
package/dist/context.js CHANGED
@@ -109,6 +109,11 @@ export function useSetCompareFn() {
109
109
  const ref = TerminalContext.useActorRef();
110
110
  return useCallback((id, fn) => ref.send({ type: 'series.setCompareFn', id, fn }), [ref]);
111
111
  }
112
+ /** Wire a compare's other side by hand, or `null` for automatic. */
113
+ export function useSetCompareWith() {
114
+ const ref = TerminalContext.useActorRef();
115
+ return useCallback((id, other) => ref.send({ type: 'series.setCompareWith', id, with: other }), [ref]);
116
+ }
112
117
  export function useSetStudyParam() {
113
118
  const ref = TerminalContext.useActorRef();
114
119
  return useCallback((id, name, value) => ref.send({ type: 'series.setStudyParam', id, name, value }), [ref]);
package/dist/index.d.ts CHANGED
@@ -5,6 +5,6 @@
5
5
  * union — the pre-paid bus contract. The actor context + machine internals stay
6
6
  * private to the module.
7
7
  */
8
- export { TerminalProvider, useRows, useSelectedSeries, useExpandedSeries, useControlsOpen, usePresetSaved, useActiveSlot, useAddSeries, useAddPair, useCompareAll, useAddStudy, useSetCompareFn, useSetStudyParam, useSetStudyOutput, useSetStudyInput, useSetDataColumns, useSetStudyOutputs, useOrderSeries, useReindexSeries, useMoveSeriesToRow, useMoveToAxis, useRemoveSeries, usePatchSeries, useToggleVisible, useToggleGroupVisible, useRemoveGroup, useSelectSeries, useExpandSeries, useSetControlsOpen, useSelectPreset, useSavePreset, useSetPresetSet, usePresetSet, useSetRowHeights, useAddRow, useRemoveRow, useAxisRanges, useToggleAxisLink, useSwapAxisSide, useSetAxisRange, useSetAxisAuto, useSetAxisLocks, useSetAxisAutoOpts, useSetAxisFormat, useTerminalHotkeys, type TerminalProviderProps, } from './context.js';
9
- export { PRESET_SLOTS, PRESET_SETS, type PresetSet, type PresetSlot, MAX_ROWS, activeSlot, canonRows, columnLabel, columnUnits, compareFnAllowed, entryColumn, groupMembers, mintId, axisMoveAllowed, linkToggleAllowed, respecAllowed, outputAllowed, inputAllowed, dataColumnsAllowed, outputsAllowed, studyOutputChoices, studyOutputsOf, studyOutputsRead, defaultStudyOutputs, studyChainTail, studyOfferable, SOURCE_COLUMNS, swapAllowed, type EditScope, type RowState, type TerminalPreset, type MetricEntry, type PairLegInput, type PresetStorage, type TerminalEvent, type TerminalInput, } from './terminalMachine.js';
8
+ export { TerminalProvider, useRows, useSelectedSeries, useExpandedSeries, useControlsOpen, usePresetSaved, useActiveSlot, useAddSeries, useAddPair, useCompareAll, useAddStudy, useSetCompareFn, useSetCompareWith, useSetStudyParam, useSetStudyOutput, useSetStudyInput, useSetDataColumns, useSetStudyOutputs, useOrderSeries, useReindexSeries, useMoveSeriesToRow, useMoveToAxis, useRemoveSeries, usePatchSeries, useToggleVisible, useToggleGroupVisible, useRemoveGroup, useSelectSeries, useExpandSeries, useSetControlsOpen, useSelectPreset, useSavePreset, useSetPresetSet, usePresetSet, useSetRowHeights, useAddRow, useRemoveRow, useAxisRanges, useToggleAxisLink, useSwapAxisSide, useSetAxisRange, useSetAxisAuto, useSetAxisLocks, useSetAxisAutoOpts, useSetAxisFormat, useTerminalHotkeys, type TerminalProviderProps, } from './context.js';
9
+ export { PRESET_SLOTS, PRESET_SETS, type PresetSet, type PresetSlot, MAX_ROWS, activeSlot, canonRows, columnLabel, columnUnits, compareFnAllowed, compareWithAllowed, entryColumn, groupMembers, mintId, axisMoveAllowed, linkToggleAllowed, respecAllowed, outputAllowed, inputAllowed, dataColumnsAllowed, outputsAllowed, studyOutputChoices, studyOutputsOf, studyOutputsRead, defaultStudyOutputs, studyChainTail, studyOfferable, SOURCE_COLUMNS, swapAllowed, type EditScope, type RowState, type TerminalPreset, type MetricEntry, type PairLegInput, type PresetStorage, type TerminalEvent, type TerminalInput, } from './terminalMachine.js';
10
10
  export { axisBindings, allowedAxes, canAddUnit, axisForAdd, type AxisMember, } from './axisPolicy.js';
package/dist/index.js CHANGED
@@ -5,8 +5,8 @@
5
5
  * union — the pre-paid bus contract. The actor context + machine internals stay
6
6
  * private to the module.
7
7
  */
8
- export { TerminalProvider, useRows, useSelectedSeries, useExpandedSeries, useControlsOpen, usePresetSaved, useActiveSlot, useAddSeries, useAddPair, useCompareAll, useAddStudy, useSetCompareFn, useSetStudyParam, useSetStudyOutput, useSetStudyInput, useSetDataColumns, useSetStudyOutputs, useOrderSeries, useReindexSeries, useMoveSeriesToRow, useMoveToAxis, useRemoveSeries, usePatchSeries, useToggleVisible, useToggleGroupVisible, useRemoveGroup, useSelectSeries, useExpandSeries, useSetControlsOpen, useSelectPreset, useSavePreset, useSetPresetSet, usePresetSet, useSetRowHeights, useAddRow, useRemoveRow, useAxisRanges, useToggleAxisLink, useSwapAxisSide, useSetAxisRange, useSetAxisAuto, useSetAxisLocks, useSetAxisAutoOpts, useSetAxisFormat, useTerminalHotkeys, } from './context.js';
9
- export { PRESET_SLOTS, PRESET_SETS, MAX_ROWS, activeSlot, canonRows, columnLabel, columnUnits, compareFnAllowed, entryColumn, groupMembers, mintId, axisMoveAllowed, linkToggleAllowed, respecAllowed, outputAllowed, inputAllowed, dataColumnsAllowed, outputsAllowed, studyOutputChoices, studyOutputsOf, studyOutputsRead, defaultStudyOutputs, studyChainTail, studyOfferable, SOURCE_COLUMNS, swapAllowed, } from './terminalMachine.js';
8
+ export { TerminalProvider, useRows, useSelectedSeries, useExpandedSeries, useControlsOpen, usePresetSaved, useActiveSlot, useAddSeries, useAddPair, useCompareAll, useAddStudy, useSetCompareFn, useSetCompareWith, useSetStudyParam, useSetStudyOutput, useSetStudyInput, useSetDataColumns, useSetStudyOutputs, useOrderSeries, useReindexSeries, useMoveSeriesToRow, useMoveToAxis, useRemoveSeries, usePatchSeries, useToggleVisible, useToggleGroupVisible, useRemoveGroup, useSelectSeries, useExpandSeries, useSetControlsOpen, useSelectPreset, useSavePreset, useSetPresetSet, usePresetSet, useSetRowHeights, useAddRow, useRemoveRow, useAxisRanges, useToggleAxisLink, useSwapAxisSide, useSetAxisRange, useSetAxisAuto, useSetAxisLocks, useSetAxisAutoOpts, useSetAxisFormat, useTerminalHotkeys, } from './context.js';
9
+ export { PRESET_SLOTS, PRESET_SETS, MAX_ROWS, activeSlot, canonRows, columnLabel, columnUnits, compareFnAllowed, compareWithAllowed, entryColumn, groupMembers, mintId, axisMoveAllowed, linkToggleAllowed, respecAllowed, outputAllowed, inputAllowed, dataColumnsAllowed, outputsAllowed, studyOutputChoices, studyOutputsOf, studyOutputsRead, defaultStudyOutputs, studyChainTail, studyOfferable, SOURCE_COLUMNS, swapAllowed, } from './terminalMachine.js';
10
10
  // The axis policy a host's MENUS gate on — the same predicates the machine's
11
11
  // guards enforce, exported so the two cannot disagree (0.1.0 left them internal;
12
12
  // a host had to keep its own copy).
@@ -12,7 +12,7 @@ export declare const SOURCE_COLUMNS: Readonly<Record<string, readonly string[]>>
12
12
  * The menu filters on it; `canAddStudy` refuses on it. */
13
13
  export declare function studyOfferable(source: string | undefined, op: DeriveOp): boolean;
14
14
  import { type PairLensOp } from '@tidal-ts/chart';
15
- import { type AxisRange, type SeriesAxis, type SeriesConfig } from '@tidal-ts/chart';
15
+ import { type AxisRange, type SeriesAxis, type SeriesConfig, type CompareWith } from '@tidal-ts/chart';
16
16
  /**
17
17
  * The terminal machine (XState migration, slice 2 — see Tidal's state-architecture note).
18
18
  * The **headless product**: it owns the control-panel *configuration* — rows of
@@ -236,7 +236,7 @@ export type TerminalEvent = {
236
236
  | {
237
237
  type: 'series.patch';
238
238
  id: string;
239
- patch: Partial<Omit<SeriesConfig, 'axis' | 'axisGroup' | 'parentIds' | 'compareFn'>>;
239
+ patch: Partial<Omit<SeriesConfig, 'axis' | 'axisGroup' | 'parentIds' | 'compareFn' | 'compareWith'>>;
240
240
  } | {
241
241
  type: 'series.toggleVisible';
242
242
  id: string;
@@ -251,6 +251,14 @@ export type TerminalEvent = {
251
251
  id: string;
252
252
  fn: PairOp | null;
253
253
  }
254
+ /** Wire a compare's other side by hand ({@link SeriesConfig.compareWith}),
255
+ * or `null` to make it automatic again. Refused for anything
256
+ * {@link compareWithAllowed} refuses. */
257
+ | {
258
+ type: 'series.setCompareWith';
259
+ id: string;
260
+ with: CompareWith | null;
261
+ }
254
262
  /** The PAIR's own eye — hides both legs at once. Each leg keeps its own
255
263
  * `visible`, so unhiding the pair restores what each leg was rather than
256
264
  * switching everything on (Peter, 2026-08-18). */
@@ -608,9 +616,18 @@ export declare function linkToggleAllowed(configs: readonly SeriesConfig[], id:
608
616
  * gutters.
609
617
  */
610
618
  export declare function swapAllowed(configs: readonly SeriesConfig[], id: string): boolean;
619
+ /**
620
+ * Whether `other` can be wired by hand as `target`'s compare other side
621
+ * ({@link SeriesConfig.compareWith}): the target must be able to carry a
622
+ * compare at all ({@link compareFnAllowed}), and the other side must be a
623
+ * different config with ONE line to read — its only line, or the one
624
+ * `output` names when it draws several ({@link compareWithInput}).
625
+ */
626
+ export declare function compareWithAllowed(target: SeriesConfig, other: SeriesConfig, output?: string): boolean;
611
627
  /**
612
628
  * Whether a seated config can carry a compare function
613
- * ({@link SeriesConfig.compareFn}).
629
+ * ({@link SeriesConfig.compareFn}) or a hand-wired other side
630
+ * ({@link SeriesConfig.compareWith}).
614
631
  *
615
632
  * Only the ROOT of a chain — a metric, raw or a derived catalog metric, never
616
633
  * a study: the function sits straight after the metric and every study on it
@@ -683,7 +700,7 @@ export declare const terminalMachine: import("xstate").StateMachine<TerminalCont
683
700
  } | {
684
701
  type: "series.patch";
685
702
  id: string;
686
- patch: Partial<Omit<SeriesConfig, "axis" | "axisGroup" | "parentIds" | "compareFn">>;
703
+ patch: Partial<Omit<SeriesConfig, "axis" | "axisGroup" | "parentIds" | "compareFn" | "compareWith">>;
687
704
  } | {
688
705
  type: "series.toggleVisible";
689
706
  id: string;
@@ -691,6 +708,10 @@ export declare const terminalMachine: import("xstate").StateMachine<TerminalCont
691
708
  type: "series.setCompareFn";
692
709
  id: string;
693
710
  fn: PairOp | null;
711
+ } | {
712
+ type: "series.setCompareWith";
713
+ id: string;
714
+ with: CompareWith | null;
694
715
  } | {
695
716
  type: "group.toggleVisible";
696
717
  groupId: string;
@@ -806,6 +827,9 @@ export declare const terminalMachine: import("xstate").StateMachine<TerminalCont
806
827
  } | {
807
828
  type: "setCompareFn";
808
829
  params: import("xstate").NonReducibleUnknown;
830
+ } | {
831
+ type: "setCompareWith";
832
+ params: import("xstate").NonReducibleUnknown;
809
833
  } | {
810
834
  type: "patchSeries";
811
835
  params: import("xstate").NonReducibleUnknown;
@@ -890,6 +914,9 @@ export declare const terminalMachine: import("xstate").StateMachine<TerminalCont
890
914
  } | {
891
915
  type: "canSetCompareFn";
892
916
  params: unknown;
917
+ } | {
918
+ type: "canSetCompareWith";
919
+ params: unknown;
893
920
  } | {
894
921
  type: "canSetStudyParam";
895
922
  params: unknown;
@@ -32,7 +32,7 @@ function hidesPointer(context, toggledId, pointer) {
32
32
  return cascade.has(pointer);
33
33
  }
34
34
  import { axisRangeIsEmpty, logAllowed, isStudySeries, readsFrom, sourcesOf, fanIsBand, offersOutputBars, publishedColumns, } from '@tidal-ts/chart';
35
- import { configAxisId, seriesAxisId, sharedAxisId, } from '@tidal-ts/chart';
35
+ import { configAxisId, seriesAxisId, sharedAxisId, compareWithInput, } from '@tidal-ts/chart';
36
36
  import { axisForAdd, canAddUnit } from './axisPolicy.js';
37
37
  export const PRESET_SETS = ['plain', 'compare'];
38
38
  /** Slots in each preset set. */
@@ -1072,7 +1072,7 @@ export function dataColumnsAllowed(scope, id, columns) {
1072
1072
  function coherentPatch(c, patch) {
1073
1073
  // `compareFn` is set only through `series.setCompareFn` and its guard; a
1074
1074
  // patch that names it (an untyped caller) is not let around that guard.
1075
- const { compareFn: _ignored, ...asked } = patch;
1075
+ const { compareFn: _ignored, compareWith: _alsoIgnored, ...asked } = patch;
1076
1076
  return keepCompareFnCoherent(drawablePatch(c, asked));
1077
1077
  }
1078
1078
  function drawablePatch(c, patch) {
@@ -1095,11 +1095,41 @@ function drawablePatch(c, patch) {
1095
1095
  * review). Everything else passes through unchanged.
1096
1096
  */
1097
1097
  function keepCompareFnCoherent(c) {
1098
- if (!c.compareFn || compareFnAllowed(c))
1098
+ if ((!c.compareFn && !c.compareWith) || compareFnAllowed(c))
1099
1099
  return c;
1100
- const { compareFn: _drop, ...rest } = c;
1100
+ const { compareFn: _drop, compareWith: _dropWith, ...rest } = c;
1101
1101
  return rest;
1102
1102
  }
1103
+ /**
1104
+ * Rows with every hand-wired compare that no longer holds made automatic
1105
+ * again: the config it names is gone, or has stopped being one line to read
1106
+ * ({@link compareWithAllowed}) — pulled into a pair as a leg, given several
1107
+ * columns, a band or a fan. Run after every edit that can change either side,
1108
+ * so a saved config never carries a wiring the guard would refuse (PR #48
1109
+ * review). Returns `rows` itself when nothing changes.
1110
+ */
1111
+ function keepCompareWithValid(rows) {
1112
+ const byId = new Map(flatConfigs(rows).map((c) => [c.id, c]));
1113
+ const holds = (c) => {
1114
+ if (!c.compareWith)
1115
+ return true;
1116
+ const other = byId.get(c.compareWith.id);
1117
+ return !!other && compareWithAllowed(c, other, c.compareWith.output);
1118
+ };
1119
+ if (flatConfigs(rows).every(holds))
1120
+ return rows;
1121
+ return rows.map((r) => ({
1122
+ ...r,
1123
+ configs: r.configs.map((c) => {
1124
+ if (holds(c))
1125
+ return c;
1126
+ const { compareWith: _drop, ...rest } = c;
1127
+ return rest;
1128
+ }),
1129
+ }));
1130
+ }
1131
+ /** A rows assigner whose result passes through {@link keepCompareWithValid}. */
1132
+ const keepingCompareWithValid = (fn) => (args) => keepCompareWithValid(fn(args));
1103
1133
  /** Bar price columns in the bar's own order. */
1104
1134
  const barOrder = (columns) => [...columns].sort((a, b) => BAR_PRICE_COLUMNS.indexOf(a) - BAR_PRICE_COLUMNS.indexOf(b));
1105
1135
  /**
@@ -1801,9 +1831,28 @@ function comparableMetric(c) {
1801
1831
  return false; // a fan is several lines too
1802
1832
  return !c.column.startsWith(COMPARE_PREFIX);
1803
1833
  }
1834
+ /**
1835
+ * Whether `other` can be wired by hand as `target`'s compare other side
1836
+ * ({@link SeriesConfig.compareWith}): the target must be able to carry a
1837
+ * compare at all ({@link compareFnAllowed}), and the other side must be a
1838
+ * different config with ONE line to read — its only line, or the one
1839
+ * `output` names when it draws several ({@link compareWithInput}).
1840
+ */
1841
+ export function compareWithAllowed(target, other, output) {
1842
+ if (!compareFnAllowed(target) || other.id === target.id)
1843
+ return false;
1844
+ // Read from its own spec: the same refusals as a function's metric, except
1845
+ // that a study is a fine other side (price against its own SMA).
1846
+ if (other.group || other.column.startsWith(COMPARE_PREFIX))
1847
+ return false;
1848
+ if (other.derive && (hasPairOp(other.derive) || usesCompare(other.derive)))
1849
+ return false;
1850
+ return compareWithInput(other, output) !== null;
1851
+ }
1804
1852
  /**
1805
1853
  * Whether a seated config can carry a compare function
1806
- * ({@link SeriesConfig.compareFn}).
1854
+ * ({@link SeriesConfig.compareFn}) or a hand-wired other side
1855
+ * ({@link SeriesConfig.compareWith}).
1807
1856
  *
1808
1857
  * Only the ROOT of a chain — a metric, raw or a derived catalog metric, never
1809
1858
  * a study: the function sits straight after the metric and every study on it
@@ -1834,6 +1883,9 @@ export function compareFnAllowed(c) {
1834
1883
  return true;
1835
1884
  }
1836
1885
  export function canonRows(rows) {
1886
+ // A hand-wired compare names its other side by config id, which is minted;
1887
+ // it hashes by what that config draws (its column) and the side instead.
1888
+ const columnOf = new Map(rows.flatMap((r) => r.configs.map((c) => [c.id, c.column])));
1837
1889
  return JSON.stringify(rows.map((r) => {
1838
1890
  // Axis membership is preset identity — but `axisGroup` is a MINTED config
1839
1891
  // id, and hashing it would make a hand-rebuilt copy of a saved preset read
@@ -1895,6 +1947,18 @@ export function canonRows(rows) {
1895
1947
  // Appended only when set, so a config without one hashes exactly as
1896
1948
  // it did before the field existed and a saved preset still matches.
1897
1949
  ...(c.compareFn ? [c.compareFn] : []),
1950
+ // …and what it reads as its other side, when that is wired by hand.
1951
+ // Appended the same way, after a placeholder for an absent function.
1952
+ ...(c.compareWith
1953
+ ? [
1954
+ ...(c.compareFn ? [] : [null]),
1955
+ [
1956
+ columnOf.get(c.compareWith.id) ?? null,
1957
+ c.compareWith.side,
1958
+ ...(c.compareWith.output ? [c.compareWith.output] : []),
1959
+ ],
1960
+ ]
1961
+ : []),
1898
1962
  ]),
1899
1963
  ];
1900
1964
  }));
@@ -2082,6 +2146,24 @@ export const terminalMachine = setup({
2082
2146
  // can no longer carry one must still be removable.
2083
2147
  return event.fn === null || (isPairOp(event.fn) && compareFnAllowed(target));
2084
2148
  },
2149
+ canSetCompareWith: ({ context, event }) => {
2150
+ if (event.type !== 'series.setCompareWith')
2151
+ return false;
2152
+ const all = flatConfigs(context.rows);
2153
+ const target = all.find((c) => c.id === event.id);
2154
+ if (!target)
2155
+ return false;
2156
+ if (event.with === null)
2157
+ return true;
2158
+ if (event.with.side !== 'primary' && event.with.side !== 'comparison')
2159
+ return false;
2160
+ const output = event.with.output;
2161
+ // A name, or none: an empty name would be stored as none.
2162
+ if (output !== undefined && (typeof output !== 'string' || output === ''))
2163
+ return false;
2164
+ const other = all.find((c) => c.id === event.with.id);
2165
+ return !!other && compareWithAllowed(target, other, output);
2166
+ },
2085
2167
  canSetStudyParam: ({ context, event }) => event.type === 'series.setStudyParam' &&
2086
2168
  respecAllowed(context, event.id, event.name, event.value),
2087
2169
  // The chain toggle — delegated to the shared pure helper so the panel's
@@ -2191,7 +2273,7 @@ export const terminalMachine = setup({
2191
2273
  },
2192
2274
  }),
2193
2275
  addPair: assign({
2194
- rows: ({ context, event }) => {
2276
+ rows: keepingCompareWithValid(({ context, event }) => {
2195
2277
  if (event.type !== 'series.addPair')
2196
2278
  return context.rows;
2197
2279
  const a = resolveLeg(context, event.a.metricId);
@@ -2246,7 +2328,7 @@ export const terminalMachine = setup({
2246
2328
  });
2247
2329
  }
2248
2330
  return context.rows.map((r) => r.id === row.id ? { ...r, configs: [cfg, ...r.configs] } : r);
2249
- },
2331
+ }),
2250
2332
  // Expand the new node so its controls open for tuning — the spread, or
2251
2333
  // (unjoined) whichever leg took the group's first minted id. Nothing to
2252
2334
  // expand when BOTH legs were adopted: no config was created.
@@ -2275,17 +2357,17 @@ export const terminalMachine = setup({
2275
2357
  // expansion, ink, axis membership and range pins all survive (they used to
2276
2358
  // be destroyed, and the dependents deleted outright).
2277
2359
  setStudyOutput: assign({
2278
- rows: ({ context, event }) => {
2360
+ rows: keepingCompareWithValid(({ context, event }) => {
2279
2361
  if (event.type !== 'series.setStudyOutput')
2280
2362
  return context.rows;
2281
2363
  const target = flatConfigs(context.rows).find((c) => c.id === event.id);
2282
2364
  if (!target?.derive)
2283
2365
  return context.rows;
2284
2366
  return applyRespec(context, event.id, respecOutput(target, event.output));
2285
- },
2367
+ }),
2286
2368
  }),
2287
2369
  setStudyInput: assign({
2288
- rows: ({ context, event }) => {
2370
+ rows: keepingCompareWithValid(({ context, event }) => {
2289
2371
  if (event.type !== 'series.setStudyInput')
2290
2372
  return context.rows;
2291
2373
  const configs = flatConfigs(context.rows);
@@ -2317,10 +2399,10 @@ export const terminalMachine = setup({
2317
2399
  return { ...c, parentIds };
2318
2400
  }),
2319
2401
  }));
2320
- },
2402
+ }),
2321
2403
  }),
2322
2404
  setDataColumns: assign({
2323
- rows: ({ context, event }) => {
2405
+ rows: keepingCompareWithValid(({ context, event }) => {
2324
2406
  if (event.type !== 'series.setDataColumns')
2325
2407
  return context.rows;
2326
2408
  const configs = flatConfigs(context.rows);
@@ -2356,10 +2438,10 @@ export const terminalMachine = setup({
2356
2438
  return { ...c, parentIds };
2357
2439
  }),
2358
2440
  }));
2359
- },
2441
+ }),
2360
2442
  }),
2361
2443
  setStudyOutputs: assign({
2362
- rows: ({ context, event }) => {
2444
+ rows: keepingCompareWithValid(({ context, event }) => {
2363
2445
  if (event.type !== 'series.setStudyOutputs')
2364
2446
  return context.rows;
2365
2447
  const study = flatConfigs(context.rows).find((c) => c.id === event.id);
@@ -2377,17 +2459,17 @@ export const terminalMachine = setup({
2377
2459
  ...r,
2378
2460
  configs: r.configs.map((c) => c.id === event.id ? { ...c, fan: next.fan, style: next.style } : c),
2379
2461
  }));
2380
- },
2462
+ }),
2381
2463
  }),
2382
2464
  setStudyParam: assign({
2383
- rows: ({ context, event }) => {
2465
+ rows: keepingCompareWithValid(({ context, event }) => {
2384
2466
  if (event.type !== 'series.setStudyParam')
2385
2467
  return context.rows;
2386
2468
  const target = flatConfigs(context.rows).find((c) => c.id === event.id);
2387
2469
  if (!target?.derive)
2388
2470
  return context.rows;
2389
2471
  return applyRespec(context, event.id, respecParam(target, event.name, event.value));
2390
- },
2472
+ }),
2391
2473
  }),
2392
2474
  addSeries: assign({
2393
2475
  rows: ({ context, event }) => {
@@ -2434,7 +2516,7 @@ export const terminalMachine = setup({
2434
2516
  },
2435
2517
  }),
2436
2518
  removeGroup: assign({
2437
- rows: ({ context, event }) => {
2519
+ rows: keepingCompareWithValid(({ context, event }) => {
2438
2520
  if (event.type !== 'group.remove')
2439
2521
  return context.rows;
2440
2522
  const doomed = groupDoomed(context.rows, event.groupId);
@@ -2443,7 +2525,7 @@ export const terminalMachine = setup({
2443
2525
  configs: r.configs.filter((c) => !doomed.has(c.id)),
2444
2526
  }));
2445
2527
  return pruneEmptied(context.rows, mapped);
2446
- },
2528
+ }),
2447
2529
  // The same invariant `removeSeries` states: neither pointer may point at a
2448
2530
  // removed series. Sibling assigners read the PRE-update context, so both
2449
2531
  // recompute the doomed set rather than reading the new rows (PR #149
@@ -2509,7 +2591,7 @@ export const terminalMachine = setup({
2509
2591
  },
2510
2592
  }),
2511
2593
  removeSeries: assign({
2512
- rows: ({ context, event }) => {
2594
+ rows: keepingCompareWithValid(({ context, event }) => {
2513
2595
  if (event.type !== 'series.remove')
2514
2596
  return context.rows;
2515
2597
  // Cascade to dependent studies so a nested study can't strand invisible +
@@ -2530,7 +2612,7 @@ export const terminalMachine = setup({
2530
2612
  // Not only the host row: an oscillator on its own row is in the
2531
2613
  // cascade, and its row must not stay behind as an empty pane.
2532
2614
  return pruneEmptied(context.rows, mapped);
2533
- },
2615
+ }),
2534
2616
  // Neither pointer may point at a removed series (or a cascaded dependent).
2535
2617
  selected: ({ context, event }) => event.type === 'series.remove' &&
2536
2618
  context.selected != null &&
@@ -2547,7 +2629,7 @@ export const terminalMachine = setup({
2547
2629
  // (a drag's absolute drop). Both splice-move within the series' own row; the
2548
2630
  // list is front→back, so index 0 is topmost on the canvas.
2549
2631
  orderSeries: assign({
2550
- rows: ({ context, event }) => {
2632
+ rows: keepingCompareWithValid(({ context, event }) => {
2551
2633
  if (event.type !== 'series.order' && event.type !== 'series.reindex')
2552
2634
  return context.rows;
2553
2635
  return context.rows.map((r) => {
@@ -2566,14 +2648,14 @@ export const terminalMachine = setup({
2566
2648
  : Math.max(0, Math.min(event.index, blocks.length - 1));
2567
2649
  return to === i ? r : { ...r, configs: reindex(blocks, i, to).flat() };
2568
2650
  });
2569
- },
2651
+ }),
2570
2652
  }),
2571
2653
  // Cross-row move: the series leaves its row (pruned if that empties it, as
2572
2654
  // with remove) and lands at the FRONT of the target row, on an axis its unit
2573
2655
  // can take there (the guard already proved one exists). Studies layered on it
2574
2656
  // travel with it, so a study can't strand on a row without its source.
2575
2657
  moveSeriesToRow: assign({
2576
- rows: ({ context, event }) => {
2658
+ rows: keepingCompareWithValid(({ context, event }) => {
2577
2659
  if (event.type !== 'series.moveToRow')
2578
2660
  return context.rows;
2579
2661
  const from = context.rows.find((r) => r.configs.some((c) => c.id === event.id));
@@ -2597,7 +2679,7 @@ export const terminalMachine = setup({
2597
2679
  });
2598
2680
  // An emptied source row goes away; the remainder row is kept.
2599
2681
  return pruneEmptied(context.rows, mapped);
2600
- },
2682
+ }),
2601
2683
  }),
2602
2684
  setCompareFn: assign({
2603
2685
  rows: ({ context, event }) => {
@@ -2618,8 +2700,29 @@ export const terminalMachine = setup({
2618
2700
  : r);
2619
2701
  },
2620
2702
  }),
2621
- patchSeries: assign({
2703
+ setCompareWith: assign({
2622
2704
  rows: ({ context, event }) => {
2705
+ if (event.type !== 'series.setCompareWith')
2706
+ return context.rows;
2707
+ return context.rows.map((r) => r.configs.some((c) => c.id === event.id)
2708
+ ? {
2709
+ ...r,
2710
+ configs: r.configs.map((c) => {
2711
+ if (c.id !== event.id)
2712
+ return c;
2713
+ // Automatic again by REMOVING the key, as for the function.
2714
+ const { compareWith: _drop, ...rest } = c;
2715
+ if (!event.with)
2716
+ return rest;
2717
+ const { id, side, output } = event.with;
2718
+ return { ...rest, compareWith: { id, side, ...(output ? { output } : {}) } };
2719
+ }),
2720
+ }
2721
+ : r);
2722
+ },
2723
+ }),
2724
+ patchSeries: assign({
2725
+ rows: keepingCompareWithValid(({ context, event }) => {
2623
2726
  if (event.type !== 'series.patch')
2624
2727
  return context.rows;
2625
2728
  // A study's colour is its parent's until the user says otherwise
@@ -2664,7 +2767,7 @@ export const terminalMachine = setup({
2664
2767
  ? { ...c, color: color }
2665
2768
  : c),
2666
2769
  }));
2667
- },
2770
+ }),
2668
2771
  }),
2669
2772
  toggleVisible: assign({
2670
2773
  rows: ({ context, event }) => {
@@ -3005,7 +3108,7 @@ export const terminalMachine = setup({
3005
3108
  removeRow: assign({
3006
3109
  // Removing a row is removing each of its series (`rowDoomed`), so a
3007
3110
  // study of one of its STUDIES goes too, on whatever row it sits.
3008
- rows: ({ context, event }) => {
3111
+ rows: keepingCompareWithValid(({ context, event }) => {
3009
3112
  if (event.type !== 'row.remove' || context.rows.length <= 1)
3010
3113
  return context.rows;
3011
3114
  const doomed = rowDoomed(context.rows, event.id);
@@ -3015,7 +3118,7 @@ export const terminalMachine = setup({
3015
3118
  // Removing the remainder row would leave the stack with none, so the
3016
3119
  // first survivor takes over as the one that absorbs slack.
3017
3120
  return pruneEmptied(context.rows, after);
3018
- },
3121
+ }),
3019
3122
  // Removing a row drops its series for good, so a pointer into it can't return
3020
3123
  // (unlike a ticker switch) — clear both (mirrors removeSeries).
3021
3124
  selected: ({ context, event }) => {
@@ -3095,6 +3198,7 @@ export const terminalMachine = setup({
3095
3198
  'series.remove': { actions: 'removeSeries' },
3096
3199
  'series.patch': { actions: 'patchSeries' },
3097
3200
  'series.setCompareFn': { guard: 'canSetCompareFn', actions: 'setCompareFn' },
3201
+ 'series.setCompareWith': { guard: 'canSetCompareWith', actions: 'setCompareWith' },
3098
3202
  'series.toggleVisible': { actions: 'toggleVisible' },
3099
3203
  'group.toggleVisible': { actions: 'toggleGroupVisible' },
3100
3204
  'group.remove': { actions: 'removeGroup' },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tidal-ts/terminal",
3
- "version": "0.23.0",
3
+ "version": "0.25.0",
4
4
  "private": false,
5
5
  "description": "The terminal machine — an XState v5 statechart that owns a chart's configuration (rows, series, studies, pairs, axes, presets) and never its data.",
6
6
  "license": "MIT",
@@ -36,8 +36,8 @@
36
36
  "xstate": "^5.28.0"
37
37
  },
38
38
  "dependencies": {
39
- "@tidal-ts/chart": "0.23.0",
40
- "@tidal-ts/core": "0.23.0"
39
+ "@tidal-ts/chart": "0.25.0",
40
+ "@tidal-ts/core": "0.25.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@pond-ts/charts": "^0.78.0",