@vestra-player/sdk 1.6.22 → 1.7.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.
Files changed (56) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/bin/vestra-plugin.mjs +34 -12
  3. package/dist/api-requests.d.ts +22 -0
  4. package/dist/api-requests.js +38 -0
  5. package/dist/capability-check.d.ts +48 -0
  6. package/dist/capability-check.js +219 -51
  7. package/dist/capability-rules.d.ts +21 -0
  8. package/dist/capability-rules.js +32 -1
  9. package/dist/commands.d.ts +8 -0
  10. package/dist/docs-tree.json.gz +0 -0
  11. package/dist/plugin/commands.js +43 -13
  12. package/dist/plugin/connection.d.ts +37 -0
  13. package/dist/plugin/connection.js +34 -0
  14. package/dist/plugin/index.js +12 -2
  15. package/dist/plugin/media.js +3 -1
  16. package/dist/plugin/packages.js +3 -1
  17. package/dist/plugin/passes.d.ts +38 -1
  18. package/dist/plugin/passes.js +89 -5
  19. package/dist/plugin/player/events.d.ts +9 -0
  20. package/dist/plugin/player/events.js +43 -0
  21. package/dist/plugin/player/rules.d.ts +7 -0
  22. package/dist/plugin/player/rules.js +11 -0
  23. package/dist/plugin/player.d.ts +37 -4
  24. package/dist/plugin/player.js +85 -12
  25. package/dist/plugin/resolvers.js +2 -3
  26. package/dist/plugin/runtime.d.ts +2 -3
  27. package/dist/plugin/runtime.js +8 -2
  28. package/dist/plugin/scope.d.ts +9 -0
  29. package/dist/plugin/scope.js +24 -0
  30. package/dist/plugin/services.js +4 -5
  31. package/dist/plugin/state.d.ts +3 -0
  32. package/dist/plugin/state.js +81 -0
  33. package/dist/plugin/stream.d.ts +27 -3
  34. package/dist/plugin/testing/core-actions.js +1 -1
  35. package/dist/plugin/testing/services.js +1 -1
  36. package/dist/plugin/types/base.d.ts +78 -2
  37. package/dist/plugin/types/commands.d.ts +17 -1
  38. package/dist/plugin/types/playback.d.ts +156 -1
  39. package/dist/plugin/types/plugin-wire.d.ts +589 -0
  40. package/dist/plugin/types/plugin-wire.js +1 -0
  41. package/dist/plugin/types/ui-content.d.ts +23 -4
  42. package/dist/plugin/types/ui-shell.d.ts +1 -0
  43. package/dist/plugin/types.d.ts +4 -2
  44. package/dist/plugin/view/base.d.ts +10 -3
  45. package/dist/plugin/view/base.js +30 -6
  46. package/dist/plugin/view/binder.js +8 -1
  47. package/dist/plugin/view/writer.d.ts +49 -4
  48. package/dist/plugin/view/writer.js +383 -17
  49. package/dist/plugin/wire-facade.d.ts +4 -0
  50. package/dist/plugin/wire-facade.js +190 -0
  51. package/dist/ts-plugin.d.ts +3 -0
  52. package/dist/ts-plugin.js +18 -6
  53. package/dist/wire.d.ts +233 -20
  54. package/llms.txt +3 -3
  55. package/package.json +1 -1
  56. package/slint/vestra/focus.slint +33 -0
@@ -11,13 +11,23 @@ app,
11
11
  /** `TestHost` sets `strictDeclarations`; the player's runner does not. */
12
12
  options) {
13
13
  const runtime = new Runtime(transport, manifest, directory, dataDirectory, app, options);
14
- // Player, window and timeline state are synchronous for plugins: read them first.
14
+ // Player, window and timeline state are synchronous for plugins: read them
15
+ // first. With `events` the copy of the player's state follows the events
16
+ // from the start, subscribed before the read, as before 1.7.0: a read in
17
+ // any later handler is current. Without `events` nothing can be
18
+ // subscribed, and the copy starts on its first use. Window and timeline
19
+ // subscribe on their first use.
20
+ const player = playerOf(runtime);
21
+ if (runtime.can("events"))
22
+ void player.start(false);
15
23
  await Promise.all([
16
- playerOf(runtime).init(),
24
+ player.init(),
17
25
  windowOf(runtime).init(),
18
26
  timelineOf(runtime).init(),
19
27
  ]);
20
28
  const root = new ScopeImpl(runtime);
29
+ // A runaway of the update passes goes to the plugin's error handlers.
30
+ runtime.passes.report = (error) => root.report(error);
21
31
  // Failures the plugin did not ask about: a property the player could not
22
32
  // show when it rebuilt a panel or the Options window (an image removed
23
33
  // since), a callback whose arguments it could not convert, a detached
@@ -253,7 +253,9 @@ export function mediaCapture(scope) {
253
253
  const runtime = scope.runtime;
254
254
  const service = serviceOf(runtime);
255
255
  const player = playerOf(runtime);
256
- const at = options.ifCurrent ?? player.state;
256
+ // The source now: a capture needs one, even from a plugin that never
257
+ // read the state (its copy then starts following the player).
258
+ const at = options.ifCurrent ?? (await player.current());
257
259
  const { expected } = player.expected(at);
258
260
  if (!expected)
259
261
  throw failure("noSource", "no source to capture");
@@ -41,7 +41,9 @@ class ChangeHandle {
41
41
  return;
42
42
  for (const listener of this.#listeners)
43
43
  this.#scope.run(listener, operation.status);
44
- this.#scope.update();
44
+ // `status` changed: bindings that show it are evaluated again (a full
45
+ // pass, through the connection: this file is a primitive).
46
+ this.#scope.runtime.signals.invalidate();
45
47
  }
46
48
  async #poll() {
47
49
  const host = this.#scope.runtime.host.packages;
@@ -21,6 +21,17 @@ export type Change = ReadonlyMap<string, ReadonlySet<string>>;
21
21
  * without `update()`) still shows within about this long.
22
22
  */
23
23
  export declare const SAFETY_PASS_MS = 1000;
24
+ /**
25
+ * The most passes in a row that a pass itself asks for (something it
26
+ * evaluated changed what it reads, `plugin.state` written in a `bind`
27
+ * function). The next one is dropped and reported once: without it such a
28
+ * plugin would spin in microtasks and never yield. It counts passes, not
29
+ * loops: a legitimate chain deeper than this (101 bindings, each writing a
30
+ * `plugin.state` field the next one reads) is cut the same way: the
31
+ * readers of the dropped pass's changes run again only in the next full
32
+ * pass (after a handler, `update()`).
33
+ */
34
+ export declare const MAX_CHAINED_PASSES = 100;
24
35
  /**
25
36
  * Read-only counters of the update passes, for the SDK's own
26
37
  * characterisation tests and benchmark (`test/golden-traces.test.ts`,
@@ -45,13 +56,39 @@ export interface PassCounts {
45
56
  */
46
57
  safetyPasses: number;
47
58
  }
59
+ /**
60
+ * One `Passes.bind` registration as its owner holds it: dispose it, or ask
61
+ * for it alone to be evaluated again ({@link BindingHandle.invalidate}).
62
+ */
63
+ export interface BindingHandle extends Disposable {
64
+ /**
65
+ * Evaluate this binding in the next pass even if nothing it read
66
+ * changed (a precise pass, not a full one): its owner changed something
67
+ * the binding reads that no state copy tracks (`Command.set`).
68
+ */
69
+ invalidate(): void;
70
+ }
48
71
  /** The update pass scheduler of one plugin (`Runtime` delegates to it). */
49
72
  export declare class Passes {
50
73
  #private;
74
+ /** Where a runaway goes: the plugin's error handlers (`createPlugin`). */
75
+ report: (error: unknown) => void;
51
76
  /** Pass counters, read by the SDK's own tests and benchmark ({@link PassCounts}). */
52
77
  readonly counts: PassCounts;
53
78
  constructor(connection: Connection);
54
- bind(evaluate: () => void): Disposable;
79
+ /**
80
+ * Register a binding: `evaluate` runs in a full pass now and in every
81
+ * pass that concerns it. Only what calls this takes part in the passes:
82
+ * a view on its first `bind()`, a command with a function field, a
83
+ * sequence provider (SDK 1.7.0; before, every view and command).
84
+ */
85
+ bind(evaluate: () => void, slot?: number): BindingHandle;
86
+ /**
87
+ * A place in the evaluation order, for a binding made later (`bind`): a
88
+ * view or a command reserves one when it is made, so it is evaluated in
89
+ * the order of making whenever it joins.
90
+ */
91
+ slot(): number;
55
92
  /**
56
93
  * A service's state for `event` is read: a binding evaluated now depends
57
94
  * on it, so the event counts as listened to while the binding reads it.
@@ -1,3 +1,4 @@
1
+ import { failure } from "./errors.js";
1
2
  /**
2
3
  * The state each event keeps current, and whether `plugin.when` can say
3
4
  * the same without the plugin running.
@@ -47,12 +48,28 @@ function merge(into, from) {
47
48
  * without `update()`) still shows within about this long.
48
49
  */
49
50
  export const SAFETY_PASS_MS = 1000;
51
+ /**
52
+ * The most passes in a row that a pass itself asks for (something it
53
+ * evaluated changed what it reads, `plugin.state` written in a `bind`
54
+ * function). The next one is dropped and reported once: without it such a
55
+ * plugin would spin in microtasks and never yield. It counts passes, not
56
+ * loops: a legitimate chain deeper than this (101 bindings, each writing a
57
+ * `plugin.state` field the next one reads) is cut the same way: the
58
+ * readers of the dropped pass's changes run again only in the next full
59
+ * pass (after a handler, `update()`).
60
+ */
61
+ export const MAX_CHAINED_PASSES = 100;
50
62
  /** The update pass scheduler of one plugin (`Runtime` delegates to it). */
51
63
  export class Passes {
52
64
  /** The plugin's connection: holds the events a binding reads. */
53
65
  #connection;
54
- /** Each binding with the events whose state it read last time (held). */
66
+ /**
67
+ * Each binding with the events whose state it read last time (held), in
68
+ * the order of their slots (`slot`): the order they are evaluated in.
69
+ */
55
70
  #bindings = new Map();
71
+ /** The last slot handed out (`slot`). */
72
+ #slots = 0;
56
73
  /** The events read by the binding being evaluated, with who read them. */
57
74
  #reads;
58
75
  /** Who reads now (a command's field and where it was added). */
@@ -62,6 +79,8 @@ export class Passes {
62
79
  #full = false;
63
80
  /** State copy changes since the last pass (a precise pass, unless `#full`). */
64
81
  #pending = new Map();
82
+ /** Bindings their owner asked to evaluate in the next pass (`BindingHandle.invalidate`). */
83
+ #dirty = new Set();
65
84
  /** The change the running precise pass is for; `undefined` in a full pass. */
66
85
  #pass;
67
86
  /** The state copy fields read by what is being evaluated now. */
@@ -74,6 +93,16 @@ export class Passes {
74
93
  #skipped = false;
75
94
  /** The plugin was disposed: no safety pass is armed any more. */
76
95
  #stopped = false;
96
+ /** A pass is evaluating bindings now. */
97
+ #running = false;
98
+ /** The scheduled pass was asked for while a pass ran. */
99
+ #nested = false;
100
+ /** Passes in a row that the pass before asked for (`MAX_CHAINED_PASSES`). */
101
+ #chain = 0;
102
+ /** The runaway was reported (once per plugin). */
103
+ #reportedChain = false;
104
+ /** Where a runaway goes: the plugin's error handlers (`createPlugin`). */
105
+ report = (error) => console.error(`[${this.#connection.manifest.id}]`, error);
77
106
  /** Pass counters, read by the SDK's own tests and benchmark ({@link PassCounts}). */
78
107
  counts = {
79
108
  passes: 0,
@@ -86,22 +115,48 @@ export class Passes {
86
115
  constructor(connection) {
87
116
  this.#connection = connection;
88
117
  }
89
- bind(evaluate) {
118
+ /**
119
+ * Register a binding: `evaluate` runs in a full pass now and in every
120
+ * pass that concerns it. Only what calls this takes part in the passes:
121
+ * a view on its first `bind()`, a command with a function field, a
122
+ * sequence provider (SDK 1.7.0; before, every view and command).
123
+ */
124
+ bind(evaluate, slot = this.slot()) {
90
125
  const held = new Map();
91
- const binding = { held, deps: new Map() };
126
+ const binding = { held, deps: new Map(), slot };
127
+ const last = [...this.#bindings.values()].at(-1);
92
128
  this.#bindings.set(evaluate, binding);
129
+ // A slot reserved earlier (a view made before it was bound): evaluated
130
+ // where it was made, as when every view joined the passes when made.
131
+ if (last && last.slot > slot)
132
+ this.#bindings = new Map([...this.#bindings].sort((a, b) => a[1].slot - b[1].slot));
93
133
  this.invalidate();
94
134
  return {
135
+ invalidate: () => {
136
+ if (this.#bindings.get(evaluate) !== binding)
137
+ return;
138
+ this.#dirty.add(binding);
139
+ this.#schedule();
140
+ },
95
141
  dispose: async () => {
96
142
  if (this.#bindings.get(evaluate) !== binding)
97
143
  return;
98
144
  this.#bindings.delete(evaluate);
145
+ this.#dirty.delete(binding);
99
146
  for (const hold of held.values())
100
147
  await hold.dispose();
101
148
  held.clear();
102
149
  },
103
150
  };
104
151
  }
152
+ /**
153
+ * A place in the evaluation order, for a binding made later (`bind`): a
154
+ * view or a command reserves one when it is made, so it is evaluated in
155
+ * the order of making whenever it joins.
156
+ */
157
+ slot() {
158
+ return ++this.#slots;
159
+ }
105
160
  /**
106
161
  * A service's state for `event` is read: a binding evaluated now depends
107
162
  * on it, so the event counts as listened to while the binding reads it.
@@ -281,9 +336,18 @@ export class Passes {
281
336
  this.#schedule();
282
337
  }
283
338
  #schedule() {
339
+ // Nothing takes part in the passes (a plugin of handlers, `set()` and
340
+ // constant commands only): no pass at all. The first `bind` asks for
341
+ // a full pass anyway.
342
+ if (this.#bindings.size === 0) {
343
+ this.#full = false;
344
+ this.#pending.clear();
345
+ return;
346
+ }
284
347
  if (this.#scheduled)
285
348
  return;
286
349
  this.#scheduled = true;
350
+ this.#nested = this.#running;
287
351
  queueMicrotask(() => this.#run());
288
352
  }
289
353
  /**
@@ -293,10 +357,27 @@ export class Passes {
293
357
  */
294
358
  #run() {
295
359
  this.#scheduled = false;
360
+ this.#chain = this.#nested ? this.#chain + 1 : 0;
361
+ this.#nested = false;
362
+ if (this.#chain >= MAX_CHAINED_PASSES) {
363
+ // A loop: each pass changed what it reads. Drop this one; the next
364
+ // change from outside a pass starts afresh.
365
+ this.#chain = 0;
366
+ this.#full = false;
367
+ this.#pending = new Map();
368
+ this.#dirty.clear();
369
+ if (!this.#reportedChain) {
370
+ this.#reportedChain = true;
371
+ this.report(failure("limit", `${MAX_CHAINED_PASSES} update passes in a row each changed what they read (a bind function or a command's state function that writes plugin.state it reads); the next was dropped`));
372
+ }
373
+ return;
374
+ }
296
375
  const full = this.#full;
297
376
  const change = this.#pending;
377
+ const dirty = new Set(this.#dirty);
298
378
  this.#full = false;
299
379
  this.#pending = new Map();
380
+ this.#dirty.clear();
300
381
  this.counts.passes++;
301
382
  if (full)
302
383
  this.counts.fullPasses++;
@@ -305,14 +386,16 @@ export class Passes {
305
386
  this.#safety = undefined;
306
387
  }
307
388
  this.#skipped = false;
389
+ this.#running = true;
308
390
  for (const [evaluate, binding] of [...this.#bindings]) {
309
391
  if (this.#bindings.get(evaluate) !== binding)
310
392
  continue;
311
- if (!full && !affected(binding.deps, change)) {
393
+ if (!full && !dirty.has(binding) && !affected(binding.deps, change)) {
312
394
  this.#skipped = true;
313
395
  continue;
314
396
  }
315
- this.#pass = full ? undefined : change;
397
+ // A binding its owner invalidated is evaluated whole (every part).
398
+ this.#pass = full || dirty.has(binding) ? undefined : change;
316
399
  try {
317
400
  this.#evaluate(evaluate, binding);
318
401
  }
@@ -323,6 +406,7 @@ export class Passes {
323
406
  this.#pass = undefined;
324
407
  }
325
408
  }
409
+ this.#running = false;
326
410
  if (this.#skipped && !this.#safety && !this.#stopped) {
327
411
  this.#safety = setTimeout(() => {
328
412
  this.#safety = undefined;
@@ -0,0 +1,9 @@
1
+ import type { ScopeImpl } from "../scope.ts";
2
+ import type { Disposable, EventPayload, HostEvent } from "../types.ts";
3
+ /**
4
+ * The capability each event needs, as the player checks `events.subscribe`
5
+ * (`HOST_CAPABILITIES`: by event, otherwise `events`); `null`: none.
6
+ */
7
+ export declare const EVENT_NEEDS: Readonly<Record<HostEvent, string | null>>;
8
+ /** `Player.on`: `listener` hears each `event`, with a copy of its data. */
9
+ export declare function playerOn<E extends HostEvent>(scope: ScopeImpl, event: E, listener: (data: EventPayload<E>) => void): Disposable;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * `plugin.player.on` (L1, refactor plan §2.2; SDK 1.7.0): one listener of
3
+ * one player event, with the event's data as the player sent it. It is a
4
+ * plain subscription of the connection (counted as a listener, its `why`
5
+ * naming where the plugin added it), never a state copy: nothing is kept,
6
+ * nothing is compared.
7
+ */
8
+ import { failure } from "../errors.js";
9
+ /**
10
+ * The capability each event needs, as the player checks `events.subscribe`
11
+ * (`HOST_CAPABILITIES`: by event, otherwise `events`); `null`: none.
12
+ */
13
+ export const EVENT_NEEDS = {
14
+ "source.state": "events",
15
+ "playback.backend": "events",
16
+ "window.state": "events",
17
+ "packages.changed": null,
18
+ "bindings.changed": "bindings.manage",
19
+ "commands.keys": "commands",
20
+ "timeline.changed": "events",
21
+ "appearance.changed": "events",
22
+ "resolvers.fileTypes": "events",
23
+ "video.upscaling.changed": "video.upscaling",
24
+ };
25
+ /** `Player.on`: `listener` hears each `event`, with a copy of its data. */
26
+ export function playerOn(scope, event, listener) {
27
+ if (typeof event !== "string" || !Object.hasOwn(EVENT_NEEDS, event))
28
+ throw failure("invalid", `unknown event ${JSON.stringify(event)}; the events are ${Object.keys(EVENT_NEEDS).join(", ")}`);
29
+ if (typeof listener !== "function")
30
+ throw failure("invalid", "on takes an event name and a listener");
31
+ const watch = scope.runtime.on(event,
32
+ // A copy: the same data also feeds the SDK's own state copies.
33
+ // No full pass after it (`runQuiet`): the listener pushes its own
34
+ // values; `plugin.state` writes run the passes that concern them.
35
+ (data) => scope.runQuiet(listener, structuredClone(data)), EVENT_NEEDS[event]);
36
+ const handle = {
37
+ dispose: async () => {
38
+ scope.forget(handle);
39
+ await watch.dispose();
40
+ },
41
+ };
42
+ return scope.add(handle);
43
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * The player's rules for its controls that the SDK checks before asking
3
+ * (L1, refactor plan §2.2): shared by `plugin.player`, `plugin.host` and
4
+ * `TestHost`.
5
+ */
6
+ /** A step-seek length the player accepts: 0.1–3600 seconds in tenths. */
7
+ export declare const validStep: (seconds: unknown) => seconds is number;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The player's rules for its controls that the SDK checks before asking
3
+ * (L1, refactor plan §2.2): shared by `plugin.player`, `plugin.host` and
4
+ * `TestHost`.
5
+ */
6
+ /** A step-seek length the player accepts: 0.1–3600 seconds in tenths. */
7
+ export const validStep = (seconds) => typeof seconds === "number" &&
8
+ Number.isFinite(seconds) &&
9
+ seconds >= 0.1 &&
10
+ seconds <= 3600 &&
11
+ Math.abs(Math.round(seconds * 10) - seconds * 10) <= 1e-9;
@@ -2,6 +2,8 @@ import type { SourceIntent, SourceStamp, SourceState, SourceTicket } from "../so
2
2
  import { Latest } from "./latest.ts";
3
3
  import type { Runtime } from "./runtime.ts";
4
4
  import type { ScopeImpl } from "./scope.ts";
5
+ import { resolvedStream } from "./stream.ts";
6
+ import type { StreamSource } from "./stream.ts";
5
7
  import type { Conditional, Disposable, Player, PlayerState } from "./types.ts";
6
8
  /**
7
9
  * The key of `plugin.player.state` in the runtime's field tracking
@@ -10,11 +12,21 @@ import type { Conditional, Disposable, Player, PlayerState } from "./types.ts";
10
12
  */
11
13
  export declare const PLAYER = "player";
12
14
  export declare const rawOf: (state: PlayerState) => SourceState | undefined;
13
- /** A step-seek length the player accepts: 0.1–3600 seconds in tenths. */
14
- export declare const validStep: (seconds: unknown) => seconds is number;
15
15
  export declare const sameStamp: (a: SourceStamp, b: SourceStamp) => boolean;
16
- /** One per plugin: the state store and the operation book client. */
17
- export declare class PlayerService {
16
+ /**
17
+ * One per plugin: the state store and the operation book client.
18
+ *
19
+ * The state copy (a mirror of `source.state`) follows the player's events
20
+ * from the plugin's start when the manifest declares `events`
21
+ * (`createPlugin` subscribes before the start-up read, as before 1.7.0, so
22
+ * a read in any later handler is current). Without `events` no event can
23
+ * arrive; `start` is then called by the first use of the state (a read of
24
+ * `plugin.player.state`, `onChange`, `isCurrent`, a media handle, a
25
+ * scrub, a stream, a filter, a shader or view request, a capture) and
26
+ * subscribes nothing. A start on demand with `events` and `playback.read`
27
+ * would read the state again; `createPlugin` makes it unreachable today.
28
+ */
29
+ export declare class PlayerService implements StreamSource {
18
30
  #private;
19
31
  readonly runtime: Runtime;
20
32
  raw: SourceState;
@@ -30,6 +42,20 @@ export declare class PlayerService {
30
42
  }>;
31
43
  constructor(runtime: Runtime);
32
44
  init(): Promise<void>;
45
+ /** Whether the mirror follows the player's events yet. */
46
+ get started(): boolean;
47
+ /**
48
+ * Follow `source.state` from now on: the SDK's copy of the state (a
49
+ * mirror; a plugin listens through `watch` and the bindings that read
50
+ * `state`). Then read the state once more (with `playback.read` and
51
+ * `events`): a change between the start-up read and the subscription
52
+ * sent no event to this plugin. Resolves when that read is in.
53
+ * `reread: false`: subscribed before the start-up read
54
+ * (`createPlugin`), which then is that read.
55
+ */
56
+ start(reread?: boolean): Promise<void>;
57
+ /** The state as the player has it now, for a call that needs a source. */
58
+ current(): Promise<PlayerState>;
33
59
  /** Drop anything older than what was already delivered. */
34
60
  accept(raw: SourceState): void;
35
61
  watch(watcher: (raw: SourceState) => void): Disposable;
@@ -52,4 +78,11 @@ export declare class PlayerService {
52
78
  closeIfCurrent(generation: number): Promise<void>;
53
79
  }
54
80
  export declare const playerOf: (runtime: Runtime) => PlayerService;
81
+ /**
82
+ * The player's source as a stream needs it (`stream.ts`, L1): a stream
83
+ * follows the source, so its state copy starts following the events.
84
+ */
85
+ export declare function streamSource(runtime: Runtime): StreamSource;
86
+ /** A file resolver's stream (`resolvers.ts`), over {@link streamSource}. */
87
+ export declare const resolvedPlayerStream: (scope: ScopeImpl, stream: Parameters<typeof resolvedStream>[2], signal: AbortSignal) => Promise<import("../source-resolvers.ts").ResolvedProducer>;
55
88
  export declare function player(scope: ScopeImpl): Player;
@@ -5,7 +5,9 @@
5
5
  import { HostError } from "../wire.js";
6
6
  import { Latest } from "./latest.js";
7
7
  import { abortError, failure, quiet, sleep, uncertain } from "./errors.js";
8
- import { openStream } from "./stream.js";
8
+ import { openStream, resolvedStream } from "./stream.js";
9
+ import { playerOn } from "./player/events.js";
10
+ import { validStep } from "./player/rules.js";
9
11
  const raws = new WeakMap();
10
12
  const empty = {
11
13
  kind: "media",
@@ -144,19 +146,25 @@ function decoding(hwdec) {
144
146
  return hwdec === "no" ? "software" : "hardware";
145
147
  }
146
148
  export const rawOf = (state) => raws.get(state);
147
- /** A step-seek length the player accepts: 0.1–3600 seconds in tenths. */
148
- export const validStep = (seconds) => typeof seconds === "number" &&
149
- Number.isFinite(seconds) &&
150
- seconds >= 0.1 &&
151
- seconds <= 3600 &&
152
- Math.abs(Math.round(seconds * 10) - seconds * 10) <= 1e-9;
153
149
  export const sameStamp = (a, b) => a.generation === b.generation && a.intentVersion === b.intentVersion;
154
150
  function sourceFailure(error) {
155
151
  return typeof error === "string"
156
152
  ? failure(error)
157
153
  : failure("backend", error.backend);
158
154
  }
159
- /** One per plugin: the state store and the operation book client. */
155
+ /**
156
+ * One per plugin: the state store and the operation book client.
157
+ *
158
+ * The state copy (a mirror of `source.state`) follows the player's events
159
+ * from the plugin's start when the manifest declares `events`
160
+ * (`createPlugin` subscribes before the start-up read, as before 1.7.0, so
161
+ * a read in any later handler is current). Without `events` no event can
162
+ * arrive; `start` is then called by the first use of the state (a read of
163
+ * `plugin.player.state`, `onChange`, `isCurrent`, a media handle, a
164
+ * scrub, a stream, a filter, a shader or view request, a capture) and
165
+ * subscribes nothing. A start on demand with `events` and `playback.read`
166
+ * would read the state again; `createPlugin` makes it unreachable today.
167
+ */
160
168
  export class PlayerService {
161
169
  runtime;
162
170
  raw = empty;
@@ -168,13 +176,17 @@ export class PlayerService {
168
176
  change = new Map();
169
177
  #watchers = new Set();
170
178
  #operation = 0;
179
+ /** The mirror subscribed (`start`). */
180
+ #started = false;
181
+ /** The read that followed the subscription (resolved: none was needed). */
182
+ #fresh = Promise.resolve();
171
183
  output;
172
184
  constructor(runtime) {
173
185
  this.runtime = runtime;
174
186
  this.state = view(empty, runtime);
175
- // The SDK's copy of the state (a mirror): a plugin listens through
176
- // `watch` and the bindings that read `state`.
177
- runtime.on("source.state", (data) => this.accept(data), "events", true);
187
+ // An action outside this file that changed the source (`plugin.host.run`)
188
+ // asks for the state again through the connection.
189
+ runtime.rereads.set("source.state", () => this.refresh());
178
190
  this.output = new Latest(async ({ volume, muted }) => {
179
191
  if (volume !== undefined)
180
192
  await runtime.host.playback.set("volume", volume);
@@ -187,6 +199,35 @@ export class PlayerService {
187
199
  if (this.runtime.can("playback.read"))
188
200
  await this.refresh();
189
201
  }
202
+ /** Whether the mirror follows the player's events yet. */
203
+ get started() {
204
+ return this.#started;
205
+ }
206
+ /**
207
+ * Follow `source.state` from now on: the SDK's copy of the state (a
208
+ * mirror; a plugin listens through `watch` and the bindings that read
209
+ * `state`). Then read the state once more (with `playback.read` and
210
+ * `events`): a change between the start-up read and the subscription
211
+ * sent no event to this plugin. Resolves when that read is in.
212
+ * `reread: false`: subscribed before the start-up read
213
+ * (`createPlugin`), which then is that read.
214
+ */
215
+ start(reread = true) {
216
+ if (this.#started)
217
+ return this.#fresh;
218
+ this.#started = true;
219
+ this.runtime.on("source.state", (data) => this.accept(data), "events", true);
220
+ if (reread &&
221
+ this.runtime.can("playback.read") &&
222
+ this.runtime.can("events"))
223
+ this.#fresh = this.refresh().then(() => { }, () => { });
224
+ return this.#fresh;
225
+ }
226
+ /** The state as the player has it now, for a call that needs a source. */
227
+ async current() {
228
+ await this.start();
229
+ return this.state;
230
+ }
190
231
  /** Drop anything older than what was already delivered. */
191
232
  accept(raw) {
192
233
  if (raw.eventSequence < this.raw.eventSequence)
@@ -205,6 +246,7 @@ export class PlayerService {
205
246
  this.runtime.changed(PLAYER, changed);
206
247
  }
207
248
  watch(watcher) {
249
+ void this.start();
208
250
  this.#watchers.add(watcher);
209
251
  const held = this.runtime.hold("source.state");
210
252
  return {
@@ -304,6 +346,7 @@ class MediaHandle {
304
346
  this.#ticket = ticket;
305
347
  }
306
348
  get current() {
349
+ void this.#service.start();
307
350
  this.#service.runtime.depends(PLAYER, "stamp");
308
351
  return this.#service.raw.stamp.generation === this.#ticket.stamp.generation;
309
352
  }
@@ -377,13 +420,29 @@ class ScrubHandle {
377
420
  #service;
378
421
  constructor(service) {
379
422
  this.#service = service;
423
+ // A scrub follows the source. When it is the first thing to need the
424
+ // state (the mirror starts now), the gesture starts from the state read
425
+ // right after subscribing, not from the copy of start-up.
426
+ const late = !service.started;
427
+ const fresh = service.start();
428
+ let ready = !late;
380
429
  this.#allowed = { ...service.raw.stamp };
430
+ if (late)
431
+ void fresh.then(() => {
432
+ ready = true;
433
+ if (this.#active && !this.#pending)
434
+ this.#allowed = { ...service.raw.stamp };
435
+ });
381
436
  this.#watch = service.watch((raw) => {
437
+ if (!ready)
438
+ return;
382
439
  if (!sameStamp(raw.stamp, this.#allowed) &&
383
440
  !(this.#pending && sameStamp(raw.stamp, this.#pending)))
384
441
  this.end();
385
442
  });
386
443
  this.#queue = new Latest(async ({ seconds, keyframes }) => {
444
+ if (late)
445
+ await fresh;
387
446
  if (!this.#active)
388
447
  throw superseded;
389
448
  const started = Date.now();
@@ -495,10 +554,22 @@ function provideSequence(scope, provider) {
495
554
  return scope.add(handle);
496
555
  }
497
556
  export const playerOf = (runtime) => runtime.service("player", () => new PlayerService(runtime));
557
+ /**
558
+ * The player's source as a stream needs it (`stream.ts`, L1): a stream
559
+ * follows the source, so its state copy starts following the events.
560
+ */
561
+ export function streamSource(runtime) {
562
+ const service = playerOf(runtime);
563
+ void service.start();
564
+ return service;
565
+ }
566
+ /** A file resolver's stream (`resolvers.ts`), over {@link streamSource}. */
567
+ export const resolvedPlayerStream = (scope, stream, signal) => resolvedStream(scope, streamSource(scope.runtime), stream, signal);
498
568
  export function player(scope) {
499
569
  const service = () => playerOf(scope.runtime);
500
570
  return {
501
571
  get state() {
572
+ void service().start();
502
573
  scope.runtime.reading("source.state");
503
574
  scope.runtime.depends(PLAYER);
504
575
  return service().state;
@@ -513,7 +584,9 @@ export function player(scope) {
513
584
  });
514
585
  },
515
586
  refresh: () => service().refresh(),
587
+ on: (event, listener) => playerOn(scope, event, listener),
516
588
  isCurrent(state) {
589
+ void service().start();
517
590
  const raw = raws.get(state);
518
591
  scope.runtime.depends(PLAYER, "stamp");
519
592
  return !!raw && sameStamp(raw.stamp, service().raw.stamp);
@@ -546,7 +619,7 @@ export function player(scope) {
546
619
  }
547
620
  return media;
548
621
  },
549
- openStream: (options) => openStream(scope, service(), options),
622
+ openStream: (options) => openStream(scope, streamSource(scope.runtime), options),
550
623
  setPaused: (paused, options) => service().control({ kind: "control", control: { kind: "pause", paused } }, options),
551
624
  seek: (seconds, options) => service().control({ kind: "control", control: { kind: "seek", seconds } }, options),
552
625
  setRate: (rate, options) => service().control({ kind: "control", control: { kind: "rate", rate } }, options),
@@ -3,8 +3,7 @@
3
3
  * this plugin generates, for `player.open`.
4
4
  */
5
5
  import { failure, quiet } from "./errors.js";
6
- import { playerOf } from "./player.js";
7
- import { resolvedStream } from "./stream.js";
6
+ import { resolvedPlayerStream } from "./player.js";
8
7
  import { windowOf } from "./window.js";
9
8
  const isFiles = (options) => "extensions" in options;
10
9
  const sameTypes = (a, b) => a.length === b.length && a.every((type, i) => type === b[i]);
@@ -111,7 +110,7 @@ export const resolvers = (scope) => ({
111
110
  const stream = await options.resolve(url, signal);
112
111
  if (stream?.kind !== "stream")
113
112
  throw failure("invalid", 'a file resolver returns { kind: "stream", width, height, frame }');
114
- return resolvedStream(scope, playerOf(runtime), stream, signal);
113
+ return resolvedPlayerStream(scope, stream, signal);
115
114
  })
116
115
  : runtime.host.sourceResolvers.register({ id: options.id, schemes: options.schemes, hosts: options.hosts }, ({ url, signal }) => options.resolve(url, signal));
117
116
  let disposed = false;
@@ -7,9 +7,8 @@
7
7
  import { Connection } from "./connection.ts";
8
8
  import type { PluginManifest, RuntimeOptions } from "./connection.ts";
9
9
  import { Passes } from "./passes.ts";
10
- import type { Change, Deps, PassCounts } from "./passes.ts";
10
+ import type { BindingHandle, Change, Deps, PassCounts } from "./passes.ts";
11
11
  import type { Transport } from "../wire.ts";
12
- import type { Disposable } from "./types.ts";
13
12
  export { WAKE_EVENTS } from "./connection.ts";
14
13
  /** Per-plugin services: the connection, and the update passes over it. */
15
14
  export declare class Runtime extends Connection {
@@ -19,7 +18,7 @@ export declare class Runtime extends Connection {
19
18
  readonly counts: PassCounts;
20
19
  constructor(transport: Transport, manifest: PluginManifest, directory: string, dataDirectory?: string, app?: unknown, options?: RuntimeOptions);
21
20
  /** {@link Passes.bind} */
22
- bind(evaluate: () => void): Disposable;
21
+ bind(evaluate: () => void, slot?: number): BindingHandle;
23
22
  /** {@link Passes.reading} */
24
23
  reading(event: string): void;
25
24
  /** {@link Passes.depends} */