@vestra-player/sdk 1.8.0 → 1.10.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 (106) hide show
  1. package/CHANGELOG.md +64 -2
  2. package/README.md +2 -2
  3. package/bin/vestra-plugin.mjs +98 -106
  4. package/capability-scan.json +21 -1
  5. package/dist/api-requests.d.ts +15 -39
  6. package/dist/api-requests.js +28 -50
  7. package/dist/capability-scan.d.ts +1 -1
  8. package/dist/capability-scan.js +8 -9
  9. package/dist/condition-table.d.ts +1889 -0
  10. package/dist/condition-table.js +562 -0
  11. package/dist/docs-tree.json.gz +0 -0
  12. package/dist/docs.d.ts +1 -1
  13. package/dist/docs.js +1 -1
  14. package/dist/i18n-tools.d.ts +7 -0
  15. package/dist/i18n-tools.js +66 -3
  16. package/dist/index.d.ts +4 -5
  17. package/dist/manifest.d.ts +5 -0
  18. package/dist/manifest.js +9 -24
  19. package/dist/member-uses.d.ts +61 -0
  20. package/dist/member-uses.js +160 -0
  21. package/dist/player-runtime.d.ts +4 -2
  22. package/dist/player-runtime.js +4 -2
  23. package/dist/plugin/appearance.d.ts +1 -1
  24. package/dist/plugin/appearance.js +2 -2
  25. package/dist/plugin/bindings.d.ts +1 -1
  26. package/dist/plugin/bindings.js +2 -2
  27. package/dist/plugin/commands.js +1 -12
  28. package/dist/plugin/connection.d.ts +38 -8
  29. package/dist/plugin/connection.js +126 -37
  30. package/dist/plugin/drops.js +1 -1
  31. package/dist/plugin/index.d.ts +7 -0
  32. package/dist/plugin/index.js +22 -15
  33. package/dist/plugin/media.js +0 -12
  34. package/dist/plugin/mpv.js +39 -12
  35. package/dist/plugin/packages.js +1 -2
  36. package/dist/plugin/player/events.d.ts +17 -7
  37. package/dist/plugin/player/events.js +26 -9
  38. package/dist/plugin/player.d.ts +37 -13
  39. package/dist/plugin/player.js +133 -22
  40. package/dist/plugin/scope.d.ts +4 -1
  41. package/dist/plugin/scope.js +8 -0
  42. package/dist/plugin/services.d.ts +4 -4
  43. package/dist/plugin/services.js +7 -29
  44. package/dist/plugin/settings.js +1 -1
  45. package/dist/plugin/shaders.js +0 -2
  46. package/dist/plugin/stream.d.ts +2 -1
  47. package/dist/plugin/stream.js +2 -2
  48. package/dist/plugin/testing/commands.d.ts +3 -10
  49. package/dist/plugin/testing/commands.js +24 -37
  50. package/dist/plugin/testing/index.d.ts +133 -116
  51. package/dist/plugin/testing/index.js +198 -125
  52. package/dist/plugin/testing/media.d.ts +6 -0
  53. package/dist/plugin/testing/media.js +15 -2
  54. package/dist/plugin/testing/processing.js +1 -1
  55. package/dist/plugin/testing/services.js +0 -1
  56. package/dist/plugin/testing/source-events.d.ts +28 -0
  57. package/dist/plugin/testing/source-events.js +66 -0
  58. package/dist/plugin/testing/transport.js +5 -0
  59. package/dist/plugin/testing/triggers.d.ts +82 -0
  60. package/dist/plugin/testing/triggers.js +448 -0
  61. package/dist/plugin/timeline.d.ts +5 -6
  62. package/dist/plugin/timeline.js +7 -11
  63. package/dist/plugin/triggers.d.ts +55 -0
  64. package/dist/plugin/triggers.js +233 -0
  65. package/dist/plugin/types/base.d.ts +77 -87
  66. package/dist/plugin/types/commands.d.ts +384 -68
  67. package/dist/plugin/types/packages.d.ts +17 -55
  68. package/dist/plugin/types/playback.d.ts +382 -152
  69. package/dist/plugin/types/plugin-wire.d.ts +6 -36
  70. package/dist/plugin/types/processing.d.ts +10 -49
  71. package/dist/plugin/types/services.d.ts +11 -70
  72. package/dist/plugin/types/snapshots.d.ts +7 -60
  73. package/dist/plugin/types/sources.d.ts +2 -11
  74. package/dist/plugin/types/subtitles.d.ts +5 -26
  75. package/dist/plugin/types/ui-content.d.ts +1 -16
  76. package/dist/plugin/types/ui-layout.d.ts +53 -79
  77. package/dist/plugin/types/ui-locale.d.ts +5 -23
  78. package/dist/plugin/types/ui-shell.d.ts +13 -59
  79. package/dist/plugin/view/layout.js +1 -1
  80. package/dist/plugin/view.js +0 -2
  81. package/dist/plugin/when.d.ts +78 -4
  82. package/dist/plugin/when.js +182 -14
  83. package/dist/plugin/window.d.ts +1 -1
  84. package/dist/plugin/window.js +1 -1
  85. package/dist/plugin/wire-facade.js +1 -2
  86. package/dist/processing.d.ts +2 -3
  87. package/dist/protocol.d.ts +14 -0
  88. package/dist/protocol.js +14 -0
  89. package/dist/request-queue.js +1 -1
  90. package/dist/slint-version.d.ts +7 -0
  91. package/dist/slint-version.js +7 -0
  92. package/dist/subtitles.d.ts +1 -1
  93. package/dist/ts-plugin.d.ts +8 -15
  94. package/dist/ts-plugin.js +3 -141
  95. package/dist/wire.d.ts +22 -12
  96. package/dist/wire.js +17 -8
  97. package/llms.txt +6 -6
  98. package/package.json +1 -2
  99. package/dist/capabilities.d.ts +0 -107
  100. package/dist/capabilities.js +0 -69
  101. package/dist/capability-check.d.ts +0 -176
  102. package/dist/capability-check.js +0 -461
  103. package/dist/capability-rules.d.ts +0 -125
  104. package/dist/capability-rules.js +0 -192
  105. package/dist/host-capabilities.d.ts +0 -10
  106. package/dist/host-capabilities.js +0 -492
@@ -17,7 +17,8 @@ const SDK_DIRECTORY = fileURLToPath(new URL("..", import.meta.url));
17
17
  /**
18
18
  * Events the player wakes a stopped plugin for: a listener of one does not keep
19
19
  * the plugin running. They follow deliberate changes (an install, a key edit,
20
- * the palette, a resolver), not playback.
20
+ * the palette, a resolver), not playback, or report one thing that happened
21
+ * to a source (it opened, became ready, ended), never its stream of states.
21
22
  */
22
23
  export const WAKE_EVENTS = [
23
24
  "packages.changed",
@@ -28,6 +29,11 @@ export const WAKE_EVENTS = [
28
29
  "commands.keys",
29
30
  "appearance.changed",
30
31
  "resolvers.fileTypes",
32
+ // Player 0.10.0: the discrete source events, once per thing that
33
+ // happened to a source.
34
+ "source.opening",
35
+ "source.ready",
36
+ "source.ended",
31
37
  ];
32
38
  /**
33
39
  * The player's answers to a declaration that refuse that one declaration (a
@@ -88,8 +94,18 @@ export class Connection {
88
94
  */
89
95
  requests;
90
96
  #events = new Map();
91
- /** The missing `events` warning was given (once per plugin). */
97
+ /** The missing `subscribe()` warning was given (once per plugin). */
92
98
  #warnedEvents = false;
99
+ /**
100
+ * Event delivery is on: `plugin.player.subscribe()` was called
101
+ * ({@link Connection.deliver}).
102
+ */
103
+ #delivering = false;
104
+ /**
105
+ * The player's state is read again after the plugin's own actions:
106
+ * `plugin.player.read()` was called.
107
+ */
108
+ reads = false;
93
109
  views = new Map();
94
110
  commands = new Map();
95
111
  /**
@@ -169,8 +185,24 @@ export class Connection {
169
185
  this.commands.get(data.id)?.();
170
186
  });
171
187
  }
172
- can(capability) {
173
- return this.manifest.capabilities.includes(capability);
188
+ /** Event delivery is on (`plugin.player.subscribe()` was called). */
189
+ get delivering() {
190
+ return this.#delivering;
191
+ }
192
+ /**
193
+ * Turn event delivery on (`plugin.player.subscribe()`): every later
194
+ * listener is subscribed, and so is what was added before and heard
195
+ * nothing. Idempotent.
196
+ */
197
+ deliver() {
198
+ if (this.#delivering)
199
+ return;
200
+ this.#delivering = true;
201
+ for (const [event, group] of this.#events)
202
+ if (group.unheard) {
203
+ group.unheard = false;
204
+ this.#open(event, group);
205
+ }
174
206
  }
175
207
  /** Read the state copy kept by `event` again, if one exists; never rejects. */
176
208
  async reread(event) {
@@ -275,12 +307,14 @@ export class Connection {
275
307
  }
276
308
  /**
277
309
  * Host events with one `events.subscribe` per name, however many listeners.
278
- * Without the `events` capability the listener simply never fires.
310
+ * An event that needs delivery (`"events"`) is subscribed only once
311
+ * `plugin.player.subscribe()` was called; until then its listener simply
312
+ * never fires.
279
313
  * `mirror`: the SDK's own copy of the state (`plugin.player.state` and
280
314
  * the like), not a listener of the plugin's (see {@link EventGroup}).
281
315
  */
282
316
  on(event, listener,
283
- /** The capability the event needs; `null`: none. */
317
+ /** `"events"`: it arrives after `plugin.player.subscribe()`; `null`: always. */
284
318
  needs = "events", mirror = false) {
285
319
  let entry = this.#events.get(event);
286
320
  const created = !entry;
@@ -302,34 +336,12 @@ export class Connection {
302
336
  group.active++;
303
337
  group.whys.set(token, listenerWhy(this.callSite()));
304
338
  }
305
- if (created && needs === "events" && !this.can(needs))
339
+ if (created && needs === "events" && !this.#delivering)
306
340
  group.unheard = true;
307
341
  if (!mirror && group.unheard)
308
342
  this.#warnUnheard(event);
309
- if (created && (needs === null || this.can(needs))) {
310
- group.told = group.active > 0;
311
- const whys = group.told ? allWhys(group) : undefined;
312
- group.toldWhy = whys?.join("\n");
313
- group.off = this.host.events.subscribe(event, (data) => {
314
- // Split once: the state copies first, each on its own (one that
315
- // throws stops neither the others nor the plugin's listeners),
316
- // then the plugin's listeners in order.
317
- const mirrors = [];
318
- const others = [];
319
- for (const each of group.listeners)
320
- (group.mirrors.has(each) ? mirrors : others).push(each);
321
- for (const each of mirrors)
322
- try {
323
- each(data);
324
- }
325
- catch (error) {
326
- this.reportMirror(error);
327
- }
328
- for (const each of others)
329
- each(data);
330
- }, !group.told, whys);
331
- this.starting(group.off, (error) => console.error(`[${this.manifest.id}] subscribe ${event}:`, error));
332
- }
343
+ if (created && !group.unheard)
344
+ this.#open(event, group);
333
345
  else
334
346
  this.#tell(event, group);
335
347
  let listening = true;
@@ -353,10 +365,35 @@ export class Connection {
353
365
  },
354
366
  };
355
367
  }
368
+ /** Subscribe `group` to `event` at the player, as it stands now. */
369
+ #open(event, group) {
370
+ group.told = group.active > 0;
371
+ const whys = group.told ? allWhys(group) : undefined;
372
+ group.toldWhy = whys?.join("\n");
373
+ group.off = this.host.events.subscribe(event, (data) => {
374
+ // Split once: the state copies first, each on its own (one that
375
+ // throws stops neither the others nor the plugin's listeners),
376
+ // then the plugin's listeners in order.
377
+ const mirrors = [];
378
+ const others = [];
379
+ for (const each of group.listeners)
380
+ (group.mirrors.has(each) ? mirrors : others).push(each);
381
+ for (const each of mirrors)
382
+ try {
383
+ each(data);
384
+ }
385
+ catch (error) {
386
+ this.reportMirror(error);
387
+ }
388
+ for (const each of others)
389
+ each(data);
390
+ }, !group.told, whys);
391
+ this.starting(group.off, (error) => console.error(`[${this.manifest.id}] subscribe ${event}:`, error));
392
+ }
356
393
  /**
357
394
  * Count `event` as listened to while held: a watcher of a service's
358
395
  * state, a binding that reads it. Nothing when nothing subscribed to it
359
- * (no capability, or the service did not start).
396
+ * (no `plugin.player.subscribe()`, or the service did not start).
360
397
  */
361
398
  hold(event, why) {
362
399
  const group = this.#events.get(event);
@@ -381,17 +418,69 @@ export class Connection {
381
418
  },
382
419
  };
383
420
  }
421
+ /**
422
+ * Hear `event` while followed, whether or not event delivery is on, and
423
+ * count it as listened to (`plugin.player.stream`, SDK 1.10.0): the group
424
+ * the caller started (a state copy's) is subscribed now if it waited for
425
+ * `plugin.player.subscribe()`, and only it. When the last follower ends,
426
+ * such a group waits again as before: its subscription at the player
427
+ * ends, and the copy stays as it was. Nothing when nothing started the
428
+ * group.
429
+ */
430
+ follow(event, why) {
431
+ const group = this.#events.get(event);
432
+ if (!group)
433
+ return { dispose: async () => { } };
434
+ group.followers = (group.followers ?? 0) + 1;
435
+ group.active++;
436
+ const token = {};
437
+ group.whys.set(token, why ?? listenerWhy(this.callSite()));
438
+ if (group.unheard) {
439
+ group.unheard = false;
440
+ group.followed = true;
441
+ this.#open(event, group);
442
+ }
443
+ else
444
+ this.#tell(event, group);
445
+ let following = true;
446
+ return {
447
+ dispose: async () => {
448
+ if (!following)
449
+ return;
450
+ following = false;
451
+ group.active--;
452
+ group.followers = (group.followers ?? 1) - 1;
453
+ group.whys.delete(token);
454
+ if (this.#events.get(event) !== group)
455
+ return;
456
+ if (group.followers === 0 && group.followed && !this.#delivering) {
457
+ // Back to waiting for `subscribe()`, as before the first follower.
458
+ group.followed = false;
459
+ group.unheard = true;
460
+ const off = group.off;
461
+ group.off = undefined;
462
+ group.told = undefined;
463
+ group.toldWhy = undefined;
464
+ const unsubscribe = await off?.catch(() => undefined);
465
+ await unsubscribe?.().catch(() => { });
466
+ return;
467
+ }
468
+ this.#tell(event, group);
469
+ },
470
+ };
471
+ }
384
472
  /**
385
473
  * A development warning, once per plugin, no behaviour change: the plugin
386
- * follows `event` (an `onChange`, a `bind` that reads the state) without the
387
- * `events` capability, so changes made elsewhere (the user, the player,
388
- * another plugin) never reach it; the state moves only after its own calls.
474
+ * follows `event` (a listener, an `onChange`, a `bind` that reads the
475
+ * state) before `plugin.player.subscribe()`, so changes made elsewhere
476
+ * (the user, the player, another plugin) never reach it; the state moves
477
+ * only after its own calls.
389
478
  */
390
479
  #warnUnheard(event) {
391
480
  if (this.#warnedEvents)
392
481
  return;
393
482
  this.#warnedEvents = true;
394
- console.warn(`[${this.manifest.id}] follows ${event} (onChange, or a bind() that reads the state) without the "events" capability: changes made elsewhere will not reach it, only its own calls refresh the state. Declare "events" in manifest.json.`);
483
+ console.warn(`[${this.manifest.id}] follows ${event} (a listener, onChange, or a bind() that reads the state) before plugin.player.subscribe(): changes made elsewhere will not reach it, only its own calls refresh the state. Call plugin.player.subscribe() first in activate().`);
395
484
  }
396
485
  /**
397
486
  * Tell the player when `group` changes between mirror and listener, and
@@ -430,7 +519,7 @@ export class Connection {
430
519
  }
431
520
  /**
432
521
  * One request by its method name, as given (`plugin.wire.request`): nothing
433
- * added, nothing checked here, the player checks the capability. Every other
522
+ * added, nothing checked here; the player checks the request. Every other
434
523
  * part of the SDK uses the typed clients (`host`); `scripts/check-layers.mjs`
435
524
  * keeps it so. It waits in the request queue like every other.
436
525
  */
@@ -1,4 +1,4 @@
1
- /** File drops (`plugin.drops`, `Panel.onDrop`; capability `ui.drop`). */
1
+ /** File drops (`plugin.drops`, `Panel.onDrop`). */
2
2
  import { failure } from "./errors.js";
3
3
  const modifiers = (raw) => Object.freeze({
4
4
  alt: raw?.alt === true,
@@ -12,6 +12,13 @@ export type * from "./types.ts";
12
12
  export type { PluginManifest, RuntimeOptions };
13
13
  export interface PluginInstance {
14
14
  plugin: Plugin;
15
+ /**
16
+ * Call the plugin's `activate(plugin)` as `entry`: `plugin.trigger()`
17
+ * works only during its synchronous part, after which the player is told
18
+ * which triggers it declared (1.10.0). Returns what `entry` returned, as a
19
+ * promise. The runner and the test host call it instead of `entry`.
20
+ */
21
+ activate(entry: (plugin: Plugin) => unknown): Promise<unknown>;
15
22
  /**
16
23
  * Wait for registrations started during activation; rejects if one failed.
17
24
  * A command declaration the player refused is not a failure (it is
@@ -2,29 +2,24 @@ import { Runtime } from "./runtime.js";
2
2
  import { stopReason } from "./connection.js";
3
3
  import { failure } from "./errors.js";
4
4
  import { ScopeImpl } from "./scope.js";
5
- import { playerOf } from "./player.js";
6
5
  import { windowOf } from "./window.js";
7
6
  import { timelineOf } from "./timeline.js";
7
+ import { triggersOf } from "./triggers.js";
8
8
  export async function createPlugin(transport, manifest, directory, dataDirectory,
9
9
  /** `plugin.app` (the runner passes the player's `VESTRA_APP_INFO`). */
10
10
  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
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);
23
- await Promise.all([
24
- player.init(),
25
- windowOf(runtime).init(),
26
- timelineOf(runtime).init(),
27
- ]);
14
+ // The window state is synchronous for plugins: read it first. The
15
+ // player's state and the timeline are the plugin's explicit calls, first
16
+ // in `activate`: `plugin.player.subscribe()` (event delivery, the copy
17
+ // of the player's state follows from then on) and
18
+ // `await plugin.player.read()` (state and timeline read once). Window and
19
+ // timeline subscribe on their first use. The timeline's copy exists from
20
+ // the start: it tells the connection how `read()` reads the lists.
21
+ timelineOf(runtime);
22
+ await windowOf(runtime).init();
28
23
  const root = new ScopeImpl(runtime);
29
24
  // A runaway of the update passes goes to the plugin's error handlers.
30
25
  runtime.passes.report = (error) => root.report(error);
@@ -38,8 +33,20 @@ options) {
38
33
  const { code, message } = (data ?? {});
39
34
  root.report(failure(code ?? "ui", message ?? "user interface error"));
40
35
  }));
36
+ const triggers = triggersOf(runtime);
41
37
  return {
42
38
  plugin: root,
39
+ activate(entry) {
40
+ triggers.begin();
41
+ let result;
42
+ try {
43
+ result = entry(root);
44
+ }
45
+ finally {
46
+ triggers.end();
47
+ }
48
+ return Promise.resolve(result);
49
+ },
43
50
  activated: () => runtime.activated(),
44
51
  dispose: async (info) => {
45
52
  runtime.stopping ??= { reason: stopReason(info?.reason) };
@@ -324,7 +324,6 @@ export function mediaCapture(scope) {
324
324
  const service = serviceOf(runtime);
325
325
  if (typeof options !== "object" || options === null)
326
326
  throw failure("invalid", "export options must be an object");
327
- needSources(scope);
328
327
  const parts = checkParts(options.parts);
329
328
  let mode;
330
329
  if (options.format === "copy") {
@@ -377,7 +376,6 @@ export function mediaCapture(scope) {
377
376
  // As the player: parts carry their own ends; `end` is a snapshot's.
378
377
  if (options?.end !== undefined)
379
378
  throw failure("invalid", "end belongs to a snapshot's check; parts carry their own end");
380
- needSources(scope);
381
379
  return scope.runtime.host.jobs.check(checkParts(input), encoding);
382
380
  },
383
381
  };
@@ -431,14 +429,8 @@ function checkParts(parts) {
431
429
  throw failure("invalid", `the parts are ${total} s long; an export takes at most ${MEDIA_SOURCE_LIMITS.totalSeconds} s`);
432
430
  return checked;
433
431
  }
434
- /** The parts' tokens are media.sources': refuse at once without it, as the player does. */
435
- function needSources(scope) {
436
- if (!scope.runtime.can("media.sources"))
437
- throw failure("capability", "media.sources must be declared in manifest");
438
- }
439
432
  function mediaSources(scope) {
440
433
  const host = () => {
441
- needSources(scope);
442
434
  return scope.runtime.host.mediaSources;
443
435
  };
444
436
  const token = (value) => {
@@ -459,10 +451,6 @@ function mediaSources(scope) {
459
451
  forget: async (value) => host().forget(token(value)),
460
452
  list: async () => host().list(),
461
453
  async open(value, options = {}) {
462
- needSources(scope);
463
- // An open like Player.open's: the player asks playback.control too.
464
- if (!scope.runtime.can("playback.control"))
465
- throw failure("capability", "playback.control must be declared in manifest");
466
454
  const start = options?.start;
467
455
  if (start !== undefined && !(Number.isFinite(start) && start >= 0))
468
456
  throw failure("invalid", "start must be seconds from 0");
@@ -9,19 +9,34 @@ import { failure } from "./errors.js";
9
9
  class Observers {
10
10
  #runtime;
11
11
  #names = new Map();
12
+ /** The newest value of each observed name, once one arrived. */
13
+ #values = new Map();
14
+ /** Listeners that still wait for the kept value (see `add`). */
15
+ #replays = new Set();
12
16
  #started = false;
13
17
  constructor(runtime) {
14
18
  this.#runtime = runtime;
15
19
  }
16
- /** One `mpv.observe` per name, however many listeners. */
20
+ /**
21
+ * One `mpv.observe` per name, however many listeners. The player sends
22
+ * the current value once, in answer to that request; a listener added
23
+ * after it arrived gets the kept value as its first, one microtask later
24
+ * (never inside `observe()` itself), unless a newer one reached it first.
25
+ */
17
26
  add(name, listener, report) {
18
27
  if (!this.#started) {
19
28
  this.#started = true;
20
29
  // Sent to the plugin that observes: no subscription of its own.
21
30
  this.#runtime.raw("mpv.property", (data) => {
22
31
  const event = data;
23
- for (const each of [...(this.#names.get(event.name) ?? [])])
32
+ const listeners = this.#names.get(event.name);
33
+ if (!listeners)
34
+ return;
35
+ this.#values.set(event.name, event.value);
36
+ for (const each of [...listeners]) {
37
+ this.#replays.delete(each);
24
38
  each(event.value);
39
+ }
25
40
  });
26
41
  }
27
42
  let listeners = this.#names.get(name);
@@ -30,24 +45,35 @@ class Observers {
30
45
  this.#names.set(name, listeners);
31
46
  this.#runtime.starting(this.#runtime.host.mpv.observeProperty(name), report);
32
47
  }
48
+ else if (this.#values.has(name)) {
49
+ this.#replays.add(listener);
50
+ const added = listeners;
51
+ queueMicrotask(() => {
52
+ // Still waiting, and still listening to the same observation (not
53
+ // one made again after every listener had left).
54
+ if (!this.#replays.delete(listener))
55
+ return;
56
+ if (this.#names.get(name) !== added || !added.has(listener))
57
+ return;
58
+ listener(this.#values.get(name));
59
+ });
60
+ }
33
61
  listeners.add(listener);
34
62
  }
35
63
  async remove(name, listener) {
36
64
  const listeners = this.#names.get(name);
37
- if (!listeners?.delete(listener) || listeners.size)
65
+ if (!listeners?.delete(listener))
66
+ return;
67
+ this.#replays.delete(listener);
68
+ if (listeners.size)
38
69
  return;
39
70
  this.#names.delete(name);
71
+ this.#values.delete(name);
40
72
  await this.#runtime.host.mpv.unobserveProperty(name).catch(() => { });
41
73
  }
42
74
  }
43
75
  export const mpv = (scope) => {
44
- /** Refuse at once without `mpv`, as the player does. */
45
- const declared = () => {
46
- if (!scope.runtime.can("mpv"))
47
- throw failure("capability", "mpv must be declared in manifest");
48
- };
49
76
  const named = (name, write) => {
50
- declared();
51
77
  const problem = mpvNameProblem(name);
52
78
  if (problem)
53
79
  throw failure("invalid", problem);
@@ -59,7 +85,6 @@ export const mpv = (scope) => {
59
85
  const observers = () => scope.runtime.service("mpv.observers", () => new Observers(scope.runtime));
60
86
  return {
61
87
  async command(args) {
62
- declared();
63
88
  const problem = mpvCommandProblem(args);
64
89
  if (problem)
65
90
  throw failure("invalid", problem);
@@ -83,8 +108,10 @@ export const mpv = (scope) => {
83
108
  const property = named(name, false);
84
109
  if (typeof listener !== "function")
85
110
  throw failure("invalid", "observe takes a property name and a listener");
86
- // Like `player.on`: no full update pass after it.
87
- const deliver = (value) => scope.runQuiet(listener, value);
111
+ // A handler like the others: a full update pass follows it (unlike
112
+ // `player.on`, whose events can come with every position change; the
113
+ // player sends a property at most every 100 ms).
114
+ const deliver = (value) => scope.run(listener, value);
88
115
  observers().add(property, deliver, (error) => scope.report(error));
89
116
  const handle = {
90
117
  dispose: async () => {
@@ -1,4 +1,4 @@
1
- /** Package validation, install and uninstall (`plugins.manage`). */
1
+ /** Package validation, install and uninstall. */
2
2
  import { randomUUID } from "node:crypto";
3
3
  import { resolve } from "node:path";
4
4
  import { failure, sleep, uncertain } from "./errors.js";
@@ -159,7 +159,6 @@ const info = (plugin) => Object.freeze({
159
159
  origin: plugin.origin,
160
160
  state: plugin.state,
161
161
  error: plugin.error,
162
- capabilities: [...plugin.capabilities],
163
162
  directory: plugin.directory,
164
163
  builtin: plugin.builtin === true,
165
164
  source: plugin.source ? Object.freeze({ ...plugin.source }) : null,
@@ -1,11 +1,21 @@
1
1
  import type { ScopeImpl } from "../scope.ts";
2
- import type { Disposable, EventPayload, HostEvent } from "../types.ts";
2
+ import type { Disposable, EventPayload, HostEvent, SourceEvent } from "../types.ts";
3
3
  /**
4
- * The capability each event needs, as the player checks `events.subscribe`
5
- * (`HOST_CAPABILITIES`: by event, otherwise `events`); `null`: none.
4
+ * Which events need event delivery (`"events"`: they reach the plugin only
5
+ * after `plugin.player.subscribe()`, as they needed the `events`
6
+ * declaration before 1.9.0); `null`: they arrive from the first listener.
6
7
  */
7
- export declare const EVENT_NEEDS: Readonly<Record<HostEvent, string | null>>;
8
+ export declare const EVENT_NEEDS: Readonly<Record<HostEvent | SourceEvent, "events" | null>>;
8
9
  /** `Player.on`'s arguments: a {@link HostEvent} name and a function. */
9
- export declare function checkOn(event: unknown, listener: unknown): asserts event is HostEvent;
10
- /** `Player.on`: `listener` hears each `event`, with a copy of its data. */
11
- export declare function playerOn<E extends HostEvent>(scope: ScopeImpl, event: E, listener: (data: EventPayload<E>) => void): Disposable;
10
+ export declare function checkOn(event: unknown, listener: unknown): asserts event is HostEvent | SourceEvent;
11
+ /**
12
+ * The discrete source events (player 0.10.0): their `state` reaches the
13
+ * listener as a `PlayerState`, not in the wire shape.
14
+ */
15
+ export declare const SOURCE_EVENTS: ReadonlySet<string>;
16
+ /**
17
+ * `Player.on`: `listener` hears each `event`, with a copy of its data;
18
+ * `convert` (the source events) turns the copy into what the listener
19
+ * gets.
20
+ */
21
+ export declare function playerOn<E extends HostEvent>(scope: ScopeImpl, event: E | SourceEvent, listener: (data: EventPayload<E>) => void, convert?: (data: unknown) => unknown): Disposable;
@@ -7,21 +7,25 @@
7
7
  */
8
8
  import { failure } from "../errors.js";
9
9
  /**
10
- * The capability each event needs, as the player checks `events.subscribe`
11
- * (`HOST_CAPABILITIES`: by event, otherwise `events`); `null`: none.
10
+ * Which events need event delivery (`"events"`: they reach the plugin only
11
+ * after `plugin.player.subscribe()`, as they needed the `events`
12
+ * declaration before 1.9.0); `null`: they arrive from the first listener.
12
13
  */
13
14
  export const EVENT_NEEDS = {
14
15
  "source.state": "events",
15
16
  "playback.backend": "events",
16
17
  "window.state": "events",
17
18
  "packages.changed": null,
18
- "bindings.changed": "bindings.manage",
19
- "commands.keys": "commands",
19
+ "bindings.changed": null,
20
+ "commands.keys": null,
20
21
  "timeline.changed": "events",
21
22
  "appearance.changed": "events",
22
23
  "resolvers.fileTypes": "events",
23
- "video.upscaling.changed": "video.upscaling",
24
- "video.motion.changed": "video.motion",
24
+ "video.upscaling.changed": null,
25
+ "video.motion.changed": null,
26
+ "source.opening": null,
27
+ "source.ready": null,
28
+ "source.ended": null,
25
29
  };
26
30
  /** `Player.on`'s arguments: a {@link HostEvent} name and a function. */
27
31
  export function checkOn(event, listener) {
@@ -30,14 +34,27 @@ export function checkOn(event, listener) {
30
34
  if (typeof listener !== "function")
31
35
  throw failure("invalid", "on takes an event name and a listener");
32
36
  }
33
- /** `Player.on`: `listener` hears each `event`, with a copy of its data. */
34
- export function playerOn(scope, event, listener) {
37
+ /**
38
+ * The discrete source events (player 0.10.0): their `state` reaches the
39
+ * listener as a `PlayerState`, not in the wire shape.
40
+ */
41
+ export const SOURCE_EVENTS = new Set([
42
+ "source.opening",
43
+ "source.ready",
44
+ "source.ended",
45
+ ]);
46
+ /**
47
+ * `Player.on`: `listener` hears each `event`, with a copy of its data;
48
+ * `convert` (the source events) turns the copy into what the listener
49
+ * gets.
50
+ */
51
+ export function playerOn(scope, event, listener, convert = (data) => data) {
35
52
  checkOn(event, listener);
36
53
  const watch = scope.runtime.on(event,
37
54
  // A copy: the same data also feeds the SDK's own state copies.
38
55
  // No full pass after it (`runQuiet`): the listener pushes its own
39
56
  // values; `plugin.state` writes run the passes that concern them.
40
- (data) => scope.runQuiet(listener, structuredClone(data)), EVENT_NEEDS[event]);
57
+ (data) => scope.runQuiet(listener, convert(structuredClone(data))), EVENT_NEEDS[event]);
41
58
  const handle = {
42
59
  dispose: async () => {
43
60
  scope.forget(handle);
@@ -17,14 +17,13 @@ export declare const sameStamp: (a: SourceStamp, b: SourceStamp) => boolean;
17
17
  * One per plugin: the state store and the operation book client.
18
18
  *
19
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
20
+ * from `plugin.player.subscribe()` on (`subscribe`, first in `activate`
21
+ * as the plugin writes it). Before that no event can arrive; `start` is
22
+ * then called by the first use of the state (a read of
24
23
  * `plugin.player.state`, `onChange`, `isCurrent`, a media handle, a
25
24
  * 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.
25
+ * subscribes nothing until `subscribe()`, which subscribes it and, after
26
+ * `read()`, reads the state once more (`#catchUp`).
28
27
  */
29
28
  export declare class PlayerService implements StreamSource {
30
29
  #private;
@@ -41,24 +40,49 @@ export declare class PlayerService implements StreamSource {
41
40
  muted?: boolean;
42
41
  }>;
43
42
  constructor(runtime: Runtime);
44
- init(): Promise<void>;
43
+ /**
44
+ * `plugin.player.read()`: read the source state and the timeline now, and
45
+ * from now on read the state again after the plugin's own actions
46
+ * (`Connection.reads`). Idempotent: one read, however often it is called;
47
+ * a read that failed is tried again by the next call.
48
+ */
49
+ read(): Promise<void>;
50
+ /**
51
+ * `plugin.player.subscribe()`: turn event delivery on and follow
52
+ * `source.state` now. Idempotent.
53
+ */
54
+ subscribe(): void;
45
55
  /** Whether the mirror follows the player's events yet. */
46
56
  get started(): boolean;
47
57
  /**
48
58
  * Follow `source.state` from now on: the SDK's copy of the state (a
49
59
  * 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.
60
+ * `state`). Then read the state once more (after `read()` and
61
+ * `subscribe()`): a change between that read and the subscription sent
62
+ * no event to this plugin. Resolves when that read is in.
55
63
  */
56
- start(reread?: boolean): Promise<void>;
64
+ start(): Promise<void>;
57
65
  /** The state as the player has it now, for a call that needs a source. */
58
66
  current(): Promise<PlayerState>;
59
67
  /** Drop anything older than what was already delivered. */
60
68
  accept(raw: SourceState): void;
69
+ /**
70
+ * A source event's data (`source.opening`, `source.ready`,
71
+ * `source.ended`) as the listener gets it: `state` as a
72
+ * {@link PlayerState}. The state of an opening or a ready is the
73
+ * player's newest, so `plugin.player.state` takes it first (when it is
74
+ * newer than what the plugin has): `isCurrent(state)` and `ifCurrent`
75
+ * then hold after a wake too, without a read. An ended source's state
76
+ * is not the player's current one and is not taken.
77
+ */
78
+ sourceEvent(event: string, data: unknown): unknown;
61
79
  watch(watcher: (raw: SourceState) => void): Disposable;
80
+ /**
81
+ * `watch`, for `plugin.player.stream`: the copy hears `source.state` while
82
+ * the watcher is open, with or without `subscribe()`, and the plugin
83
+ * counts as listening to it (`Connection.follow`).
84
+ */
85
+ follow(watcher: (raw: SourceState) => void, why: () => string): Disposable;
62
86
  refresh(): Promise<PlayerState>;
63
87
  /**
64
88
  * Submit an intent and wait for its definite outcome. A reply from the host