@vestra-player/sdk 1.9.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 (48) hide show
  1. package/CHANGELOG.md +29 -7
  2. package/bin/vestra-plugin.mjs +89 -8
  3. package/capability-scan.json +18 -0
  4. package/dist/api-requests.d.ts +4 -3
  5. package/dist/api-requests.js +10 -3
  6. package/dist/condition-table.d.ts +1889 -0
  7. package/dist/condition-table.js +562 -0
  8. package/dist/docs-tree.json.gz +0 -0
  9. package/dist/i18n-tools.d.ts +7 -0
  10. package/dist/i18n-tools.js +66 -3
  11. package/dist/player-runtime.d.ts +3 -0
  12. package/dist/player-runtime.js +3 -0
  13. package/dist/plugin/connection.d.ts +12 -1
  14. package/dist/plugin/connection.js +58 -1
  15. package/dist/plugin/index.d.ts +7 -0
  16. package/dist/plugin/index.js +13 -0
  17. package/dist/plugin/mpv.js +39 -5
  18. package/dist/plugin/player/events.d.ts +14 -5
  19. package/dist/plugin/player/events.js +19 -3
  20. package/dist/plugin/player.d.ts +16 -0
  21. package/dist/plugin/player.js +77 -1
  22. package/dist/plugin/scope.d.ts +4 -1
  23. package/dist/plugin/scope.js +8 -0
  24. package/dist/plugin/testing/commands.js +18 -22
  25. package/dist/plugin/testing/index.d.ts +107 -0
  26. package/dist/plugin/testing/index.js +168 -21
  27. package/dist/plugin/testing/media.d.ts +6 -0
  28. package/dist/plugin/testing/media.js +15 -2
  29. package/dist/plugin/testing/source-events.d.ts +28 -0
  30. package/dist/plugin/testing/source-events.js +66 -0
  31. package/dist/plugin/testing/transport.js +5 -0
  32. package/dist/plugin/testing/triggers.d.ts +82 -0
  33. package/dist/plugin/testing/triggers.js +448 -0
  34. package/dist/plugin/timeline.d.ts +3 -3
  35. package/dist/plugin/timeline.js +3 -3
  36. package/dist/plugin/triggers.d.ts +55 -0
  37. package/dist/plugin/triggers.js +233 -0
  38. package/dist/plugin/types/base.d.ts +37 -1
  39. package/dist/plugin/types/commands.d.ts +374 -1
  40. package/dist/plugin/types/playback.d.ts +257 -9
  41. package/dist/plugin/when.d.ts +78 -4
  42. package/dist/plugin/when.js +182 -14
  43. package/dist/slint-version.d.ts +7 -0
  44. package/dist/slint-version.js +7 -0
  45. package/dist/wire.d.ts +15 -0
  46. package/dist/wire.js +11 -0
  47. package/llms.txt +4 -4
  48. package/package.json +1 -1
@@ -9,7 +9,8 @@ export type Why = () => string;
9
9
  /**
10
10
  * Events the player wakes a stopped plugin for: a listener of one does not keep
11
11
  * the plugin running. They follow deliberate changes (an install, a key edit,
12
- * the palette, a resolver), not playback.
12
+ * the palette, a resolver), not playback, or report one thing that happened
13
+ * to a source (it opened, became ready, ended), never its stream of states.
13
14
  */
14
15
  export declare const WAKE_EVENTS: readonly string[];
15
16
  /**
@@ -165,6 +166,16 @@ export declare class Connection {
165
166
  * (no `plugin.player.subscribe()`, or the service did not start).
166
167
  */
167
168
  hold(event: string, why?: Why): Disposable;
169
+ /**
170
+ * Hear `event` while followed, whether or not event delivery is on, and
171
+ * count it as listened to (`plugin.player.stream`, SDK 1.10.0): the group
172
+ * the caller started (a state copy's) is subscribed now if it waited for
173
+ * `plugin.player.subscribe()`, and only it. When the last follower ends,
174
+ * such a group waits again as before: its subscription at the player
175
+ * ends, and the copy stays as it was. Nothing when nothing started the
176
+ * group.
177
+ */
178
+ follow(event: string, why?: Why): Disposable;
168
179
  /**
169
180
  * Why the plugin listens to each event it really listens to (mirrors
170
181
  * left out), as the player's log says it: for {@link TestHost} and
@@ -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
@@ -412,6 +418,57 @@ export class Connection {
412
418
  },
413
419
  };
414
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
+ }
415
472
  /**
416
473
  * A development warning, once per plugin, no behaviour change: the plugin
417
474
  * follows `event` (a listener, an `onChange`, a `bind` that reads the
@@ -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
@@ -4,6 +4,7 @@ import { failure } from "./errors.js";
4
4
  import { ScopeImpl } from "./scope.js";
5
5
  import { windowOf } from "./window.js";
6
6
  import { timelineOf } from "./timeline.js";
7
+ import { triggersOf } from "./triggers.js";
7
8
  export async function createPlugin(transport, manifest, directory, dataDirectory,
8
9
  /** `plugin.app` (the runner passes the player's `VESTRA_APP_INFO`). */
9
10
  app,
@@ -32,8 +33,20 @@ options) {
32
33
  const { code, message } = (data ?? {});
33
34
  root.report(failure(code ?? "ui", message ?? "user interface error"));
34
35
  }));
36
+ const triggers = triggersOf(runtime);
35
37
  return {
36
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
+ },
37
50
  activated: () => runtime.activated(),
38
51
  dispose: async (info) => {
39
52
  runtime.stopping ??= { reason: stopReason(info?.reason) };
@@ -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,13 +45,30 @@ 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
  }
@@ -76,8 +108,10 @@ export const mpv = (scope) => {
76
108
  const property = named(name, false);
77
109
  if (typeof listener !== "function")
78
110
  throw failure("invalid", "observe takes a property name and a listener");
79
- // Like `player.on`: no full update pass after it.
80
- 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);
81
115
  observers().add(property, deliver, (error) => scope.report(error));
82
116
  const handle = {
83
117
  dispose: async () => {
@@ -1,12 +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
4
  * Which events need event delivery (`"events"`: they reach the plugin only
5
5
  * after `plugin.player.subscribe()`, as they needed the `events`
6
6
  * declaration before 1.9.0); `null`: they arrive from the first listener.
7
7
  */
8
- export declare const EVENT_NEEDS: Readonly<Record<HostEvent, "events" | null>>;
8
+ export declare const EVENT_NEEDS: Readonly<Record<HostEvent | SourceEvent, "events" | null>>;
9
9
  /** `Player.on`'s arguments: a {@link HostEvent} name and a function. */
10
- export declare function checkOn(event: unknown, listener: unknown): asserts event is HostEvent;
11
- /** `Player.on`: `listener` hears each `event`, with a copy of its data. */
12
- 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;
@@ -23,6 +23,9 @@ export const EVENT_NEEDS = {
23
23
  "resolvers.fileTypes": "events",
24
24
  "video.upscaling.changed": null,
25
25
  "video.motion.changed": null,
26
+ "source.opening": null,
27
+ "source.ready": null,
28
+ "source.ended": null,
26
29
  };
27
30
  /** `Player.on`'s arguments: a {@link HostEvent} name and a function. */
28
31
  export function checkOn(event, listener) {
@@ -31,14 +34,27 @@ export function checkOn(event, listener) {
31
34
  if (typeof listener !== "function")
32
35
  throw failure("invalid", "on takes an event name and a listener");
33
36
  }
34
- /** `Player.on`: `listener` hears each `event`, with a copy of its data. */
35
- 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) {
36
52
  checkOn(event, listener);
37
53
  const watch = scope.runtime.on(event,
38
54
  // A copy: the same data also feeds the SDK's own state copies.
39
55
  // No full pass after it (`runQuiet`): the listener pushes its own
40
56
  // values; `plugin.state` writes run the passes that concern them.
41
- (data) => scope.runQuiet(listener, structuredClone(data)), EVENT_NEEDS[event]);
57
+ (data) => scope.runQuiet(listener, convert(structuredClone(data))), EVENT_NEEDS[event]);
42
58
  const handle = {
43
59
  dispose: async () => {
44
60
  scope.forget(handle);
@@ -66,7 +66,23 @@ export declare class PlayerService implements StreamSource {
66
66
  current(): Promise<PlayerState>;
67
67
  /** Drop anything older than what was already delivered. */
68
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;
69
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;
70
86
  refresh(): Promise<PlayerState>;
71
87
  /**
72
88
  * Submit an intent and wait for its definite outcome. A reply from the host
@@ -6,7 +6,7 @@ import { HostError } from "../wire.js";
6
6
  import { Latest } from "./latest.js";
7
7
  import { abortError, failure, quiet, sleep, uncertain } from "./errors.js";
8
8
  import { openStream, resolvedStream } from "./stream.js";
9
- import { checkOn, playerOn } from "./player/events.js";
9
+ import { SOURCE_EVENTS, checkOn, playerOn } from "./player/events.js";
10
10
  import { validStep } from "./player/rules.js";
11
11
  const raws = new WeakMap();
12
12
  const empty = {
@@ -42,6 +42,7 @@ const audioTrack = (track) => Object.freeze({
42
42
  codec: track.codec,
43
43
  title: track.title,
44
44
  language: track.language,
45
+ streamIndex: track.streamIndex ?? null,
45
46
  });
46
47
  /**
47
48
  * The key of `plugin.player.state` in the runtime's field tracking
@@ -278,6 +279,21 @@ export class PlayerService {
278
279
  // A precise pass: only the bindings that read a changed field.
279
280
  this.runtime.changed(PLAYER, changed);
280
281
  }
282
+ /**
283
+ * A source event's data (`source.opening`, `source.ready`,
284
+ * `source.ended`) as the listener gets it: `state` as a
285
+ * {@link PlayerState}. The state of an opening or a ready is the
286
+ * player's newest, so `plugin.player.state` takes it first (when it is
287
+ * newer than what the plugin has): `isCurrent(state)` and `ifCurrent`
288
+ * then hold after a wake too, without a read. An ended source's state
289
+ * is not the player's current one and is not taken.
290
+ */
291
+ sourceEvent(event, data) {
292
+ const message = data;
293
+ if (event !== "source.ended")
294
+ this.accept(message.state);
295
+ return { ...message, state: view(message.state) };
296
+ }
281
297
  watch(watcher) {
282
298
  void this.start();
283
299
  this.#watchers.add(watcher);
@@ -289,6 +305,22 @@ export class PlayerService {
289
305
  },
290
306
  };
291
307
  }
308
+ /**
309
+ * `watch`, for `plugin.player.stream`: the copy hears `source.state` while
310
+ * the watcher is open, with or without `subscribe()`, and the plugin
311
+ * counts as listening to it (`Connection.follow`).
312
+ */
313
+ follow(watcher, why) {
314
+ void this.start();
315
+ this.#watchers.add(watcher);
316
+ const followed = this.runtime.follow("source.state", why);
317
+ return {
318
+ dispose: async () => {
319
+ this.#watchers.delete(watcher);
320
+ await followed.dispose();
321
+ },
322
+ };
323
+ }
292
324
  async refresh() {
293
325
  this.accept(await this.runtime.host.source.state());
294
326
  return this.state;
@@ -618,6 +650,48 @@ export function player(scope) {
618
650
  },
619
651
  });
620
652
  },
653
+ stream(listener) {
654
+ if (typeof listener !== "function")
655
+ throw failure("invalid", "stream takes a function");
656
+ const copy = service();
657
+ const site = scope.runtime.callSite();
658
+ let open = true;
659
+ let called = false;
660
+ // `player.state` is updated first (`accept`), then the listener runs,
661
+ // then an update pass (`run`).
662
+ const call = () => {
663
+ if (!open)
664
+ return;
665
+ called = true;
666
+ scope.run(listener, copy.state);
667
+ };
668
+ const followed = copy.follow(call, () => {
669
+ const at = site();
670
+ return at ? `a stream opened at ${at}` : "a stream";
671
+ });
672
+ // The current state once: read now. An event that came first was
673
+ // the current state already, and the read then changed nothing.
674
+ void copy
675
+ .refresh()
676
+ .catch(() => { })
677
+ .then(() => {
678
+ if (!called)
679
+ call();
680
+ });
681
+ const handle = {
682
+ dispose: async () => {
683
+ if (!open)
684
+ return;
685
+ open = false;
686
+ scope.forget(handle);
687
+ await followed.dispose();
688
+ },
689
+ };
690
+ scope.add(handle);
691
+ return Object.freeze({
692
+ stop: () => void handle.dispose(),
693
+ });
694
+ },
621
695
  refresh: () => service().refresh(),
622
696
  subscribe: () => service().subscribe(),
623
697
  read: () => service().read(),
@@ -626,6 +700,8 @@ export function player(scope) {
626
700
  // (it hears each event first in any case).
627
701
  checkOn(event, listener);
628
702
  scope.followCopy(event);
703
+ if (SOURCE_EVENTS.has(event))
704
+ return playerOn(scope, event, listener, (data) => service().sourceEvent(event, data));
629
705
  return playerOn(scope, event, listener);
630
706
  },
631
707
  isCurrent(state) {
@@ -2,7 +2,7 @@
2
2
  import type { MotionEngine } from "./types.ts";
3
3
  import type { Change } from "./passes.ts";
4
4
  import type { Runtime } from "./runtime.ts";
5
- import type { Disposable, DisposeInfo, Plugin, PluginError, Scope, StopReason } from "./types.ts";
5
+ import type { Disposable, DisposeInfo, Plugin, PluginError, Scope, StopReason, Trigger } from "./types.ts";
6
6
  export declare class ScopeImpl implements Plugin {
7
7
  #private;
8
8
  readonly runtime: Runtime;
@@ -11,6 +11,8 @@ export declare class ScopeImpl implements Plugin {
11
11
  get dataDirectory(): string;
12
12
  get stopReason(): StopReason | undefined;
13
13
  get disposed(): boolean;
14
+ /** The plugin itself, not a scope made with `scope()`. */
15
+ get root(): boolean;
14
16
  /**
15
17
  * Start the SDK's copy that `event` keeps current (`plugin.commands.keys`,
16
18
  * `plugin.timeline`, `plugin.appearance`), for `player.on`; nothing for
@@ -60,6 +62,7 @@ export declare class ScopeImpl implements Plugin {
60
62
  readonly wire: import("./types.ts").Wire;
61
63
  get app(): import("./types.ts").App;
62
64
  scope(): Scope;
65
+ trigger(name?: string): Trigger;
63
66
  add<T extends Disposable>(item: T): T;
64
67
  /** Handles call this when disposed directly, so the scope forgets them. */
65
68
  forget(item: Disposable): void;
@@ -1,5 +1,6 @@
1
1
  import { failure, toError } from "./errors.js";
2
2
  import { commands, keysOf, palette } from "./commands.js";
3
+ import { triggersOf } from "./triggers.js";
3
4
  import { WHEN } from "./when.js";
4
5
  import { dialogs } from "./services/dialogs.js";
5
6
  import { ui } from "./view/image.js";
@@ -60,6 +61,10 @@ export class ScopeImpl {
60
61
  get disposed() {
61
62
  return this.#disposed || (this.#parent?.disposed ?? false);
62
63
  }
64
+ /** The plugin itself, not a scope made with `scope()`. */
65
+ get root() {
66
+ return this.#parent === undefined;
67
+ }
63
68
  /**
64
69
  * Start the SDK's copy that `event` keeps current (`plugin.commands.keys`,
65
70
  * `plugin.timeline`, `plugin.appearance`), for `player.on`; nothing for
@@ -114,6 +119,9 @@ export class ScopeImpl {
114
119
  scope() {
115
120
  return this.add(new ScopeImpl(this.runtime, this));
116
121
  }
122
+ trigger(name) {
123
+ return triggersOf(this.runtime).declare(this, name);
124
+ }
117
125
  add(item) {
118
126
  if (this.#disposed)
119
127
  void item.dispose().catch(() => { });
@@ -9,7 +9,8 @@
9
9
  import { HostError } from "../../wire.js";
10
10
  import { BAR_BUTTON_ICONS, BAR_BUTTON_LIMITS, BUILTIN_COMMANDS, COMMAND_LIMITS, MENU_IDS, thenProblem, } from "../../commands.js";
11
11
  import { canonical, keyLabel } from "../bindings.js";
12
- import { evaluate } from "../when.js";
12
+ import { evaluate, readsPositionOrTime } from "../when.js";
13
+ import { conditionFacts } from "./triggers.js";
13
14
  import { PALETTE_LEAD_LIMITS, PALETTE_LEAD_STATES } from "../../commands.js";
14
15
  import { menuKeys, textKey } from "../../i18n.js";
15
16
  import { commandInvalid, commandConflict, commandText, menuTextKey, checkDeclaration, } from "./declarations.js";
@@ -145,31 +146,24 @@ function bindingRows(host, inner) {
145
146
  });
146
147
  return commands;
147
148
  }
149
+ /**
150
+ * A command's `when` as the player refuses it (`conditions.rs`
151
+ * `Conditions::parse`): a position or time condition is a trigger's.
152
+ */
153
+ function commandWhenChecked(when) {
154
+ if (!when || typeof when !== "object")
155
+ return;
156
+ for (const [name, condition] of Object.entries(when))
157
+ if (condition && readsPositionOrTime(condition))
158
+ throw new HostError("invalid", `${name}: a position or time condition is for a trigger (plugin.trigger), not a command's state`);
159
+ }
148
160
  /** A command's state as the player shows it: its conditions evaluated. */
149
161
  export function commandShown(host, _inner, sent, when) {
150
162
  if (!when)
151
163
  return sent;
152
- const source = host.source;
153
- const capabilities = source.capabilities;
154
- const facts = {
155
- phase: source.phase,
156
- paused: source.paused,
157
- kind: source.kind === "producer" ? "stream" : "media",
158
- can: {
159
- pause: capabilities.pause,
160
- seek: capabilities.seek,
161
- rate: capabilities.rate,
162
- restart: capabilities.restart,
163
- close: capabilities.close,
164
- snapshot: capabilities.snapshotSource,
165
- // As the player: the flags merged into the source state.
166
- next: source.sequence?.next === true,
167
- previous: source.sequence?.previous === true,
168
- },
169
- fullscreen: host.mainWindow.fullscreen,
170
- theme: host.appearance,
171
- language: host.language,
172
- };
164
+ // As the player: the same facts as a trigger's (next and previous are
165
+ // the flags merged into the source state).
166
+ const facts = conditionFacts(host);
173
167
  const shown = { ...sent };
174
168
  if (when.enabled) {
175
169
  shown.enabled = evaluate(when.enabled, facts);
@@ -338,6 +332,7 @@ export function commandsHandlers(host, inner) {
338
332
  host.commands.size + host.coreCommands.size + hostOnly.length >=
339
333
  COMMAND_LIMITS.total)
340
334
  throw new HostError("limit", "command registry limit reached");
335
+ commandWhenChecked(p.when);
341
336
  let sent = p.state;
342
337
  host.commands.set(id, {
343
338
  declaration,
@@ -394,6 +389,7 @@ export function commandsHandlers(host, inner) {
394
389
  catch (error) {
395
390
  throw new HostError("command", error.message);
396
391
  }
392
+ commandWhenChecked(p.when);
397
393
  command.revision = Number(p.revision);
398
394
  command.state = p.state;
399
395
  if (p.when)