@vehicle-sil/gui-client 0.0.0-stage → 0.2.0-rc.1

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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.ja.md +839 -0
  3. package/README.md +836 -2
  4. package/dist/elements.js +872 -0
  5. package/dist/index.js +5354 -0
  6. package/package.json +39 -4
  7. package/timeline/vehicle-sil-timeline.d.ts +108 -0
  8. package/timeline/vehicle-sil-timeline.js +4391 -0
  9. package/timeline/vehicle-sil-timeline.mjs +4393 -0
  10. package/types/client.d.ts +148 -0
  11. package/types/connection.d.ts +155 -0
  12. package/types/elements/index.d.ts +25 -0
  13. package/types/elements/outside-parts.d.ts +20 -0
  14. package/types/elements/page-intent.d.ts +10 -0
  15. package/types/elements/part.d.ts +48 -0
  16. package/types/elements/reasons.d.ts +12 -0
  17. package/types/elements/run-parts.d.ts +21 -0
  18. package/types/elements/scenario-parts.d.ts +18 -0
  19. package/types/elements/variable-parts.d.ts +28 -0
  20. package/types/front-actions.d.ts +122 -0
  21. package/types/front-compose.d.ts +61 -0
  22. package/types/front-outside.d.ts +19 -0
  23. package/types/front-types.d.ts +353 -0
  24. package/types/front-variable.d.ts +28 -0
  25. package/types/front.d.ts +50 -0
  26. package/types/http.d.ts +71 -0
  27. package/types/index.d.ts +28 -0
  28. package/types/protocol/launcher.d.ts +578 -0
  29. package/types/protocol/read.d.ts +72 -0
  30. package/types/protocol/server.d.ts +758 -0
  31. package/types/protocol.d.ts +15 -0
  32. package/types/run.d.ts +74 -0
  33. package/types/session-state.d.ts +289 -0
  34. package/types/session.d.ts +130 -0
  35. package/types/time.d.ts +133 -0
  36. package/types/token.d.ts +35 -0
  37. package/types/urls.d.ts +8 -0
  38. package/types/values.d.ts +54 -0
  39. package/types/variable-binding.d.ts +181 -0
  40. package/types/variable-state.d.ts +134 -0
  41. package/types/variables.d.ts +382 -0
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Takes the token from the URL the server printed (`http://127.0.0.1:<port>/#token=<...>`).
3
+ *
4
+ * Every screen the GUI server serves (the sample screens, and your own screen served with `--web`) reads `#token=`
5
+ * from the URL it was opened with, removes it from the URL right away (also from the tab's back/forward history; the
6
+ * browsing history may still keep it) and keeps it in the tab's sessionStorage (it survives a reload and is gone when
7
+ * the tab closes). Browser objects come in as arguments (this library runs on Node too, so it does not read `window`
8
+ * directly). In the browser, call `takeToken(window)`.
9
+ */
10
+ /** sessionStorage key. Screens of the same server (same origin) share this key. */
11
+ export declare const TOKEN_STORAGE_KEY = "vehicle-sil-gui-token";
12
+ /** Where to read the token from. The browser's `window` has this shape. */
13
+ export interface TokenPlace {
14
+ readonly location: {
15
+ readonly hash: string;
16
+ readonly pathname: string;
17
+ readonly search: string;
18
+ };
19
+ readonly history: {
20
+ readonly state: unknown;
21
+ replaceState(data: unknown, unused: string, url?: string | null): void;
22
+ };
23
+ /** Even reading it can throw (when the browser blocks site data); the token is then kept in memory only. */
24
+ readonly sessionStorage?: {
25
+ getItem(key: string): string | null;
26
+ setItem(key: string, value: string): void;
27
+ } | null;
28
+ }
29
+ /**
30
+ * Reads `#token=` from the URL, removes it from the URL, stores it in sessionStorage and returns it. If the URL has
31
+ * none, returns the one in sessionStorage (received earlier in the same tab). If neither has one, returns null (the
32
+ * page was not opened from the URL the server printed).
33
+ * Call it before anything rewrites the URL (when the screen's router rewrites the URL, `#token=` is lost).
34
+ */
35
+ export declare function takeToken(place: TokenPlace): string | null;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Builds the URLs to the server.
3
+ *
4
+ * URLs have no leading / and are relative to <base href> (`api/app`, `ws`), so a screen served under a path other
5
+ * than / (for example /gui/) works unchanged.
6
+ */
7
+ /** WebSocket URL relative to the base href (ws for http, wss for https). */
8
+ export declare function webSocketUrl(baseUri: string, path: string): string;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Readable value: the current value (get) and change notifications (subscribe). All of the library's state comes in
3
+ * this form.
4
+ *
5
+ * - Plain JavaScript can use it directly (no framework needed). A framework wrapper (an Angular signal and so on) takes
6
+ * the first value with get() and mirrors later values received through subscribe().
7
+ * - subscribe(listener) calls listener whenever the value changes (not with the current value). Calling the returned
8
+ * function unsubscribes.
9
+ * - Notifications are synchronous: they arrive inside the code that changed the value. A listener that unsubscribes
10
+ * during a round of notifications still gets that round's notification.
11
+ * - If a listener changes the same value, the new value reaches every listener and the rest of the notifications for
12
+ * the old value are dropped (an old value never arrives later).
13
+ * - A listener that throws stops neither the notification of the other listeners nor the library's state updates.
14
+ * What it threw goes to the client's `onError` (an option of createClient).
15
+ */
16
+ export interface Readable<T> {
17
+ get(): T;
18
+ subscribe(listener: (value: T) => void): () => void;
19
+ }
20
+ /** Receives what a subscriber threw. */
21
+ export type ErrorSink = (error: unknown) => void;
22
+ /** Default ErrorSink: rethrows later (never in the middle of notifying). */
23
+ export declare const rethrowLater: ErrorSink;
24
+ /** Calls each listener. If one throws, passes it to onError and moves on. */
25
+ export declare function callEach<A extends unknown[]>(listeners: Iterable<(...args: A) => void>, args: A, onError: ErrorSink, stillCurrent?: () => boolean): void;
26
+ /** Writable value. The library keeps its state in these and exposes them read-only through readonly(). */
27
+ export declare class Writable<T> implements Readable<T> {
28
+ /** Equality test. Equal values are not notified. */
29
+ private readonly equal;
30
+ /** Receives what a subscriber threw. */
31
+ private readonly onError;
32
+ private value;
33
+ private version;
34
+ private readonly listeners;
35
+ constructor(initial: T,
36
+ /** Equality test. Equal values are not notified. */
37
+ equal?: (a: T, b: T) => boolean,
38
+ /** Receives what a subscriber threw. */
39
+ onError?: ErrorSink);
40
+ get(): T;
41
+ set(value: T): void;
42
+ update(change: (value: T) => T): void;
43
+ subscribe(listener: (value: T) => void): () => void;
44
+ /** Read-only view (get and subscribe only). */
45
+ readonly(): Readable<T>;
46
+ }
47
+ /**
48
+ * Value derived from other readable values. Recomputed with compute whenever a source changes, and notified only when
49
+ * equal says it differs. Stays subscribed to the sources for good (use it for values that live as long as the
50
+ * derived value, for example inside the client).
51
+ */
52
+ export declare function derived<T>(sources: readonly Readable<unknown>[], compute: () => T, equal?: (a: T, b: T) => boolean, onError?: ErrorSink): Readable<T>;
53
+ /** Shallow equality (every field of the objects is the same by Object.is). Used for state snapshots. */
54
+ export declare function shallowEqual<T extends object>(a: T, b: T): boolean;
@@ -0,0 +1,181 @@
1
+ import type { VariableAvailability } from './protocol.js';
2
+ import { ConnectionState } from './connection.js';
3
+ import { Reason, SessionView } from './session-state.js';
4
+ import { ErrorSink, Readable } from './values.js';
5
+ import { ValueCondition, WriteBlock, WriteStatus, WriteValue } from './variable-state.js';
6
+ import { HistoryPoint, OperationView, VariableInfo, VariableReading, VariableRef, VariableWatch, VariablesState, WatchOptions, WriteOptions } from './variables.js';
7
+ /** Options of client.variables.bind. */
8
+ export interface BindOptions {
9
+ /** Interval at which values are received (ms; default WATCH_PERIOD_MS). */
10
+ periodMs?: number;
11
+ /** Number of history points to keep (for graphs; default 0). */
12
+ history?: number;
13
+ /** Do not allow writes to a variable without a range (min and max) (sliders). Meanwhile block is `no-range`. */
14
+ needsRange?: boolean;
15
+ /**
16
+ * Do not allow writes while there is no value (read or sent) (widgets that move from the value: sliders). Meanwhile
17
+ * block is `no-value`.
18
+ */
19
+ needsValue?: boolean;
20
+ /**
21
+ * Whether to receive values from the start (default true). If false, nothing is watched until setVisible(true) is
22
+ * called (so that only widgets visible on the screen receive values).
23
+ */
24
+ visible?: boolean;
25
+ }
26
+ /** Current state of one variable (the value of VariableBinding.state). */
27
+ export interface BoundVariable {
28
+ /** Which variable. */
29
+ readonly ref: VariableRef;
30
+ /** Definition of the variable (undefined if it is not in the current run). */
31
+ readonly info: VariableInfo | undefined;
32
+ /** The last value (undefined if none yet, or if values are not received (visible is false)). */
33
+ readonly reading: VariableReading | undefined;
34
+ /**
35
+ * Whether the ECU uses the variable now (`unknown` if the variable is not in the current run). Information only: it
36
+ * does not enter condition or block, and a disconnection does not change it.
37
+ */
38
+ readonly availability: VariableAvailability;
39
+ /** State of the value (stale, none yet, the value at the paused time and so on). */
40
+ readonly condition: ValueCondition;
41
+ /** Why the variable cannot be written now (null if it can). */
42
+ readonly block: WriteBlock | null;
43
+ /**
44
+ * When block is `launcher`: the launcher's reason, as it wrote it in `allowed.write` of its `state` (`code`, such as
45
+ * `scenario-running`, and one English sentence). null otherwise.
46
+ */
47
+ readonly launcherReason: Reason | null;
48
+ /**
49
+ * Status of the last write to this variable (in the current run). Every bind of the same variable sees the same
50
+ * status.
51
+ */
52
+ readonly writeStatus: WriteStatus;
53
+ /**
54
+ * The value sent whose result has not arrived yet (null if none). If set, a widget shows it instead of the value
55
+ * read (so a switch that was clicked does not appear to flip back while the result is pending). Cleared when the run
56
+ * changes.
57
+ */
58
+ readonly pending: WriteValue | null;
59
+ }
60
+ /**
61
+ * Binding to one variable. Call close() when done with it.
62
+ *
63
+ * ```js
64
+ * const throttle = client.variables.bind({ peer: 'Engine', name: 'engine.throttle' }, { needsRange: true });
65
+ * throttle.state.subscribe(({ reading, pending, block }) => {
66
+ * slider.disabled = block !== null;
67
+ * slider.value = pending ?? reading?.value ?? 0;
68
+ * });
69
+ * slider.onchange = () => throttle.write(Number(slider.value));
70
+ * ```
71
+ */
72
+ export interface VariableBinding {
73
+ /** Which variable. */
74
+ readonly ref: VariableRef;
75
+ /**
76
+ * The current state. get() always builds it from the current state (it does not depend on the order of subscribers).
77
+ */
78
+ readonly state: Readable<BoundVariable>;
79
+ /**
80
+ * History (oldest first; only when BindOptions.history asks for it and values are being received; empty otherwise). A
81
+ * new array on every call.
82
+ */
83
+ history(): HistoryPoint[];
84
+ /**
85
+ * Writes. Does nothing when the variable cannot be written (block of state). If the server has not received the
86
+ * previous write yet, the value is held instead of sent, and once the server receives it only the last value is sent
87
+ * (so dragging a slider or repeating a key does not pile up writes; at that point it checks again that the run is the
88
+ * same and the variable can still be written, and if not, nothing is sent and writeStatus is `dropped`). A write
89
+ * whose result never came is not resent. The result appears in writeStatus of state (no Promise is returned).
90
+ */
91
+ write(value: WriteValue): void;
92
+ /**
93
+ * Shows an error the widget found before sending (not a number, out of range and so on) as the write status
94
+ * (`invalid`). Leaves alone the write being sent, the write waiting for its result and the next value held.
95
+ */
96
+ reject(message: string): void;
97
+ /**
98
+ * Whether to receive values (widgets that watch only while visible call it whenever they show or hide). While false,
99
+ * reading is undefined.
100
+ */
101
+ setVisible(visible: boolean): void;
102
+ /** Stops (cleans up the watch and the subscriptions). write, reject and setVisible do nothing afterwards. */
103
+ close(): void;
104
+ }
105
+ /** Text used when a held value was not sent (dropped of WriteStatus). */
106
+ export declare const DROPPED_MESSAGE: string;
107
+ /** What bind reads (the client's state). */
108
+ export interface BindingSources {
109
+ readonly variables: {
110
+ readonly state: Readable<VariablesState>;
111
+ watch(variables: readonly VariableRef[], options?: WatchOptions): VariableWatch;
112
+ write(writes: Record<string, Record<string, number | boolean | string>>, options?: WriteOptions): Promise<OperationView>;
113
+ };
114
+ readonly connection: Readable<ConnectionState>;
115
+ readonly session: Readable<SessionView | null>;
116
+ readonly onError: ErrorSink;
117
+ }
118
+ /**
119
+ * The inside of bind: lookup of variable definitions, and write statuses. One per client. Binding the same variable in
120
+ * several places never makes writes overlap, and recreating a bind does not lose the in-flight or failure status.
121
+ * Statuses are per run and start empty when the run changes (a previous run's "applied" or "value sent" is never
122
+ * shown for a new run).
123
+ */
124
+ export declare class VariableBinder {
125
+ readonly sources: BindingSources;
126
+ private entries;
127
+ private entriesSession;
128
+ /** Per variable, the functions to call when the write status changes (the binds listen). */
129
+ private readonly listeners;
130
+ /** Lookup table of the list (rebuilt when peers change). */
131
+ private tableOf;
132
+ private table;
133
+ private connectedPeers;
134
+ constructor(sources: BindingSources);
135
+ /** Binds to a variable (ClientVariables.bind). */
136
+ bind(ref: VariableRef, options?: BindOptions): VariableBinding;
137
+ private lookUp;
138
+ /** Definition of the variable (undefined if it is not in the current run). */
139
+ find(ref: VariableRef): VariableInfo | undefined;
140
+ /** Whether that participant is connected to the Control Server now. */
141
+ peerConnected(peer: string): boolean;
142
+ /** Whether the connection between the client and the GUI server is open. */
143
+ connected(): boolean;
144
+ /**
145
+ * Why the variable cannot be written now (null if it can). Only the widget-independent reasons (Binding adds range
146
+ * and value).
147
+ */
148
+ block(ref: VariableRef): WriteBlock | null;
149
+ /**
150
+ * Why the launcher does not take a write now (launcherAnswer of its last `state`; `unsupported` until a `state` says).
151
+ * The client keeps no rule of its own about it.
152
+ */
153
+ launcherReason(): Reason | null;
154
+ /** Status of the last write to that variable in the current run. */
155
+ status(ref: VariableRef): WriteStatus;
156
+ /** The value sent whose result has not arrived yet (null if none). */
157
+ pending(ref: VariableRef): WriteValue | null;
158
+ /** Called when the write status of that variable changes. Call the returned function to stop. */
159
+ onChange(ref: VariableRef, listener: () => void): () => void;
160
+ private changed;
161
+ /**
162
+ * Writes. Does nothing when the variable cannot be written. If the server has not received the previous write yet,
163
+ * the value is held instead of sent, and once the server receives it only the last value is sent (at that point it
164
+ * checks again that the run is the same and the variable can still be written). A write whose result never came is
165
+ * not resent.
166
+ */
167
+ write(ref: VariableRef, value: WriteValue): void;
168
+ /**
169
+ * Shows an error the widget found before sending (not a number, out of range and so on) as the write status. Leaves
170
+ * alone the write being sent, the write waiting for its result and the next value held (the result of a write
171
+ * already sent no longer appears in the status field, but the value-sent display is cleared when it settles; if the
172
+ * held value is sent later, the status field follows that write).
173
+ */
174
+ reject(ref: VariableRef, message: string): void;
175
+ /**
176
+ * Status of that variable in the current run (undefined without a run). When the run changes, the previous run's
177
+ * statuses are dropped.
178
+ */
179
+ private entry;
180
+ private send;
181
+ }
@@ -0,0 +1,134 @@
1
+ import type { VariableInfo, VariableReading, VariablesStatus } from './variables.js';
2
+ /**
3
+ * State of the value (decided by valueCondition).
4
+ * - unavailable: the variable API is unavailable (not running, this run has no variables, connecting, cannot connect).
5
+ * - missing: the variable is not in the current run.
6
+ * - waiting: in the current run, but no value has arrived yet.
7
+ * - live: the last value while running.
8
+ * - paused: the value at the paused time (includes the values of steps that began before that time).
9
+ * - stale: a value that may be out of date (why: the screen lost its connection to the server, the server is
10
+ * reconnecting to the launcher, the ECU disconnected, or reading values keeps failing).
11
+ * - ended: the last value read in a run that has ended.
12
+ */
13
+ export type ValueCondition = {
14
+ kind: 'unavailable';
15
+ status: VariablesStatus;
16
+ } | {
17
+ kind: 'missing';
18
+ } | {
19
+ kind: 'waiting';
20
+ } | {
21
+ kind: 'live';
22
+ reading: VariableReading;
23
+ } | {
24
+ kind: 'paused';
25
+ reading: VariableReading;
26
+ } | {
27
+ kind: 'stale';
28
+ reading: VariableReading;
29
+ why: 'connection' | 'reconnecting' | 'peer' | 'reading';
30
+ } | {
31
+ kind: 'ended';
32
+ reading: VariableReading;
33
+ };
34
+ /** Input of valueCondition. */
35
+ export interface ConditionInput {
36
+ /** State of the variable API (the status of client.variables.state). */
37
+ status: VariablesStatus;
38
+ /** Whether the variable is in the list of the current run. */
39
+ known: boolean;
40
+ /** The last value (undefined if none). */
41
+ reading: VariableReading | undefined;
42
+ /** Whether the connection between the client and the GUI server is open. */
43
+ connected: boolean;
44
+ /** Whether that ECU is connected to the Control Server. */
45
+ peerConnected: boolean;
46
+ /**
47
+ * Reading values keeps failing (the status stays ready; the summary's reason says why). The values received are the
48
+ * last ones read.
49
+ */
50
+ readFailing?: boolean;
51
+ }
52
+ /** Decides the state of the value. */
53
+ export declare function valueCondition(input: ConditionInput): ValueCondition;
54
+ /** Whether two value states are the same (kind, value, reason). */
55
+ export declare function sameCondition(a: ValueCondition, b: ValueCondition): boolean;
56
+ /**
57
+ * Why the variable cannot be written now (null if it can). In this order: the facts of the variable and the run first,
58
+ * then the launcher's answer, then the client's prediction (parameter), then what the widget needs.
59
+ * - read-only: a read-only variable (role output).
60
+ * - disconnected: the connection between the client and the GUI server is down.
61
+ * - not-ready: the variable API is unavailable (not running, connecting, cannot connect).
62
+ * - ended: the run has ended.
63
+ * - missing: the variable is not in the current run.
64
+ * - peer-disconnected: that ECU is not connected to the Control Server.
65
+ * - launcher: the launcher does not take a write now (`allowed.write` of its `state`, for example `scenario-running`
66
+ * while a scenario plays), or has not said yet that it does (no `state` yet: `unsupported`). Its reason, code and
67
+ * sentence, is BoundVariable.launcherReason (launcherAnswer of session-state.ts).
68
+ * - parameter: a parameter can only be written before the first step (the client's prediction of the ECU's rule: the
69
+ * launcher does not say it for each variable).
70
+ * - no-range: a widget that needs a range (slider) is bound to a variable without one (min and max)
71
+ * (BindOptions.needsRange).
72
+ * - no-value: there is no value yet, so a widget that moves from the value (slider) cannot move
73
+ * (BindOptions.needsValue).
74
+ */
75
+ export type WriteBlock = 'read-only' | 'disconnected' | 'not-ready' | 'ended' | 'missing' | 'peer-disconnected' | 'launcher' | 'parameter' | 'no-range' | 'no-value';
76
+ /** Input of writeBlock. */
77
+ export interface WriteBlockInput {
78
+ info: VariableInfo | undefined;
79
+ status: VariablesStatus;
80
+ connected: boolean;
81
+ /** Whether a run started paused is waiting before the first step (isHolding() of the session: `holding` of the launcher's state). */
82
+ beforeStart: boolean;
83
+ /** Whether that ECU is connected to the Control Server. */
84
+ peerConnected: boolean;
85
+ /** Whether the launcher does not take a write now, or has not said that it does (launcherAnswer(state, 'write')). */
86
+ launcherRefuses: boolean;
87
+ }
88
+ /**
89
+ * Decides why the variable cannot be written now (null if it can). Only the reasons that do not depend on the widget:
90
+ * client.variables.bind adds no-range and no-value.
91
+ */
92
+ export declare function writeBlock(input: WriteBlockInput): WriteBlock | null;
93
+ /** Value to write: a boolean for bool, a number for numbers, an item's value (number) or name for enums. */
94
+ export type WriteValue = number | boolean | string;
95
+ /**
96
+ * Write status (per variable and per run; that of the last write sent).
97
+ * - idle: nothing written.
98
+ * - sending: sent; waiting for the server's answer.
99
+ * - queued: the server accepted it (operation number seq). Waiting to be applied in an ECU step.
100
+ * - applied: applied (atMs: virtual time of the step that applied it).
101
+ * - invalid: the widget refused it before sending (wrong shape or out of range; the text passed to
102
+ * VariableBinding.reject). Not sent.
103
+ * - dropped: a value held while waiting for the previous write was not sent, because writing became impossible (the
104
+ * run ended, the ECU disconnected and so on).
105
+ * - refused, failed, no-answer, closed: the kind of the WriteError (the server refused, it was not applied, no answer
106
+ * came, closed). message is the English text from the server or the library.
107
+ */
108
+ export type WriteStatus = {
109
+ kind: 'idle';
110
+ } | {
111
+ kind: 'sending';
112
+ } | {
113
+ kind: 'queued';
114
+ seq: number;
115
+ } | {
116
+ kind: 'applied';
117
+ atMs: number | null;
118
+ } | {
119
+ kind: 'invalid';
120
+ message: string;
121
+ } | {
122
+ kind: 'dropped';
123
+ message: string;
124
+ } | {
125
+ kind: 'refused' | 'failed' | 'no-answer' | 'closed';
126
+ message: string;
127
+ code: string | null;
128
+ };
129
+ /** Whether the write status is one that went wrong. */
130
+ export declare function isWriteError(status: WriteStatus): status is Extract<WriteStatus, {
131
+ message: string;
132
+ }>;
133
+ /** Whether the range is set (both ends finite, the lower below the upper). */
134
+ export declare function hasRange(info: Pick<VariableInfo, 'min' | 'max'>): boolean;