@vehicle-sil/gui-client 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.ja.md +547 -0
- package/README.md +546 -2
- package/dist/index.js +3681 -0
- package/package.json +21 -4
- package/types/client.d.ts +112 -0
- package/types/connection.d.ts +155 -0
- package/types/http.d.ts +71 -0
- package/types/index.d.ts +24 -0
- package/types/protocol.d.ts +977 -0
- package/types/run.d.ts +74 -0
- package/types/session-state.d.ts +255 -0
- package/types/session.d.ts +118 -0
- package/types/time.d.ts +162 -0
- package/types/token.d.ts +35 -0
- package/types/urls.d.ts +8 -0
- package/types/values.d.ts +54 -0
- package/types/variable-binding.d.ts +167 -0
- package/types/variable-state.d.ts +128 -0
- package/types/variables.d.ts +362 -0
|
@@ -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,167 @@
|
|
|
1
|
+
import { ConnectionState } from './connection.js';
|
|
2
|
+
import { SessionView } from './session-state.js';
|
|
3
|
+
import { TimeState } from './time.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
|
+
/** State of the value (stale, none yet, the value at the paused time and so on). */
|
|
35
|
+
readonly condition: ValueCondition;
|
|
36
|
+
/** Why the variable cannot be written now (null if it can). */
|
|
37
|
+
readonly block: WriteBlock | null;
|
|
38
|
+
/**
|
|
39
|
+
* Status of the last write to this variable (in the current run). Every bind of the same variable sees the same
|
|
40
|
+
* status.
|
|
41
|
+
*/
|
|
42
|
+
readonly writeStatus: WriteStatus;
|
|
43
|
+
/**
|
|
44
|
+
* The value sent whose result has not arrived yet (null if none). If set, a widget shows it instead of the value
|
|
45
|
+
* read (so a switch that was clicked does not appear to flip back while the result is pending). Cleared when the run
|
|
46
|
+
* changes.
|
|
47
|
+
*/
|
|
48
|
+
readonly pending: WriteValue | null;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Binding to one variable. Call close() when done with it.
|
|
52
|
+
*
|
|
53
|
+
* ```js
|
|
54
|
+
* const throttle = client.variables.bind({ peer: 'Engine', name: 'engine.throttle' }, { needsRange: true });
|
|
55
|
+
* throttle.state.subscribe(({ reading, pending, block }) => {
|
|
56
|
+
* slider.disabled = block !== null;
|
|
57
|
+
* slider.value = pending ?? reading?.value ?? 0;
|
|
58
|
+
* });
|
|
59
|
+
* slider.onchange = () => throttle.write(Number(slider.value));
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export interface VariableBinding {
|
|
63
|
+
/** Which variable. */
|
|
64
|
+
readonly ref: VariableRef;
|
|
65
|
+
/**
|
|
66
|
+
* The current state. get() always builds it from the current state (it does not depend on the order of subscribers).
|
|
67
|
+
*/
|
|
68
|
+
readonly state: Readable<BoundVariable>;
|
|
69
|
+
/**
|
|
70
|
+
* History (oldest first; only when BindOptions.history asks for it and values are being received; empty otherwise). A
|
|
71
|
+
* new array on every call.
|
|
72
|
+
*/
|
|
73
|
+
history(): HistoryPoint[];
|
|
74
|
+
/**
|
|
75
|
+
* Writes. Does nothing when the variable cannot be written (block of state). If the server has not received the
|
|
76
|
+
* previous write yet, the value is held instead of sent, and once the server receives it only the last value is sent
|
|
77
|
+
* (so dragging a slider or repeating a key does not pile up writes; at that point it checks again that the run is the
|
|
78
|
+
* same and the variable can still be written, and if not, nothing is sent and writeStatus is `dropped`). A write
|
|
79
|
+
* whose result never came is not resent. The result appears in writeStatus of state (no Promise is returned).
|
|
80
|
+
*/
|
|
81
|
+
write(value: WriteValue): void;
|
|
82
|
+
/**
|
|
83
|
+
* Shows an error the widget found before sending (not a number, out of range and so on) as the write status
|
|
84
|
+
* (`invalid`). Leaves alone the write being sent, the write waiting for its result and the next value held.
|
|
85
|
+
*/
|
|
86
|
+
reject(message: string): void;
|
|
87
|
+
/**
|
|
88
|
+
* Whether to receive values (widgets that watch only while visible call it whenever they show or hide). While false,
|
|
89
|
+
* reading is undefined.
|
|
90
|
+
*/
|
|
91
|
+
setVisible(visible: boolean): void;
|
|
92
|
+
/** Stops (cleans up the watch and the subscriptions). write, reject and setVisible do nothing afterwards. */
|
|
93
|
+
close(): void;
|
|
94
|
+
}
|
|
95
|
+
/** Text used when a held value was not sent (dropped of WriteStatus). */
|
|
96
|
+
export declare const DROPPED_MESSAGE: string;
|
|
97
|
+
/** What bind reads (the client's state). */
|
|
98
|
+
export interface BindingSources {
|
|
99
|
+
readonly variables: {
|
|
100
|
+
readonly state: Readable<VariablesState>;
|
|
101
|
+
watch(variables: readonly VariableRef[], options?: WatchOptions): VariableWatch;
|
|
102
|
+
write(writes: Record<string, Record<string, number | boolean | string>>, options?: WriteOptions): Promise<OperationView>;
|
|
103
|
+
};
|
|
104
|
+
readonly connection: Readable<ConnectionState>;
|
|
105
|
+
readonly time: Readable<TimeState>;
|
|
106
|
+
readonly session: Readable<SessionView | null>;
|
|
107
|
+
readonly onError: ErrorSink;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The inside of bind: lookup of variable definitions, and write statuses. One per client. Binding the same variable in
|
|
111
|
+
* several places never makes writes overlap, and recreating a bind does not lose the in-flight or failure status.
|
|
112
|
+
* Statuses are per run and start empty when the run changes (a previous run's "applied" or "value sent" is never
|
|
113
|
+
* shown for a new run).
|
|
114
|
+
*/
|
|
115
|
+
export declare class VariableBinder {
|
|
116
|
+
readonly sources: BindingSources;
|
|
117
|
+
private entries;
|
|
118
|
+
private entriesSession;
|
|
119
|
+
/** Per variable, the functions to call when the write status changes (the binds listen). */
|
|
120
|
+
private readonly listeners;
|
|
121
|
+
/** Lookup table of the list (rebuilt when peers change). */
|
|
122
|
+
private tableOf;
|
|
123
|
+
private table;
|
|
124
|
+
private connectedPeers;
|
|
125
|
+
constructor(sources: BindingSources);
|
|
126
|
+
/** Binds to a variable (ClientVariables.bind). */
|
|
127
|
+
bind(ref: VariableRef, options?: BindOptions): VariableBinding;
|
|
128
|
+
private lookUp;
|
|
129
|
+
/** Definition of the variable (undefined if it is not in the current run). */
|
|
130
|
+
find(ref: VariableRef): VariableInfo | undefined;
|
|
131
|
+
/** Whether that participant is connected to the Control Server now. */
|
|
132
|
+
peerConnected(peer: string): boolean;
|
|
133
|
+
/** Whether the connection between the client and the GUI server is open. */
|
|
134
|
+
connected(): boolean;
|
|
135
|
+
/**
|
|
136
|
+
* Why the variable cannot be written now (null if it can). Only the widget-independent reasons (Binding adds
|
|
137
|
+
* scenario, range and value).
|
|
138
|
+
*/
|
|
139
|
+
block(ref: VariableRef): WriteBlock | null;
|
|
140
|
+
/** Status of the last write to that variable in the current run. */
|
|
141
|
+
status(ref: VariableRef): WriteStatus;
|
|
142
|
+
/** The value sent whose result has not arrived yet (null if none). */
|
|
143
|
+
pending(ref: VariableRef): WriteValue | null;
|
|
144
|
+
/** Called when the write status of that variable changes. Call the returned function to stop. */
|
|
145
|
+
onChange(ref: VariableRef, listener: () => void): () => void;
|
|
146
|
+
private changed;
|
|
147
|
+
/**
|
|
148
|
+
* Writes. Does nothing when the variable cannot be written. If the server has not received the previous write yet,
|
|
149
|
+
* the value is held instead of sent, and once the server receives it only the last value is sent (at that point it
|
|
150
|
+
* checks again that the run is the same and the variable can still be written). A write whose result never came is
|
|
151
|
+
* not resent.
|
|
152
|
+
*/
|
|
153
|
+
write(ref: VariableRef, value: WriteValue): void;
|
|
154
|
+
/**
|
|
155
|
+
* Shows an error the widget found before sending (not a number, out of range and so on) as the write status. Leaves
|
|
156
|
+
* alone the write being sent, the write waiting for its result and the next value held (the result of a write
|
|
157
|
+
* already sent no longer appears in the status field, but the value-sent display is cleared when it settles; if the
|
|
158
|
+
* held value is sent later, the status field follows that write).
|
|
159
|
+
*/
|
|
160
|
+
reject(ref: VariableRef, message: string): void;
|
|
161
|
+
/**
|
|
162
|
+
* Status of that variable in the current run (undefined without a run). When the run changes, the previous run's
|
|
163
|
+
* statuses are dropped.
|
|
164
|
+
*/
|
|
165
|
+
private entry;
|
|
166
|
+
private send;
|
|
167
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
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).
|
|
58
|
+
* - read-only: a read-only variable (role output).
|
|
59
|
+
* - disconnected: the connection between the client and the GUI server is down.
|
|
60
|
+
* - not-ready: the variable API is unavailable (not running, connecting, cannot connect).
|
|
61
|
+
* - ended: the run has ended.
|
|
62
|
+
* - missing: the variable is not in the current run.
|
|
63
|
+
* - peer-disconnected: that ECU is not connected to the Control Server.
|
|
64
|
+
* - parameter: a parameter can only be written before the first step.
|
|
65
|
+
* - scenario: a scenario is running (watch only; the launcher refuses writes).
|
|
66
|
+
* - no-range: a widget that needs a range (slider) is bound to a variable without one (min and max)
|
|
67
|
+
* (BindOptions.needsRange).
|
|
68
|
+
* - no-value: there is no value yet, so a widget that moves from the value (slider) cannot move
|
|
69
|
+
* (BindOptions.needsValue).
|
|
70
|
+
*/
|
|
71
|
+
export type WriteBlock = 'read-only' | 'disconnected' | 'not-ready' | 'ended' | 'missing' | 'peer-disconnected' | 'parameter' | 'no-range' | 'no-value' | 'scenario';
|
|
72
|
+
/** Input of writeBlock. */
|
|
73
|
+
export interface WriteBlockInput {
|
|
74
|
+
info: VariableInfo | undefined;
|
|
75
|
+
status: VariablesStatus;
|
|
76
|
+
connected: boolean;
|
|
77
|
+
/** Whether a run started paused is waiting before the first step (beforeStart of client.time.state). */
|
|
78
|
+
beforeStart: boolean;
|
|
79
|
+
/** Whether that ECU is connected to the Control Server. */
|
|
80
|
+
peerConnected: boolean;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Decides why the variable cannot be written now (null if it can). Only the reasons that do not depend on the widget:
|
|
84
|
+
* client.variables.bind adds scenario, no-range and no-value.
|
|
85
|
+
*/
|
|
86
|
+
export declare function writeBlock(input: WriteBlockInput): WriteBlock | null;
|
|
87
|
+
/** Value to write: a boolean for bool, a number for numbers, an item's value (number) or name for enums. */
|
|
88
|
+
export type WriteValue = number | boolean | string;
|
|
89
|
+
/**
|
|
90
|
+
* Write status (per variable and per run; that of the last write sent).
|
|
91
|
+
* - idle: nothing written.
|
|
92
|
+
* - sending: sent; waiting for the server's answer.
|
|
93
|
+
* - queued: the server accepted it (operation number seq). Waiting to be applied in an ECU step.
|
|
94
|
+
* - applied: applied (atMs: virtual time of the step that applied it).
|
|
95
|
+
* - invalid: the widget refused it before sending (wrong shape or out of range; the text passed to
|
|
96
|
+
* VariableBinding.reject). Not sent.
|
|
97
|
+
* - dropped: a value held while waiting for the previous write was not sent, because writing became impossible (the
|
|
98
|
+
* run ended, the ECU disconnected and so on).
|
|
99
|
+
* - refused, failed, no-answer, closed: the kind of the WriteError (the server refused, it was not applied, no answer
|
|
100
|
+
* came, closed). message is the English text from the server or the library.
|
|
101
|
+
*/
|
|
102
|
+
export type WriteStatus = {
|
|
103
|
+
kind: 'idle';
|
|
104
|
+
} | {
|
|
105
|
+
kind: 'sending';
|
|
106
|
+
} | {
|
|
107
|
+
kind: 'queued';
|
|
108
|
+
seq: number;
|
|
109
|
+
} | {
|
|
110
|
+
kind: 'applied';
|
|
111
|
+
atMs: number | null;
|
|
112
|
+
} | {
|
|
113
|
+
kind: 'invalid';
|
|
114
|
+
message: string;
|
|
115
|
+
} | {
|
|
116
|
+
kind: 'dropped';
|
|
117
|
+
message: string;
|
|
118
|
+
} | {
|
|
119
|
+
kind: 'refused' | 'failed' | 'no-answer' | 'closed';
|
|
120
|
+
message: string;
|
|
121
|
+
code: string | null;
|
|
122
|
+
};
|
|
123
|
+
/** Whether the write status is one that went wrong. */
|
|
124
|
+
export declare function isWriteError(status: WriteStatus): status is Extract<WriteStatus, {
|
|
125
|
+
message: string;
|
|
126
|
+
}>;
|
|
127
|
+
/** Whether the range is set (both ends finite, the lower below the upper). */
|
|
128
|
+
export declare function hasRange(info: Pick<VariableInfo, 'min' | 'max'>): boolean;
|