@vestra-player/sdk 1.6.22 → 1.7.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.
- package/CHANGELOG.md +53 -0
- package/bin/vestra-plugin.mjs +34 -12
- package/dist/api-requests.d.ts +22 -0
- package/dist/api-requests.js +38 -0
- package/dist/capability-check.d.ts +48 -0
- package/dist/capability-check.js +219 -51
- package/dist/capability-rules.d.ts +21 -0
- package/dist/capability-rules.js +32 -1
- package/dist/commands.d.ts +8 -0
- package/dist/commands.js +11 -11
- package/dist/docs-tree.json.gz +0 -0
- package/dist/plugin/commands.js +43 -13
- package/dist/plugin/connection.d.ts +37 -0
- package/dist/plugin/connection.js +34 -0
- package/dist/plugin/index.js +12 -2
- package/dist/plugin/media.js +3 -1
- package/dist/plugin/packages.js +3 -1
- package/dist/plugin/passes.d.ts +38 -1
- package/dist/plugin/passes.js +89 -5
- package/dist/plugin/player/events.d.ts +9 -0
- package/dist/plugin/player/events.js +43 -0
- package/dist/plugin/player/rules.d.ts +7 -0
- package/dist/plugin/player/rules.js +11 -0
- package/dist/plugin/player.d.ts +37 -4
- package/dist/plugin/player.js +85 -12
- package/dist/plugin/resolvers.js +2 -3
- package/dist/plugin/runtime.d.ts +2 -3
- package/dist/plugin/runtime.js +8 -2
- package/dist/plugin/scope.d.ts +9 -0
- package/dist/plugin/scope.js +24 -0
- package/dist/plugin/services.js +4 -5
- package/dist/plugin/state.d.ts +3 -0
- package/dist/plugin/state.js +81 -0
- package/dist/plugin/stream.d.ts +27 -3
- package/dist/plugin/testing/core-actions.js +1 -1
- package/dist/plugin/testing/services.js +1 -1
- package/dist/plugin/types/base.d.ts +78 -2
- package/dist/plugin/types/commands.d.ts +17 -1
- package/dist/plugin/types/playback.d.ts +156 -1
- package/dist/plugin/types/plugin-wire.d.ts +589 -0
- package/dist/plugin/types/plugin-wire.js +1 -0
- package/dist/plugin/types/ui-content.d.ts +23 -4
- package/dist/plugin/types/ui-shell.d.ts +1 -0
- package/dist/plugin/types.d.ts +4 -2
- package/dist/plugin/view/base.d.ts +10 -3
- package/dist/plugin/view/base.js +30 -6
- package/dist/plugin/view/binder.js +8 -1
- package/dist/plugin/view/writer.d.ts +49 -4
- package/dist/plugin/view/writer.js +383 -17
- package/dist/plugin/wire-facade.d.ts +4 -0
- package/dist/plugin/wire-facade.js +190 -0
- package/dist/ts-plugin.d.ts +3 -0
- package/dist/ts-plugin.js +18 -6
- package/dist/wire.d.ts +233 -20
- package/llms.txt +3 -3
- package/package.json +1 -1
- package/slint/vestra/focus.slint +33 -0
|
@@ -7,6 +7,27 @@
|
|
|
7
7
|
* the back-off after a failed write. It knows nothing of update passes or
|
|
8
8
|
* bindings: the binder (`binder.ts`, L2) puts values in, the view
|
|
9
9
|
* (`base.ts`) says where a write goes.
|
|
10
|
+
*
|
|
11
|
+
* Two modes (SDK 1.7.0, refactor Step 2). Until the view's first `bind()`
|
|
12
|
+
* (`track()`), the writer serves `set()` alone: each value is sent whole
|
|
13
|
+
* (limits checked, large arrays in parts with `more`, one write in
|
|
14
|
+
* flight per view), only the properties `set()` named are looked at, and
|
|
15
|
+
* no row-patch base is kept, so nothing is diffed. Once tracked it is the
|
|
16
|
+
* binder's: every value is compared with what was sent and arrays become
|
|
17
|
+
* row patches where one applies; a `set()` then takes the same path, so
|
|
18
|
+
* mixed use stays consistent. Patch planning stays here, used only in the
|
|
19
|
+
* tracked mode: it needs what was sent and the player's base, which are
|
|
20
|
+
* this writer's, and moving it above would split one property's write
|
|
21
|
+
* state across two layers.
|
|
22
|
+
*
|
|
23
|
+
* A third track (SDK 1.7.0, refactor Step 3): properties written directly
|
|
24
|
+
* through `plugin.wire.view(v)` (`direct`, `directRows`). Such a property
|
|
25
|
+
* is the plugin's alone: its `PropertyWrite`s and `RowOp`s go out as
|
|
26
|
+
* given, with only one write in flight per property, a `seq` when the
|
|
27
|
+
* player takes patches, and large writes in parts; nothing is encoded,
|
|
28
|
+
* compared or checked against the limits. One track per property: a
|
|
29
|
+
* property written directly cannot be `set()` or bound, and the other
|
|
30
|
+
* way round (`invalid`), so `#sent` and `#bases` never go stale.
|
|
10
31
|
*/
|
|
11
32
|
import { HostError } from "../../wire.js";
|
|
12
33
|
import { MarkdownHandle } from "../markdown.js";
|
|
@@ -252,6 +273,24 @@ function rowPatch(sent, next, rows) {
|
|
|
252
273
|
});
|
|
253
274
|
return { ops, counts };
|
|
254
275
|
}
|
|
276
|
+
/**
|
|
277
|
+
* How `plugin.wire.view(v)` (`wire-facade.ts`) reaches a view's writer
|
|
278
|
+
* without importing the view classes (which load the binder): a view
|
|
279
|
+
* answers this key with its {@link DirectWrites}, given a scope of the
|
|
280
|
+
* plugin that made it.
|
|
281
|
+
*/
|
|
282
|
+
export const DIRECT = Symbol("direct writes");
|
|
283
|
+
/** The message of a write on the other track of a property. */
|
|
284
|
+
export const bothTracks = (property, written) => written
|
|
285
|
+
? `property \`${property}\` is written directly; bind it or write it, not both`
|
|
286
|
+
: `property \`${property}\` is set or bound; bind it or write it, not both`;
|
|
287
|
+
/** Row operations `chunkOps` can split: the four shapes, rows in arrays. */
|
|
288
|
+
const wellFormed = (ops) => ops.every((op) => op !== null &&
|
|
289
|
+
typeof op === "object" &&
|
|
290
|
+
(op.op === "set" ||
|
|
291
|
+
op.op === "remove" ||
|
|
292
|
+
op.op === "move" ||
|
|
293
|
+
(op.op === "insert" && Array.isArray(op.rows))));
|
|
255
294
|
/**
|
|
256
295
|
* One view's properties: their values now, what the player shows as far as
|
|
257
296
|
* this side knows, and the writes that bring the one to the other.
|
|
@@ -284,9 +323,43 @@ export class ViewWriter {
|
|
|
284
323
|
#broken = false;
|
|
285
324
|
#retry;
|
|
286
325
|
#flushing = false;
|
|
326
|
+
/**
|
|
327
|
+
* The view was bound (`track()`): every value is compared with what was
|
|
328
|
+
* sent and arrays are patched. Before, only `set()` values are sent,
|
|
329
|
+
* whole, and no patch base is kept.
|
|
330
|
+
*/
|
|
331
|
+
#tracked = false;
|
|
332
|
+
/**
|
|
333
|
+
* Initial arrays `wired` cut to one request's size, by key: how many
|
|
334
|
+
* rows went (until `shown`).
|
|
335
|
+
*/
|
|
336
|
+
#cut = new Map();
|
|
337
|
+
/**
|
|
338
|
+
* Not bound: initial arrays the view shows the first rows of, by key,
|
|
339
|
+
* whose next write inserts only the rest (SDK 1.7.0).
|
|
340
|
+
*/
|
|
341
|
+
#rest = new Map();
|
|
342
|
+
/** Properties written directly (`plugin.wire.view(v)`, SDK 1.7.0). */
|
|
343
|
+
#direct = new Map();
|
|
344
|
+
/** Direct properties a binding produced: reported once each. */
|
|
345
|
+
#refused = new Set();
|
|
287
346
|
constructor(target) {
|
|
288
347
|
this.#target = target;
|
|
289
348
|
}
|
|
349
|
+
/**
|
|
350
|
+
* The view's first `bind()`: compare and patch from now on. What was
|
|
351
|
+
* sent before is known as whole values, so the first patchable write of
|
|
352
|
+
* an array goes whole and patches follow it.
|
|
353
|
+
*/
|
|
354
|
+
track() {
|
|
355
|
+
this.#tracked = true;
|
|
356
|
+
// Bound values go the bound way, compared with what was sent.
|
|
357
|
+
this.#rest.clear();
|
|
358
|
+
}
|
|
359
|
+
/** Row patches apply: the player takes them and the view is bound. */
|
|
360
|
+
get #patching() {
|
|
361
|
+
return this.#tracked && this.#patches;
|
|
362
|
+
}
|
|
290
363
|
/** The player takes row patches (and `seq` on whole writes). */
|
|
291
364
|
get #patches() {
|
|
292
365
|
return this.#target.patches();
|
|
@@ -332,6 +405,7 @@ export class ViewWriter {
|
|
|
332
405
|
}
|
|
333
406
|
const count = room > 0 ? prefixWithin(sizes, room) : 0;
|
|
334
407
|
wire = wire.slice(0, count);
|
|
408
|
+
this.#cut.set(key, count);
|
|
335
409
|
room -= sizes.slice(0, count).reduce((a, b) => a + b, 0);
|
|
336
410
|
}
|
|
337
411
|
else
|
|
@@ -341,19 +415,224 @@ export class ViewWriter {
|
|
|
341
415
|
}
|
|
342
416
|
return out;
|
|
343
417
|
}
|
|
344
|
-
/**
|
|
418
|
+
/**
|
|
419
|
+
* `View.set`: each value is sent once even if equal to what was sent. A
|
|
420
|
+
* property written directly is refused (`invalid`), before anything is
|
|
421
|
+
* kept.
|
|
422
|
+
*/
|
|
345
423
|
set(props) {
|
|
346
|
-
|
|
424
|
+
const values = Object.entries(encode(props));
|
|
425
|
+
for (const [key] of values)
|
|
426
|
+
if (this.#direct.has(key))
|
|
427
|
+
throw failure("invalid", bothTracks(key, true));
|
|
428
|
+
for (const [key, value] of values) {
|
|
347
429
|
this.#values.set(key, value);
|
|
348
430
|
this.#forced.add(key);
|
|
349
431
|
this.#failed.delete(key); // an explicit write is tried again (after any back-off)
|
|
432
|
+
this.#rest.delete(key); // a new value: no longer the initial rows' rest
|
|
350
433
|
}
|
|
351
434
|
this.flush();
|
|
352
435
|
}
|
|
353
|
-
/**
|
|
436
|
+
/**
|
|
437
|
+
* A bound value (Slint name): sent by the next flush if it changed. A
|
|
438
|
+
* property written directly is refused (`invalid`, the first time; the
|
|
439
|
+
* binder reports it) and the value is never kept.
|
|
440
|
+
*/
|
|
354
441
|
assign(key, value) {
|
|
442
|
+
if (this.#direct.has(key)) {
|
|
443
|
+
if (this.#refused.has(key))
|
|
444
|
+
return;
|
|
445
|
+
this.#refused.add(key);
|
|
446
|
+
throw failure("invalid", bothTracks(key, true));
|
|
447
|
+
}
|
|
355
448
|
this.#values.set(key, value);
|
|
356
449
|
}
|
|
450
|
+
/**
|
|
451
|
+
* `plugin.wire.view(v).write`: one property's `PropertyWrite`, sent as
|
|
452
|
+
* given after the property's earlier direct writes (a whole value drops
|
|
453
|
+
* those still waiting, which resolve `false`). With a player that takes
|
|
454
|
+
* row patches, a whole value without `seq` gets one, so that row
|
|
455
|
+
* operations can follow it, and an array larger than one request goes in
|
|
456
|
+
* parts (`more`). The property is marked: `set()` and `bind()` refuse it
|
|
457
|
+
* from now on. Rejects with `unavailable` while the view does not show.
|
|
458
|
+
*/
|
|
459
|
+
direct(property, write) {
|
|
460
|
+
const key = this.#directKey(property);
|
|
461
|
+
if (write === null ||
|
|
462
|
+
typeof write !== "object" ||
|
|
463
|
+
!("value" in write || "patch" in write))
|
|
464
|
+
throw failure("invalid", "write takes a PropertyWrite: { value } or { patch: { base, seq, ops } }");
|
|
465
|
+
return this.#queueDirect(key, { kind: "write", write }, !("patch" in write));
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* `plugin.wire.view(v).writeRows`: row operations on the property's last
|
|
469
|
+
* direct write (or on its initial value), in as many patches as one
|
|
470
|
+
* request carries, each applying on the one before. Needs a player that
|
|
471
|
+
* takes row patches (`unsupported` otherwise).
|
|
472
|
+
*/
|
|
473
|
+
directRows(property, ops) {
|
|
474
|
+
const key = this.#directKey(property);
|
|
475
|
+
if (!Array.isArray(ops))
|
|
476
|
+
throw failure("invalid", "writeRows takes an array of row operations");
|
|
477
|
+
return this.#queueDirect(key, { kind: "rows", ops: [...ops] }, false);
|
|
478
|
+
}
|
|
479
|
+
/** A direct write's property (Slint name), refused when `set()` or bound. */
|
|
480
|
+
#directKey(property) {
|
|
481
|
+
if (typeof property !== "string" || property === "")
|
|
482
|
+
throw failure("invalid", "a property name is a non-empty string");
|
|
483
|
+
const key = kebab(property);
|
|
484
|
+
if (this.#values.has(key))
|
|
485
|
+
throw failure("invalid", bothTracks(key, false));
|
|
486
|
+
return key;
|
|
487
|
+
}
|
|
488
|
+
#queueDirect(key, job, whole) {
|
|
489
|
+
if (!this.#target.live())
|
|
490
|
+
return Promise.reject(failure("unavailable", `${key}: the view does not show (a window before show(), a panel or settings page before ready, or closed)`));
|
|
491
|
+
let track = this.#direct.get(key);
|
|
492
|
+
if (!track)
|
|
493
|
+
this.#direct.set(key, (track = { base: undefined, queue: [], running: false }));
|
|
494
|
+
const queued = track;
|
|
495
|
+
return new Promise((resolve, reject) => {
|
|
496
|
+
if (whole)
|
|
497
|
+
for (const older of queued.queue.splice(0))
|
|
498
|
+
older.settle(false);
|
|
499
|
+
queued.queue.push({
|
|
500
|
+
...job,
|
|
501
|
+
settle: (ok, error) => error === undefined ? resolve(ok) : reject(error),
|
|
502
|
+
});
|
|
503
|
+
if (!queued.running)
|
|
504
|
+
void this.#drainDirect(key, queued);
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
/** Send one property's direct writes in order, one in flight. */
|
|
508
|
+
async #drainDirect(key, track) {
|
|
509
|
+
track.running = true;
|
|
510
|
+
try {
|
|
511
|
+
let job;
|
|
512
|
+
while ((job = track.queue.shift())) {
|
|
513
|
+
try {
|
|
514
|
+
if (!this.#target.live())
|
|
515
|
+
throw new Abandoned();
|
|
516
|
+
if (job.kind === "rows")
|
|
517
|
+
await this.#sendRows(key, track, job.ops);
|
|
518
|
+
else
|
|
519
|
+
await this.#sendDirect(key, track, job.write);
|
|
520
|
+
job.settle(true);
|
|
521
|
+
}
|
|
522
|
+
catch (error) {
|
|
523
|
+
job.settle(false, error instanceof Abandoned
|
|
524
|
+
? failure("unavailable", `${key}: the view closed or showed its initial properties again during the write`)
|
|
525
|
+
: error);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
finally {
|
|
530
|
+
track.running = false;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* One direct write, sent while the view shows what it showed when the
|
|
535
|
+
* job started (`generation`): a failure after the view closed or showed
|
|
536
|
+
* its initial properties again is not the write's ({@link Abandoned}, so
|
|
537
|
+
* `unavailable`), and `true` says the player took it for the view that
|
|
538
|
+
* is shown now, so its `seq` may serve as a base.
|
|
539
|
+
*/
|
|
540
|
+
async #writeNow(key, write, generation) {
|
|
541
|
+
if (!this.#target.live() || this.#generation !== generation)
|
|
542
|
+
throw new Abandoned();
|
|
543
|
+
try {
|
|
544
|
+
await this.#target.write(key, write);
|
|
545
|
+
}
|
|
546
|
+
catch (error) {
|
|
547
|
+
if (!this.#target.live() || this.#generation !== generation)
|
|
548
|
+
throw new Abandoned();
|
|
549
|
+
throw error;
|
|
550
|
+
}
|
|
551
|
+
return this.#target.live() && this.#generation === generation;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* A direct `PropertyWrite`: as given, with a `seq` and in parts where it
|
|
555
|
+
* applies. A part the player answers after the view closed or showed its
|
|
556
|
+
* initial properties again was not taken for the view shown now
|
|
557
|
+
* ({@link Abandoned}, so `unavailable`, as for row operations).
|
|
558
|
+
*/
|
|
559
|
+
async #sendDirect(key, track, write) {
|
|
560
|
+
const generation = this.#generation;
|
|
561
|
+
const send = async (part) => {
|
|
562
|
+
if (!(await this.#writeNow(key, part, generation)))
|
|
563
|
+
throw new Abandoned();
|
|
564
|
+
};
|
|
565
|
+
if ("patch" in write) {
|
|
566
|
+
await send(write);
|
|
567
|
+
const seq = write.patch?.seq;
|
|
568
|
+
if (typeof seq === "number")
|
|
569
|
+
track.base = seq;
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
if (!this.#patches) {
|
|
573
|
+
await send(write);
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
const seq = typeof write.seq === "number" ? write.seq : ++this.#seq;
|
|
577
|
+
const value = write.value;
|
|
578
|
+
if (Array.isArray(value) &&
|
|
579
|
+
write.more === undefined &&
|
|
580
|
+
!JSON.stringify(value).includes(MARKDOWN_KEY)) {
|
|
581
|
+
const sizes = value.map(rowBytes);
|
|
582
|
+
const count = prefixWithin(sizes, CHUNK_BYTES);
|
|
583
|
+
// In parts only when each row fits one request; otherwise whole, for
|
|
584
|
+
// the player to take or refuse.
|
|
585
|
+
if (count > 0 &&
|
|
586
|
+
count < value.length &&
|
|
587
|
+
sizes.every((size) => size <= CHUNK_BYTES)) {
|
|
588
|
+
await send({ value: value.slice(0, count), seq, more: true });
|
|
589
|
+
let last = seq;
|
|
590
|
+
const insert = {
|
|
591
|
+
op: "insert",
|
|
592
|
+
at: count,
|
|
593
|
+
rows: value.slice(count),
|
|
594
|
+
};
|
|
595
|
+
const groups = chunkOps([insert]);
|
|
596
|
+
for (const [i, ops] of groups.entries()) {
|
|
597
|
+
const next = ++this.#seq;
|
|
598
|
+
await send({
|
|
599
|
+
patch: { base: last, seq: next, ops },
|
|
600
|
+
...(i < groups.length - 1 ? { more: true } : {}),
|
|
601
|
+
});
|
|
602
|
+
last = next;
|
|
603
|
+
}
|
|
604
|
+
track.base = last;
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
// A base only for the view that is shown now: after a reopen the
|
|
609
|
+
// player shows the property's default, not this write.
|
|
610
|
+
await send({ ...write, seq });
|
|
611
|
+
track.base = seq;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Direct row operations: patches on the property's last direct write
|
|
615
|
+
* since the view showed its initial properties. Without one the player
|
|
616
|
+
* has no rows of the property's own to apply them on (a direct property
|
|
617
|
+
* is never an initial property): refused with `stale` before anything is
|
|
618
|
+
* sent, as the player would refuse it.
|
|
619
|
+
*/
|
|
620
|
+
async #sendRows(key, track, ops) {
|
|
621
|
+
if (!this.#patches)
|
|
622
|
+
throw failure("unsupported", `${key}: row operations need a player that takes row patches (player 0.6.0)`);
|
|
623
|
+
if (track.base === undefined)
|
|
624
|
+
throw failure("stale", `${key}: no whole value was written directly since the view showed its initial properties; write the whole value first`);
|
|
625
|
+
const generation = this.#generation;
|
|
626
|
+
// Malformed operations go in one patch, for the player to refuse.
|
|
627
|
+
const groups = wellFormed(ops) ? chunkOps(ops) : [[...ops]];
|
|
628
|
+
for (const group of groups) {
|
|
629
|
+
const seq = ++this.#seq;
|
|
630
|
+
const current = await this.#writeNow(key, { patch: { base: track.base, seq, ops: group } }, generation);
|
|
631
|
+
if (!current)
|
|
632
|
+
throw new Abandoned();
|
|
633
|
+
track.base = seq;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
357
636
|
/** Every property's value now, by Slint name. */
|
|
358
637
|
values() {
|
|
359
638
|
return Object.fromEntries(this.#values);
|
|
@@ -361,13 +640,43 @@ export class ViewWriter {
|
|
|
361
640
|
/** The host already shows these values (e.g. sent as initial properties). */
|
|
362
641
|
shown(values) {
|
|
363
642
|
this.#generation++;
|
|
364
|
-
const patches = this.#
|
|
643
|
+
const patches = this.#patching;
|
|
365
644
|
this.#sent = new Map(Object.entries(values).map(([k, v]) => [k, sentOf(v, patches)]));
|
|
366
645
|
this.#bases = new Map(patches ? Object.keys(values).map((key) => [key, 0]) : []);
|
|
367
646
|
this.#stales.clear();
|
|
368
647
|
this.#forced.clear();
|
|
369
648
|
for (const key of Object.keys(values))
|
|
370
649
|
this.#failed.delete(key);
|
|
650
|
+
// A property written directly shows its initial value again: its
|
|
651
|
+
// next patch applies on 0.
|
|
652
|
+
for (const track of this.#direct.values())
|
|
653
|
+
track.base = undefined;
|
|
654
|
+
// Not bound, a flush sends only what is marked: mark every value the
|
|
655
|
+
// view does not show as it is now, such as a `set()` made while the view
|
|
656
|
+
// was opening (after its initial properties were taken) or an initial
|
|
657
|
+
// array cut to one request's size (`wired`), whose rest goes now:
|
|
658
|
+
// only the rest, inserted on the initial rows (base 0), when the value
|
|
659
|
+
// still begins with them (`#rest`).
|
|
660
|
+
const cut = new Map(this.#cut);
|
|
661
|
+
this.#cut.clear();
|
|
662
|
+
this.#rest.clear();
|
|
663
|
+
if (!this.#tracked)
|
|
664
|
+
for (const [key, value] of this.#values) {
|
|
665
|
+
if (!this.#encode(key, value).changed)
|
|
666
|
+
continue;
|
|
667
|
+
this.#forced.add(key);
|
|
668
|
+
const count = cut.get(key);
|
|
669
|
+
const shown = values[key];
|
|
670
|
+
if (count !== undefined &&
|
|
671
|
+
this.#patches &&
|
|
672
|
+
Array.isArray(value) &&
|
|
673
|
+
Array.isArray(shown) &&
|
|
674
|
+
shown.length === count &&
|
|
675
|
+
count < value.length)
|
|
676
|
+
// The rows shown, as encoded: the value is compared with them
|
|
677
|
+
// when it is sent (`#send`), not now; it may change before.
|
|
678
|
+
this.#rest.set(key, { count, enc: shown.map(encodeRow) });
|
|
679
|
+
}
|
|
371
680
|
}
|
|
372
681
|
/** `value` encoded, and whether it differs from what was sent. */
|
|
373
682
|
#encode(key, value) {
|
|
@@ -398,21 +707,26 @@ export class ViewWriter {
|
|
|
398
707
|
// Keys only for a write: an unchanged array costs its encoding alone.
|
|
399
708
|
let keys;
|
|
400
709
|
const keyed = () => (keys === undefined ? (keys = keyOf(value)) : keys);
|
|
710
|
+
// Not bound: remembered as its JSON, for a later bind() to compare
|
|
711
|
+
// with; no rows or keys are kept until patches can follow.
|
|
712
|
+
const tracked = this.#tracked;
|
|
401
713
|
return {
|
|
402
714
|
key,
|
|
403
715
|
value,
|
|
404
716
|
changed,
|
|
405
717
|
signature,
|
|
406
718
|
rows: { ...encoded, keyed },
|
|
407
|
-
sent: (counts = []) =>
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
719
|
+
sent: (counts = []) => tracked
|
|
720
|
+
? {
|
|
721
|
+
kind: "rows",
|
|
722
|
+
rows: {
|
|
723
|
+
enc,
|
|
724
|
+
field: keyed()?.field ?? null,
|
|
725
|
+
keys: keyed()?.keys ?? null,
|
|
726
|
+
counts,
|
|
727
|
+
},
|
|
728
|
+
}
|
|
729
|
+
: { kind: "json", enc: signature() },
|
|
416
730
|
};
|
|
417
731
|
}
|
|
418
732
|
const enc = JSON.stringify(value);
|
|
@@ -440,6 +754,46 @@ export class ViewWriter {
|
|
|
440
754
|
return pending.sent();
|
|
441
755
|
}
|
|
442
756
|
const generation = this.#generation;
|
|
757
|
+
// An initial list cut to one request's size, not bound: the player shows
|
|
758
|
+
// its first `rest` rows (the initial properties, base 0); only the
|
|
759
|
+
// others go, as insert patches each applying on the one before.
|
|
760
|
+
// Only while the value still begins with those rows, as encoded now
|
|
761
|
+
// (a later `set()`, a deferred flush); a `stale` refusal of the first
|
|
762
|
+
// patch falls back to the whole value below.
|
|
763
|
+
const rest = this.#rest.get(key);
|
|
764
|
+
this.#rest.delete(key);
|
|
765
|
+
if (rest !== undefined &&
|
|
766
|
+
Array.isArray(value) &&
|
|
767
|
+
pending.rows &&
|
|
768
|
+
!pending.rows.markdown &&
|
|
769
|
+
rest.count < value.length &&
|
|
770
|
+
sameRows(pending.rows.enc.slice(0, rest.count), rest.enc)) {
|
|
771
|
+
const insert = {
|
|
772
|
+
op: "insert",
|
|
773
|
+
at: rest.count,
|
|
774
|
+
rows: value
|
|
775
|
+
.slice(rest.count)
|
|
776
|
+
.map((row) => wireValue(row ?? null)),
|
|
777
|
+
};
|
|
778
|
+
tooLarge(key, [insert]);
|
|
779
|
+
const [first, ...others] = chunkOps([insert]);
|
|
780
|
+
const seq = ++this.#seq;
|
|
781
|
+
let applied = false;
|
|
782
|
+
try {
|
|
783
|
+
await this.#target.write(key, {
|
|
784
|
+
patch: { base: 0, seq, ops: first },
|
|
785
|
+
});
|
|
786
|
+
applied = true;
|
|
787
|
+
}
|
|
788
|
+
catch (error) {
|
|
789
|
+
if (!(error instanceof HostError && error.code === "stale"))
|
|
790
|
+
throw error;
|
|
791
|
+
}
|
|
792
|
+
if (applied) {
|
|
793
|
+
await this.#parts(key, seq, others, false, generation);
|
|
794
|
+
return pending.sent();
|
|
795
|
+
}
|
|
796
|
+
}
|
|
443
797
|
const base = this.#bases.get(key);
|
|
444
798
|
this.#bases.delete(key);
|
|
445
799
|
const sent = this.#sent.get(key);
|
|
@@ -521,7 +875,9 @@ export class ViewWriter {
|
|
|
521
875
|
}
|
|
522
876
|
}
|
|
523
877
|
await this.#target.write(key, { value: wireValue(value), seq });
|
|
524
|
-
|
|
878
|
+
// A base for the next patch, once the view is bound.
|
|
879
|
+
if (this.#tracked)
|
|
880
|
+
this.#bases.set(key, seq);
|
|
525
881
|
return pending.sent();
|
|
526
882
|
}
|
|
527
883
|
/**
|
|
@@ -552,7 +908,8 @@ export class ViewWriter {
|
|
|
552
908
|
}
|
|
553
909
|
last = seq;
|
|
554
910
|
}
|
|
555
|
-
this.#
|
|
911
|
+
if (this.#tracked)
|
|
912
|
+
this.#bases.set(key, last);
|
|
556
913
|
}
|
|
557
914
|
/**
|
|
558
915
|
* Send what changed. A bound value is sent when it differs from the last one
|
|
@@ -575,7 +932,13 @@ export class ViewWriter {
|
|
|
575
932
|
let pending;
|
|
576
933
|
do {
|
|
577
934
|
pending = [];
|
|
578
|
-
|
|
935
|
+
// Not bound: only what `set()` named is sent (whole), nothing is
|
|
936
|
+
// compared. Bound: every value against what was sent.
|
|
937
|
+
const keys = this.#tracked ? this.#values.keys() : [...this.#forced];
|
|
938
|
+
for (const key of keys) {
|
|
939
|
+
if (!this.#values.has(key))
|
|
940
|
+
continue;
|
|
941
|
+
const value = this.#values.get(key);
|
|
579
942
|
const encoded = this.#encode(key, value);
|
|
580
943
|
const failedAs = this.#failed.get(key);
|
|
581
944
|
if (failedAs !== undefined && failedAs === encoded.signature())
|
|
@@ -627,8 +990,11 @@ export class ViewWriter {
|
|
|
627
990
|
this.#target.report(failure);
|
|
628
991
|
})();
|
|
629
992
|
}
|
|
630
|
-
/** The view closed: no retry after a back-off. */
|
|
993
|
+
/** The view closed: no retry after a back-off, no direct write waiting. */
|
|
631
994
|
stop() {
|
|
632
995
|
clearTimeout(this.#retry);
|
|
996
|
+
for (const track of this.#direct.values())
|
|
997
|
+
for (const job of track.queue.splice(0))
|
|
998
|
+
job.settle(false);
|
|
633
999
|
}
|
|
634
1000
|
}
|