@vehicle-sil/gui-client 0.5.0 → 0.6.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.
@@ -1,6 +1,8 @@
1
+ import type { ConnectionState } from './connection.js';
1
2
  import { HttpTransport } from './http.js';
2
3
  import { Open } from './protocol.js';
3
4
  import type { VariableAvailability } from './protocol.js';
5
+ import { type SessionView } from './session-state.js';
4
6
  import { ErrorSink, Readable } from './values.js';
5
7
  import type { BindOptions, VariableBinding } from './variable-binding.js';
6
8
  /**
@@ -21,6 +23,8 @@ export interface VariableInfo {
21
23
  role: Open<'input' | 'output' | 'parameter'>;
22
24
  /** Whether it can be written (role is input or parameter). */
23
25
  writable: boolean;
26
+ /** Whether vehicle-sil added the variable (`ecu.connected`, `peer.*`, `j1939.*`, `can.*`, `lin.*`, `eth.*`) rather than the ECU application declaring it. */
27
+ builtin: boolean;
24
28
  unit: string;
25
29
  min: number | null;
26
30
  max: number | null;
@@ -155,6 +159,26 @@ export interface OperationView {
155
159
  done: boolean;
156
160
  peers: Record<string, OperationPeer>;
157
161
  }
162
+ /** Options of waiting for a queued write to settle (`queued.applied(options)`). */
163
+ export interface WriteWaitOptions {
164
+ /**
165
+ * Watchdog timeout budget (ms; default WRITE_TIMEOUT_MS). Must be a finite number greater than 0.
166
+ * Suspended while healthy and paused/holding before all target ECUs settle; scaled by slow playback speed and
167
+ * target ECU step period while advancing towards due; bounded across disconnect/non-sync or past due.
168
+ */
169
+ timeoutMs?: number;
170
+ /** Aborts the wait (`WriteError('no-answer', ..., 'aborted', lastKnownOperation)`). */
171
+ signal?: AbortSignal;
172
+ }
173
+ /**
174
+ * Stages 1+2 result of `client.variables.enqueue`: the server accepted the write and every target ECU has queued it
175
+ * (or settled as `applied`). Call `applied()` for stages 3+4 (waiting for all target ECUs to apply it).
176
+ */
177
+ export interface QueuedOperation {
178
+ readonly session: string;
179
+ readonly seq: number;
180
+ applied(options?: WriteWaitOptions): Promise<OperationView>;
181
+ }
158
182
  /** Options of a write. */
159
183
  export interface WriteOptions {
160
184
  /** Optional server instance identifier to reject stale writes across server restarts. */
@@ -164,54 +188,46 @@ export interface WriteOptions {
164
188
  /** If the requested time cannot be met: reject (default; not applied) or apply (applied in the next step). */
165
189
  late?: 'reject' | 'apply';
166
190
  /**
167
- * How long to wait for the result (whether it was applied) (ms; default WRITE_TIMEOUT_MS). While paused, and for a
168
- * write at a later time (atMs), the write does not settle until the simulation gets there, so make this longer or
169
- * follow the result in operations. `Infinity` never times out (waits until the run changes or close()). Values
170
- * longer than about 24.8 days (2^31-1 ms) are capped there. A non-number or a value of 0 or less means the default.
171
- * Counted from the HTTP answer.
191
+ * Watchdog timeout budget for waiting on the applied result (ms; default WRITE_TIMEOUT_MS). Must be a finite number
192
+ * greater than 0. Values longer than about 24.8 days (2^31-1 ms) are capped there.
172
193
  */
173
194
  timeoutMs?: number;
174
- /**
175
- * Called once when the server receives the write and returns the operation number (before the result; not called
176
- * when refused or when no answer came). The number lets you follow the in-progress states (queued and so on) in
177
- * `operations`, so a widget can show "accepted, waiting to be applied" or know that it may send the next write.
178
- */
179
- onAccepted?: (operation: {
180
- session: string;
181
- seq: number;
182
- }) => void;
183
195
  /**
184
196
  * Forwarded to the HTTP transport (`HttpRequest.keepalive` -> `fetch(..., { keepalive })`). Set `true` for a
185
197
  * release write sent from a browser `pagehide` handler so the request can finish as the page unloads.
186
198
  */
187
199
  keepalive?: boolean;
200
+ /** Aborts the write or wait (`WriteError`). */
201
+ signal?: AbortSignal;
188
202
  }
189
203
  /**
190
204
  * A write that went wrong.
191
205
  * - refused: the server did not accept it (wrong type or range, unknown variable, no run and so on; code names the
192
206
  * error). It was not sent, so you may fix it and send it again.
193
- * - failed: accepted but not applied (operation has the state and reason per participant).
207
+ * - failed: accepted but not applied (code names the settled state; operation has the state and reason per participant).
194
208
  * - no-answer: unknown whether it was applied. The result did not come within the timeout (operation has the last
195
- * known state), there was no HTTP answer, the server failed midway (500), the Control Server's answer could not be
196
- * waited for (code `write-timeout`) or the connection broke midway (`write-failed`), the run changed before the
197
- * answer, the write settled while reconnecting and its result was lost (not in the summary's list and older than
198
- * the boundary operations_from_seq), or the server could not keep all unsettled operations and stopped following it.
199
- * **Sending it again as is may write twice**: check operations and the value first.
209
+ * known state), there was no HTTP answer, the server failed midway (500), the ECUs did not queue it in time
210
+ * (code `write-timeout`) or the connection broke midway (`write-failed`), the wait was aborted (`aborted`), the run
211
+ * changed before the answer, the write settled while reconnecting and its result was lost (not in the summary's list
212
+ * and older than the boundary operations_from_seq), or the server could not keep all unsettled operations and stopped
213
+ * following it. **Sending it again as is may write twice**: check operations and the value first.
200
214
  * - closed: the client was closed (unknown whether it was applied).
201
215
  */
202
216
  export declare class WriteError extends Error {
203
217
  readonly kind: 'refused' | 'failed' | 'no-answer' | 'closed';
204
218
  /**
205
- * Name of the server's error (for refused: `unknown-variable`, `not-writable`, `invalid-field` and so on; for
206
- * no-answer: `write-timeout`, `write-failed`; null unless it is an HTTP error).
219
+ * Name of the error (for refused: `unknown-variable`, `not-writable`, `invalid-field`, `unknown-field`,
220
+ * `invalid-argument`, `aborted` and so on; for failed: `invalid`, `busy`, `late`, `rejected`, `cancelled`,
221
+ * `unknown`, `mixed`; for no-answer: `write-timeout`, `write-failed`, `aborted`, or null).
207
222
  */
208
223
  readonly code: string | null;
209
224
  /** The operation (once its number is known). */
210
225
  readonly operation: OperationView | null;
211
226
  constructor(kind: 'refused' | 'failed' | 'no-answer' | 'closed', message: string,
212
227
  /**
213
- * Name of the server's error (for refused: `unknown-variable`, `not-writable`, `invalid-field` and so on; for
214
- * no-answer: `write-timeout`, `write-failed`; null unless it is an HTTP error).
228
+ * Name of the error (for refused: `unknown-variable`, `not-writable`, `invalid-field`, `unknown-field`,
229
+ * `invalid-argument`, `aborted` and so on; for failed: `invalid`, `busy`, `late`, `rejected`, `cancelled`,
230
+ * `unknown`, `mixed`; for no-answer: `write-timeout`, `write-failed`, `aborted`, or null).
215
231
  */
216
232
  code?: string | null,
217
233
  /** The operation (once its number is known). */
@@ -240,11 +256,14 @@ export interface ClientVariables {
240
256
  */
241
257
  watch(variables: readonly VariableRef[] | 'all', options?: WatchOptions): VariableWatch;
242
258
  /**
243
- * Writes variables: `write({Engine: {'engine.throttle': 50}})`. Sent to the current run (session of state); the
244
- * server refuses if that run is not running (refused, code session-changed / not-running; if the run is not known
245
- * yet, nothing is sent and the error is not-running). For an enum, give the value (number) or the item's name.
246
- * Resolves with the operation once applied. Otherwise rejects with a WriteError (tell the cases apart by kind). The
247
- * same write is never resent automatically.
259
+ * Sends a write and resolves with `QueuedOperation` once the server accepted it and every target ECU has queued it
260
+ * (stages 1+2). Rejects immediately with `WriteError` if refused, unanswered, or already terminally failed.
261
+ */
262
+ enqueue(writes: Record<string, Record<string, number | boolean | string>>, options?: WriteOptions): Promise<QueuedOperation>;
263
+ /**
264
+ * Writes variables: `write({Engine: {'engine.throttle': 50}})`. Sugar for
265
+ * `enqueue(writes, options).then(q => q.applied(...))`: resolves with the operation once applied; rejects with a
266
+ * `WriteError` otherwise. The same write is never resent automatically.
248
267
  */
249
268
  write(writes: Record<string, Record<string, number | boolean | string>>, options?: WriteOptions): Promise<OperationView>;
250
269
  /**
@@ -256,6 +275,19 @@ export interface ClientVariables {
256
275
  */
257
276
  bind(ref: VariableRef, options?: BindOptions): VariableBinding;
258
277
  }
278
+ /** Internal hook letting `FrontVariable` release its in-flight token even when `queued.applied()` is never called. */
279
+ export declare const ON_SETTLED: unique symbol;
280
+ export interface InternalQueuedOperation extends QueuedOperation {
281
+ [ON_SETTLED]?(listener: (outcome: OperationView | WriteError) => void): () => void;
282
+ }
283
+ /** Clock and link signals driving the four-mode write watchdog. */
284
+ export interface WriteClockSource {
285
+ readonly connection: Readable<ConnectionState>;
286
+ readonly ready: Readable<boolean>;
287
+ readonly rejected: Readable<boolean>;
288
+ readonly incompatible: Readable<string | null>;
289
+ readonly session: Readable<SessionView | null>;
290
+ }
259
291
  /** Default time to wait for the result of a write (ms). */
260
292
  export declare const WRITE_TIMEOUT_MS = 10000;
261
293
  /** Limit on the number of history points (per variable). */
@@ -276,6 +308,13 @@ export declare const WATCH_PERIOD_MS = 100;
276
308
  * subscriptions (the server's limit is 16 per connection).
277
309
  */
278
310
  export declare const WATCH_PERIODS_MS: readonly [20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 60000];
311
+ /** Shortest supported watch period (ms). */
312
+ export declare const WATCH_PERIOD_MS_MIN: 20;
313
+ /** Longest supported watch period (ms). */
314
+ export declare const WATCH_PERIOD_MS_MAX: 100 | 1000 | 5000 | 500 | 20 | 60000 | 2000 | 200 | 10000 | 50;
315
+ export declare function isAbortSignalLike(value: unknown): value is AbortSignal;
316
+ export declare function validateWriteOptions(options: unknown): WriteError | null;
317
+ export declare function validateWriteWaitOptions(options: unknown): WriteError | null;
279
318
  /** Reads `variables` of a summary. Unreadable parts are skipped as if absent (additions are compatible). */
280
319
  export declare function readVariables(value: unknown, session: string | null): VariablesState;
281
320
  /** Reads an `operation` notification (or one entry of operations in a summary). null if unreadable. */
@@ -288,16 +327,6 @@ export interface VariablesLink {
288
327
  }
289
328
  /**
290
329
  * ECU variables: list and state, value watches, writes and their results. No framework needed.
291
- *
292
- * - List and state come from `variables` in hello and from `variables` notifications (`session` notifications do not
293
- * carry them). When the run changes, they are unknown until that run's `variables` notification arrives.
294
- * - Each variable goes into the subscription with the shortest interval requested for it (one subscription per
295
- * interval). A changed subscription is resent with the same id (the server replaces it); after a reconnect, resume()
296
- * resends them all.
297
- * - Per variable, keeps the last value and as many history points as requested (ring buffer). Dropped when the run
298
- * changes.
299
- * - Write results arrive in `operation` notifications. They can arrive before the HTTP answer (the operation number),
300
- * so recent operations are kept and matched once the number is known.
301
330
  */
302
331
  export declare class VariablesFeed implements Omit<ClientVariables, 'bind'> {
303
332
  private readonly link;
@@ -305,8 +334,11 @@ export declare class VariablesFeed implements Omit<ClientVariables, 'bind'> {
305
334
  private readonly onError;
306
335
  /** Limits (from limits of api/app; the defaults until then). */
307
336
  private readonly limits;
337
+ private readonly clock?;
338
+ private readonly defaultWatchPeriodMs;
308
339
  private instanceId;
309
340
  private epoch;
341
+ private lastValuesSimMs;
310
342
  private readonly stateValue;
311
343
  private readonly operationsValue;
312
344
  private readonly handles;
@@ -315,7 +347,8 @@ export declare class VariablesFeed implements Omit<ClientVariables, 'bind'> {
315
347
  /** Cached keys kept across a reconnect until a post-reconnect `values` sample arrives. */
316
348
  private readonly awaitingRefresh;
317
349
  private readonly histories;
318
- private readonly pending;
350
+ private readonly inFlight;
351
+ private readonly trackers;
319
352
  /** Variables someone watches (null: a watch observes all). */
320
353
  private watched;
321
354
  /** History points requested by watches of all variables (the largest request). */
@@ -336,7 +369,7 @@ export declare class VariablesFeed implements Omit<ClientVariables, 'bind'> {
336
369
  watch_period_ms_min?: number;
337
370
  watch_period_ms_max?: number;
338
371
  watch_variables?: number;
339
- });
372
+ }, clock?: WriteClockSource | undefined, defaultWatchPeriodMs?: number);
340
373
  /** Messages sent right after connecting (after authentication): all current subscriptions. */
341
374
  resume(): unknown[];
342
375
  /** The client was closed: fail the pending writes (including those waiting for the HTTP answer). Watches stay. */
@@ -363,9 +396,8 @@ export declare class VariablesFeed implements Omit<ClientVariables, 'bind'> {
363
396
  */
364
397
  private forgetLost;
365
398
  /**
366
- * Switched to another run: drop the previous run's values, histories and operations (histories not requested by name
367
- * are dropped with their memory, so they do not keep growing as the variables change from run to run). Writes
368
- * waiting for the previous run's result are failed, since the result will not come.
399
+ * Switched to another run: drop the previous run's values, histories and operations. Writes waiting for the previous
400
+ * run's result are failed, since the result will not come.
369
401
  */
370
402
  private enter;
371
403
  private prepareEnter;
@@ -373,15 +405,25 @@ export declare class VariablesFeed implements Omit<ClientVariables, 'bind'> {
373
405
  /** Rebuilds values of the watches that observe the changed variables (all watches if touched is null). */
374
406
  private publish;
375
407
  private valuesOf;
408
+ private findOperation;
376
409
  private withOperation;
377
- /**
378
- * Replaces the list of operations and clears the writes whose result has settled. `fresh` holds the operations that
379
- * just arrived (a batch of notifications, the list in a summary): the list is cut to OPERATIONS_KEPT entries, so
380
- * settled operations cut from the list are picked up from here.
381
- */
382
410
  private setOperations;
383
411
  private prepareOperations;
384
- private settle;
412
+ private settleTracker;
413
+ private cleanupWaiter;
414
+ private finishWaiter;
415
+ private currentSimMs;
416
+ private evaluateAllWaiters;
417
+ private evaluateWaiter;
418
+ private waitForApplied;
419
+ enqueue(writes: Record<string, Record<string, number | boolean | string>>, options?: WriteOptions): Promise<QueuedOperation>;
420
+ /**
421
+ * Internal path for `VariableBinder`: invokes `onQueued(seq)` synchronously when the HTTP 200 answer arrives so
422
+ * synchronous widget state (`BoundVariable.writeStatus` / held-value dispatch) transitions in the same turn, then
423
+ * waits on the same four-mode clock-aware watchdog (`waitForApplied`).
424
+ */
425
+ enqueueForBinder(writes: Record<string, Record<string, number | boolean | string>>, onQueued: (seq: number) => void): Promise<OperationView>;
426
+ private enqueueInternal;
385
427
  write(writes: Record<string, Record<string, number | boolean | string>>, options?: WriteOptions): Promise<OperationView>;
386
428
  watch(variables: readonly VariableRef[] | 'all', options?: WatchOptions): VariableWatch;
387
429
  isStale(ref: VariableRef): boolean;