@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
@@ -0,0 +1,233 @@
1
+ /**
2
+ * `plugin.trigger(name?)` (1.10.0, events-and-triggers.md §3.1, §4.1):
3
+ * triggers declared in the synchronous part of `activate()`, their
4
+ * conditions handed to the player (`triggers.set`, `triggers.off`), and the
5
+ * player's `triggers.fired` notifications routed to their `enter` and
6
+ * `leave`.
7
+ *
8
+ * Identity: an unnamed trigger is its place among the unnamed ones (the
9
+ * order `activate()` declares them in), a named one its name. At the end of
10
+ * the synchronous part the player is told which names and how many unnamed
11
+ * ones were declared (`triggers.declare`), so it can drop a kept condition
12
+ * nobody declares any more (T14).
13
+ *
14
+ * Sequence numbers (T6): each `set` and `off` answers one; a notification
15
+ * with an older one than the trigger's newest answer is dropped, and one
16
+ * that arrives while a `set` or `off` is in flight waits for its answer.
17
+ */
18
+ import { HostError } from "../wire.js";
19
+ import { failure } from "./errors.js";
20
+ import { playerOf } from "./player.js";
21
+ import { conditionJson, positionLists } from "./when.js";
22
+ /**
23
+ * The player's limits on triggers (`plugin.trigger`): how many a plugin
24
+ * declares and the player keeps conditions for, and how long a name is.
25
+ * A 65th trigger throws `limit`; the player refuses a 65th kept condition
26
+ * with `limit` too. A position list holds at most `TIMELINE_LIMITS.ranges`
27
+ * ranges.
28
+ *
29
+ * @example Stay within the limit
30
+ * A plugin that makes one trigger per item of a list keeps the list
31
+ * within the number of triggers the player allows.
32
+ * ```ts
33
+ * import { TRIGGER_LIMITS } from "@vestra-player/sdk/testing";
34
+ *
35
+ * const names = ["sleep", "wake"].slice(0, TRIGGER_LIMITS.triggers);
36
+ * ```
37
+ *
38
+ * @since 1.10.0
39
+ *
40
+ * @category Testing
41
+ */
42
+ export const TRIGGER_LIMITS = Object.freeze({
43
+ /** Triggers a plugin declares, and conditions the player keeps for it. */
44
+ triggers: 64,
45
+ /** Characters of a trigger's name. */
46
+ nameChars: 64,
47
+ });
48
+ class TriggerImpl {
49
+ id;
50
+ #registry;
51
+ #scope;
52
+ #enter;
53
+ #leave;
54
+ /** The newest sequence number the player answered. */
55
+ #known = 0;
56
+ /** `set` and `off` requests without an answer yet. */
57
+ #inFlight = 0;
58
+ /** Notifications that came while one was in flight. */
59
+ #held = [];
60
+ /** The handlers run one after another (T9). */
61
+ #chain = Promise.resolve();
62
+ constructor(registry, scope, id) {
63
+ this.#registry = registry;
64
+ this.#scope = scope;
65
+ this.id = id;
66
+ }
67
+ get handled() {
68
+ return this.#enter !== undefined || this.#leave !== undefined;
69
+ }
70
+ firesOn(condition) {
71
+ const when = conditionJson(condition);
72
+ if (positionLists(when) > 1)
73
+ throw new TypeError("a condition has at most one position list");
74
+ this.#send(this.#registry.runtime.host.triggers.set(this.id, when));
75
+ return this;
76
+ }
77
+ enter(fn) {
78
+ if (typeof fn !== "function")
79
+ throw new TypeError("enter takes a function");
80
+ this.#enter = fn;
81
+ return this;
82
+ }
83
+ leave(fn) {
84
+ if (typeof fn !== "function")
85
+ throw new TypeError("leave takes a function");
86
+ this.#leave = fn;
87
+ return this;
88
+ }
89
+ off() {
90
+ this.#send(this.#registry.runtime.host.triggers.off(this.id));
91
+ return this;
92
+ }
93
+ #send(request) {
94
+ this.#inFlight++;
95
+ request.then(({ seq }) => {
96
+ this.#known = Math.max(this.#known, seq);
97
+ this.#settled();
98
+ }, (error) => {
99
+ this.#settled();
100
+ if (!(error instanceof HostError && error.code === "disabled"))
101
+ this.#scope.report(error);
102
+ });
103
+ }
104
+ #settled() {
105
+ this.#inFlight--;
106
+ if (this.#inFlight > 0)
107
+ return;
108
+ for (const fired of this.#held.splice(0))
109
+ this.fired(fired);
110
+ }
111
+ /** A notification for this trigger. */
112
+ fired(fired) {
113
+ if (this.#inFlight > 0) {
114
+ this.#held.push(fired);
115
+ return;
116
+ }
117
+ if (fired.seq < this.#known)
118
+ return;
119
+ if (!this.handled) {
120
+ this.#registry.unhandled(fired.trigger);
121
+ return;
122
+ }
123
+ const fn = fired.edge === "enter" ? this.#enter : this.#leave;
124
+ if (!fn)
125
+ return;
126
+ const state = playerOf(this.#registry.runtime).sourceEvent("triggers.fired", { state: fired.state });
127
+ const event = Object.freeze(fired.range
128
+ ? { state: state.state, range: Object.freeze({ ...fired.range }) }
129
+ : { state: state.state });
130
+ const scope = this.#scope;
131
+ this.#chain = this.#chain.then(async () => {
132
+ if (scope.disposed)
133
+ return;
134
+ try {
135
+ await fn(event);
136
+ }
137
+ catch (error) {
138
+ scope.report(error);
139
+ }
140
+ finally {
141
+ scope.update();
142
+ }
143
+ });
144
+ }
145
+ }
146
+ /** One plugin's triggers (a runtime service). */
147
+ export class TriggerRegistry {
148
+ runtime;
149
+ /** `activate()`'s synchronous part runs (`PluginInstance.activate`). */
150
+ #declaring = false;
151
+ /** That part is over: notifications go to their triggers. */
152
+ #declared = false;
153
+ #byId = new Map();
154
+ #unnamed = 0;
155
+ /** Notifications that came before the triggers were declared. */
156
+ #early = [];
157
+ constructor(runtime) {
158
+ this.runtime = runtime;
159
+ runtime.raw("triggers.fired", (data) => this.#fired(data));
160
+ }
161
+ /** `activate()` begins: triggers may be declared until `end`. */
162
+ begin() {
163
+ this.#declaring = true;
164
+ }
165
+ /**
166
+ * The synchronous part of `activate()` is over: the player learns what
167
+ * was declared (only when something was), and what came early is
168
+ * delivered.
169
+ */
170
+ end() {
171
+ this.#declaring = false;
172
+ this.#declared = true;
173
+ if (this.#byId.size) {
174
+ const names = [...this.#byId.keys()].filter((id) => typeof id === "string");
175
+ this.runtime.host.triggers
176
+ .declare(names, this.#unnamed)
177
+ .catch((error) => {
178
+ if (!(error instanceof HostError && error.code === "disabled"))
179
+ console.error(`[${this.runtime.manifest.id}] triggers.declare:`, error);
180
+ });
181
+ }
182
+ for (const fired of this.#early.splice(0))
183
+ this.#fired(fired);
184
+ }
185
+ /**
186
+ * `plugin.trigger(name?)` of `scope`. Only the plugin itself declares
187
+ * triggers, not a sub-scope (`plugin.scope()`): a trigger's condition is
188
+ * the plugin's, kept by the player beyond any one run, while a sub-scope
189
+ * is disposed within a run, and a kept condition whose functions went
190
+ * with it would go on waking the plugin for nothing.
191
+ */
192
+ declare(scope, name) {
193
+ if (!this.#declaring)
194
+ throw new Error("plugin.trigger() is called only in the synchronous part of activate(), before its first await");
195
+ if (!scope.root)
196
+ throw new Error("plugin.trigger() is called on the plugin, not on a scope made with plugin.scope()");
197
+ if (this.#byId.size >= TRIGGER_LIMITS.triggers)
198
+ throw failure("limit", `a plugin has at most ${TRIGGER_LIMITS.triggers} triggers`);
199
+ let id;
200
+ if (name === undefined)
201
+ id = this.#unnamed++;
202
+ else {
203
+ if (typeof name !== "string" ||
204
+ name.length === 0 ||
205
+ [...name].length > TRIGGER_LIMITS.nameChars ||
206
+ /\p{Cc}/u.test(name))
207
+ throw failure("invalid", `a trigger's name is 1–${TRIGGER_LIMITS.nameChars} characters`);
208
+ if (this.#byId.has(name))
209
+ throw failure("invalid", `a trigger named "${name}" exists already`);
210
+ id = name;
211
+ }
212
+ const trigger = new TriggerImpl(this, scope, id);
213
+ this.#byId.set(id, trigger);
214
+ return trigger;
215
+ }
216
+ /** Nobody handles `trigger`'s notifications (T14): the player drops its condition. */
217
+ unhandled(trigger) {
218
+ this.runtime.host.triggers.off(trigger, true).catch(() => { });
219
+ }
220
+ #fired(fired) {
221
+ if (!this.#declared) {
222
+ this.#early.push(fired);
223
+ return;
224
+ }
225
+ const trigger = this.#byId.get(fired.trigger);
226
+ if (trigger)
227
+ trigger.fired(fired);
228
+ else
229
+ this.unhandled(fired.trigger);
230
+ }
231
+ }
232
+ /** The triggers of `runtime`'s plugin. */
233
+ export const triggersOf = (runtime) => runtime.service("triggers", () => new TriggerRegistry(runtime));
@@ -10,7 +10,7 @@ import type { ExportJob, MediaSources, Snapshot } from "./snapshots.ts";
10
10
  import type { Resolver } from "./sources.ts";
11
11
  import type { PropValue, View } from "./ui-content.ts";
12
12
  import type { Panel, Window } from "./ui-layout.ts";
13
- import type { Bindings, Commands, Host, Palette, When } from "./commands.ts";
13
+ import type { Bindings, Commands, Host, Palette, Trigger, When } from "./commands.ts";
14
14
  import type { Packages } from "./packages.ts";
15
15
  import type { Mpv, Player, Timeline } from "./playback.ts";
16
16
  import type { AudioFilter, RenderOptions, VideoFilter, MotionEngine, VideoMotion, VideoShaders, VideoUpscaling, VideoView } from "./processing.ts";
@@ -660,6 +660,42 @@ export interface Plugin extends Scope {
660
660
  * {@link Scope.onDispose} cleanup runs. See {@link StopReason}.
661
661
  */
662
662
  readonly stopReason: StopReason | undefined;
663
+ /**
664
+ * Makes a trigger: a condition the player keeps and evaluates, and the
665
+ * functions it calls when the condition becomes true and false, also
666
+ * waking the plugin from standby. Called only in the synchronous part of
667
+ * `activate()` (before its first `await`). Without a name the trigger is
668
+ * known by its place among the unnamed ones, and a condition it was
669
+ * given is cleared when the plugin is upgraded; with a name it is known
670
+ * by the name and survives an upgrade. At most 64 per plugin.
671
+ *
672
+ * @param name - Unique within the plugin, 1–64 characters.
673
+ * @throws `Error` without a code (throws) Called after the synchronous
674
+ * part of `activate()`, or on a scope made with `plugin.scope()` (a
675
+ * trigger is the plugin's: its condition outlives any scope).
676
+ * @throws `invalid` (throws) A name used twice, or not 1–64 characters.
677
+ * @throws `limit` (throws) A 65th trigger.
678
+ *
679
+ * @example A sleep timer that survives an update
680
+ * A menu item pauses playback 45 minutes later. The trigger is named,
681
+ * so its condition stays with the player through standby and an update,
682
+ * and `enter` disarms it before pausing.
683
+ * ```ts
684
+ * const { when, player, commands } = plugin;
685
+ * const alarm = plugin.trigger("sleep").enter(async () => {
686
+ * alarm.off(); // fire once: the player forgets the condition
687
+ * await player.setPaused(true);
688
+ * });
689
+ * commands.add({
690
+ * id: "in45",
691
+ * path: ["Tools", "Sleep in 45 Minutes"],
692
+ * run: () => alarm.firesOn(when.time({ min: Date.now() + 45 * 60_000 })),
693
+ * });
694
+ * ```
695
+ *
696
+ * @since 1.10.0
697
+ */
698
+ trigger(name?: string): Trigger;
663
699
  }
664
700
  /**
665
701
  * Why the player stopped the plugin ({@link DisposeInfo.reason},
@@ -1,5 +1,5 @@
1
1
  import type { Scope } from "./base.ts";
2
- import type { Player } from "./playback.ts";
2
+ import type { AudioTrack, Player, TimelineLayer } from "./playback.ts";
3
3
  import type { Ui, View } from "./ui-content.ts";
4
4
  import type { Disposable } from "./base.ts";
5
5
  import type { PlayerState } from "./playback.ts";
@@ -252,6 +252,379 @@ export interface When {
252
252
  * @since 1.5.40
253
253
  */
254
254
  language(language: LanguagePreference): Condition;
255
+ /**
256
+ * The source is muted (`PlayerState.muted`).
257
+ *
258
+ * @since 1.10.0
259
+ */
260
+ readonly muted: Condition;
261
+ /**
262
+ * The main window is maximized (`MainWindowState.maximized`).
263
+ *
264
+ * @since 1.10.0
265
+ */
266
+ readonly maximized: Condition;
267
+ /**
268
+ * The video shows in the picture-in-picture window.
269
+ *
270
+ * @since 1.10.0
271
+ */
272
+ readonly pip: Condition;
273
+ /**
274
+ * An A-B loop is set (`PlayerState.loop` is not `null`).
275
+ *
276
+ * @since 1.10.0
277
+ */
278
+ readonly looping: Condition;
279
+ /**
280
+ * The source's duration in seconds is within `min ≤ duration < max`
281
+ * (at least one bound); false while it is unknown.
282
+ *
283
+ * @throws `TypeError` (throws) No bound, a bound that is not a finite
284
+ * number, or `min ≥ max`.
285
+ *
286
+ * @example Only for files longer than ten minutes
287
+ * The condition holds once the player knows the file lasts 600 seconds
288
+ * or more; with no `max` the range is open upwards.
289
+ * ```ts
290
+ * const { when } = plugin;
291
+ * const long = when.duration({ min: 600 });
292
+ * ```
293
+ *
294
+ * @since 1.10.0
295
+ */
296
+ duration(bounds: {
297
+ min?: number;
298
+ max?: number;
299
+ }): Condition;
300
+ /**
301
+ * The volume (0–200) is within `min ≤ volume < max`.
302
+ *
303
+ * @throws `TypeError` (throws) As {@link When.duration}.
304
+ *
305
+ * @example Check a Boost item above full volume
306
+ * The item carries a check mark while the volume is above 100, whoever
307
+ * raised it; the player compares its own volume, so the plugin need
308
+ * not follow the state.
309
+ * ```ts
310
+ * const { when } = plugin;
311
+ * const boosted = when.volume({ min: 101 });
312
+ * ```
313
+ *
314
+ * @since 1.10.0
315
+ */
316
+ volume(bounds: {
317
+ min?: number;
318
+ max?: number;
319
+ }): Condition;
320
+ /**
321
+ * The playback rate is within `min ≤ rate < max`.
322
+ *
323
+ * @throws `TypeError` (throws) As {@link When.duration}.
324
+ *
325
+ * @example Enable Normal Speed only when sped up
326
+ * The item is greyed out at normal speed or slower: the condition holds
327
+ * from a rate of just over 1 upwards.
328
+ * ```ts
329
+ * const { when } = plugin;
330
+ * const fast = when.rate({ min: 1.01 });
331
+ * ```
332
+ *
333
+ * @since 1.10.0
334
+ */
335
+ rate(bounds: {
336
+ min?: number;
337
+ max?: number;
338
+ }): Condition;
339
+ /**
340
+ * The wall clock (Unix milliseconds, as `Date.now()`) is within
341
+ * `min ≤ now < max`. For a trigger only (`plugin.trigger().firesOn`): a
342
+ * command's state refuses it.
343
+ *
344
+ * @throws `TypeError` (throws) As {@link When.duration}.
345
+ *
346
+ * @example The clock is at or past 45 minutes from now
347
+ * The bound is computed once, when the condition is made, so the
348
+ * condition turns true 45 minutes later and stays true.
349
+ * ```ts
350
+ * const { when } = plugin;
351
+ * const due = when.time({ min: Date.now() + 45 * 60_000 });
352
+ * ```
353
+ *
354
+ * @since 1.10.0
355
+ */
356
+ time(bounds: {
357
+ min?: number;
358
+ max?: number;
359
+ }): Condition;
360
+ /**
361
+ * The selected audio track is `id` ({@link AudioTrack.id}).
362
+ *
363
+ * @throws `TypeError` (throws) `id` is not an integer.
364
+ *
365
+ * @example Check the item of each audio track
366
+ * One menu item per track of the file carries a check mark while that
367
+ * track plays, also when the user picks it in the player's own menu.
368
+ * ```ts
369
+ * const { when, player } = plugin;
370
+ * for (const track of player.state.audioTracks ?? [])
371
+ * plugin.commands.add({
372
+ * id: `audio-${track.id}`,
373
+ * path: ["@audio", "Track", track.title ?? `Track ${track.id}`],
374
+ * radio: true,
375
+ * checked: when.audioTrack(track.id), // the player keeps the mark current
376
+ * run: () => player.selectAudioTrack(track.id),
377
+ * });
378
+ * ```
379
+ *
380
+ * @since 1.10.0
381
+ */
382
+ audioTrack(id: number): Condition;
383
+ /**
384
+ * The primary subtitle track is `id` (`SubtitleTrack.id`).
385
+ *
386
+ * @throws `TypeError` (throws) `id` is not a string of 1–256 characters.
387
+ *
388
+ * @example Check the item of a subtitle track
389
+ * The item carries a check mark while the track with this id is the
390
+ * primary subtitle, whoever selected it.
391
+ * ```ts
392
+ * declare const trackId: string;
393
+ * // ---cut---
394
+ * const { when } = plugin;
395
+ * const shown = when.subtitleTrack(trackId);
396
+ * ```
397
+ *
398
+ * @since 1.10.0
399
+ */
400
+ subtitleTrack(id: string): Condition;
401
+ /**
402
+ * The active audio output device is `id` (`PlayerState.audioOutput.active`).
403
+ *
404
+ * @throws `TypeError` (throws) `id` is not a string of 1–256 characters.
405
+ *
406
+ * @example Check the item of the active output device
407
+ * One menu item per output device carries a check mark while the
408
+ * player plays through it; the player compares its own active device.
409
+ * ```ts
410
+ * const { when, player } = plugin;
411
+ * for (const device of await player.audioDevices())
412
+ * plugin.commands.add({
413
+ * id: `out-${device.id}`,
414
+ * path: ["@audio", "Output", device.name],
415
+ * radio: true,
416
+ * checked: when.audioDevice(device.id), // no state read in the plugin
417
+ * run: () => player.setAudioDevice(device.id),
418
+ * });
419
+ * ```
420
+ *
421
+ * @since 1.10.0
422
+ */
423
+ audioDevice(id: string): Condition;
424
+ /**
425
+ * The playback position. For a trigger only: a command's state refuses
426
+ * it.
427
+ *
428
+ * @since 1.10.0
429
+ */
430
+ readonly position: {
431
+ /**
432
+ * The position is inside one of `ranges` (`start ≤ position < end`; no
433
+ * `end`: open), or less than `lead` media seconds before a range's
434
+ * start. The player binds the list to the source that is current when
435
+ * it receives it: for any other source this is false. When several
436
+ * ranges hold, the one with the earliest start is the one a trigger's
437
+ * `enter` gets as `range`. At most one position list per condition;
438
+ * it counts as one part however long it is.
439
+ *
440
+ * @param ranges - Objects with `start` and optionally `end` (media
441
+ * seconds); other fields are ignored, so the list given to
442
+ * {@link TimelineLayer.setRanges} fits as it is. At most 1000
443
+ * (`TIMELINE_LIMITS.ranges`).
444
+ * @param options - `lead`: media seconds each range starts earlier,
445
+ * to make up for the time a plugin in standby takes to start.
446
+ * @throws `TypeError` (throws) A start that is not a finite number of
447
+ * 0 or more, an end not after its start, a negative lead.
448
+ * @throws `limit` (throws) More than 1000 ranges.
449
+ *
450
+ * @example Be told 2 s before each silence
451
+ * ```ts
452
+ * declare const spans: { start: number; end: number }[];
453
+ * // ---cut---
454
+ * const { when } = plugin;
455
+ * const near = when.playing.and(when.position.in(spans, { lead: 2 }));
456
+ * ```
457
+ *
458
+ * @since 1.10.0
459
+ */
460
+ in(ranges: Iterable<PositionRange>, options?: {
461
+ lead?: number;
462
+ }): Condition;
463
+ };
464
+ }
465
+ /**
466
+ * A range of media time for {@link When.position}: any object with `start`
467
+ * and, optionally, `end`, in seconds.
468
+ *
469
+ * @since 1.10.0
470
+ * @category Commands
471
+ */
472
+ export interface PositionRange {
473
+ /** Where the range starts, in media seconds (0 or more). */
474
+ readonly start: number;
475
+ /** Where it ends, in media seconds, after `start`; absent or `null`: open. */
476
+ readonly end?: number | null;
477
+ }
478
+ /**
479
+ * What a trigger's `enter` and `leave` get.
480
+ *
481
+ * @since 1.10.0
482
+ * @category Commands
483
+ */
484
+ export interface TriggerEvent {
485
+ /** The player's state at the moment the player saw the condition flip. */
486
+ readonly state: PlayerState;
487
+ /**
488
+ * For a condition with {@link When.position}`.in`, when the position is
489
+ * in one of its ranges: that range as given, and its place in the list
490
+ * the player evaluated. `start` and `end` name the range; `index` is its
491
+ * place in that list: an `enter` that was on its way while the plugin
492
+ * gave a new list holding the same range (the trigger stays entered)
493
+ * carries the place in the old list, and the later `leave` the place in
494
+ * the new one.
495
+ */
496
+ readonly range?: {
497
+ /** The range's start, in media seconds. */
498
+ readonly start: number;
499
+ /** The range's end, in media seconds (`null`: open). */
500
+ readonly end: number | null;
501
+ /** Its place in the list given to `when.position.in`, from 0. */
502
+ readonly index: number;
503
+ };
504
+ }
505
+ /**
506
+ * A trigger (`plugin.trigger()`): a place for one condition the player
507
+ * keeps and evaluates, also while the plugin is in standby, and the
508
+ * functions to call when it becomes true (`enter`) and false (`leave`).
509
+ *
510
+ * @remarks
511
+ * When it flips while the plugin has no process, the player starts the
512
+ * plugin; `activate()` declares the same triggers again and the waiting
513
+ * notification reaches the same trigger. Every method returns the
514
+ * trigger, so the calls chain.
515
+ *
516
+ * @example On top while playing in fullscreen
517
+ * The window stays on top while a file plays in fullscreen and goes back
518
+ * when either ends. The player evaluates the condition itself, so the
519
+ * plugin may be in standby in between.
520
+ * ```ts
521
+ * const { when, window } = plugin;
522
+ * plugin
523
+ * .trigger()
524
+ * // The player keeps this condition and calls back when it flips.
525
+ * .firesOn(when.playing.and(when.fullscreen))
526
+ * .enter(() => window.setAlwaysOnTop(true))
527
+ * .leave(() => window.setAlwaysOnTop(false));
528
+ * ```
529
+ *
530
+ * @since 1.10.0
531
+ * @category Commands
532
+ */
533
+ export interface Trigger {
534
+ /**
535
+ * Hands the player this condition, replacing the one it has (without a
536
+ * `leave` for the old one). A condition already true is entered at once,
537
+ * except a range list given again for the same source while the
538
+ * position is still in the range it was entered with (the same start,
539
+ * end and lead; a list that grew as a scan went on): that range stays
540
+ * entered, with no second `enter`, and an `enter` of it still on its way
541
+ * (a plugin woken for it) reaches `enter` all the same. The same
542
+ * condition again changes nothing. A refusal (`invalid`,
543
+ * `limit`) reaches the plugin's error handlers.
544
+ *
545
+ * @throws `TypeError` (throws) Not a condition from `plugin.when`, or
546
+ * one with two position lists.
547
+ *
548
+ * @example Pause ten seconds before the end
549
+ * A command arms the trigger for the file open now: its condition holds
550
+ * from ten seconds before the end, and `enter` pauses. Pressing it again
551
+ * replaces the condition, without a `leave` for the old one.
552
+ * ```ts
553
+ * const { when, player } = plugin;
554
+ * const nearEnd = plugin.trigger("near-end").enter(() => player.setPaused(true));
555
+ * plugin.commands.add({
556
+ * id: "pause-near-end",
557
+ * path: ["Tools", "Pause Near the End"],
558
+ * run: () => {
559
+ * const duration = player.state.duration;
560
+ * // Bound to the current file: another file never matches it.
561
+ * if (duration !== null)
562
+ * nearEnd.firesOn(when.position.in([{ start: Math.max(0, duration - 10) }]));
563
+ * },
564
+ * });
565
+ * ```
566
+ *
567
+ * @since 1.10.0
568
+ */
569
+ firesOn(condition: Condition): Trigger;
570
+ /**
571
+ * `fn` runs each time the condition turns from false to true. One
572
+ * trigger's functions run one after another: the next waits for the
573
+ * promise of the one before. A second call replaces the function.
574
+ *
575
+ * @throws `TypeError` (throws) `fn` is not a function.
576
+ *
577
+ * @example Show a note as each marked range starts
578
+ * The OSD names the part of the file as playback reaches it. `range`
579
+ * says which of the ranges was entered: `index` is its place in the
580
+ * list the condition was given.
581
+ * ```ts
582
+ * declare const parts: { start: number; end: number; name: string }[];
583
+ * // ---cut---
584
+ * const { when, osd } = plugin;
585
+ * plugin
586
+ * .trigger()
587
+ * .firesOn(when.position.in(parts))
588
+ * // `range` is set because the condition has a position list.
589
+ * .enter(async ({ range }) => {
590
+ * if (range) await osd.show("Now: {}", [parts[range.index].name]);
591
+ * });
592
+ * ```
593
+ *
594
+ * @since 1.10.0
595
+ */
596
+ enter(fn: (event: TriggerEvent) => unknown): Trigger;
597
+ /**
598
+ * Optional: `fn` runs each time the condition turns from true to false.
599
+ * A second call replaces the function.
600
+ *
601
+ * @throws `TypeError` (throws) `fn` is not a function.
602
+ *
603
+ * @example See Trigger
604
+ * {@link Trigger}
605
+ *
606
+ * @since 1.10.0
607
+ */
608
+ leave(fn: (event: TriggerEvent) => unknown): Trigger;
609
+ /**
610
+ * Removes the condition, without a `leave`. The trigger stays declared
611
+ * and can be given a new condition.
612
+ *
613
+ * @example Cancel a sleep timer
614
+ * A second command disarms the timer the first one set: the player
615
+ * forgets the condition, so the plugin is not started for it.
616
+ * ```ts
617
+ * const sleep = plugin.trigger("sleep").enter(() => plugin.player.setPaused(true));
618
+ * plugin.commands.add({
619
+ * id: "sleep-cancel",
620
+ * path: ["Tools", "Cancel Sleep Timer"],
621
+ * run: () => sleep.off(), // the trigger stays; only its condition goes
622
+ * });
623
+ * ```
624
+ *
625
+ * @since 1.10.0
626
+ */
627
+ off(): Trigger;
255
628
  }
256
629
  /**
257
630
  * A built-in top-level menu of the player, named in a command path as `@<id>`