@vestra-player/sdk 1.9.0 → 1.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/CHANGELOG.md +44 -7
  2. package/bin/vestra-plugin.mjs +89 -8
  3. package/capability-scan.json +20 -0
  4. package/dist/api-requests.d.ts +4 -3
  5. package/dist/api-requests.js +11 -3
  6. package/dist/commands.js +1 -0
  7. package/dist/condition-table.d.ts +1889 -0
  8. package/dist/condition-table.js +562 -0
  9. package/dist/docs-tree.json.gz +0 -0
  10. package/dist/i18n-tools.d.ts +7 -0
  11. package/dist/i18n-tools.js +66 -3
  12. package/dist/player-runtime.d.ts +3 -0
  13. package/dist/player-runtime.js +3 -0
  14. package/dist/plugin/connection.d.ts +13 -2
  15. package/dist/plugin/connection.js +67 -4
  16. package/dist/plugin/index.d.ts +7 -0
  17. package/dist/plugin/index.js +13 -0
  18. package/dist/plugin/mpv.js +39 -5
  19. package/dist/plugin/player/events.d.ts +14 -5
  20. package/dist/plugin/player/events.js +19 -3
  21. package/dist/plugin/player.d.ts +16 -0
  22. package/dist/plugin/player.js +77 -1
  23. package/dist/plugin/scope.d.ts +4 -1
  24. package/dist/plugin/scope.js +8 -0
  25. package/dist/plugin/testing/commands.d.ts +5 -0
  26. package/dist/plugin/testing/commands.js +23 -24
  27. package/dist/plugin/testing/index.d.ts +134 -0
  28. package/dist/plugin/testing/index.js +180 -21
  29. package/dist/plugin/testing/media.d.ts +6 -0
  30. package/dist/plugin/testing/media.js +15 -2
  31. package/dist/plugin/testing/services.js +50 -1
  32. package/dist/plugin/testing/source-events.d.ts +28 -0
  33. package/dist/plugin/testing/source-events.js +66 -0
  34. package/dist/plugin/testing/transport.js +5 -0
  35. package/dist/plugin/testing/triggers.d.ts +82 -0
  36. package/dist/plugin/testing/triggers.js +448 -0
  37. package/dist/plugin/timeline.d.ts +3 -3
  38. package/dist/plugin/timeline.js +3 -3
  39. package/dist/plugin/triggers.d.ts +55 -0
  40. package/dist/plugin/triggers.js +233 -0
  41. package/dist/plugin/types/base.d.ts +117 -3
  42. package/dist/plugin/types/commands.d.ts +374 -1
  43. package/dist/plugin/types/packages.d.ts +10 -1
  44. package/dist/plugin/types/playback.d.ts +257 -9
  45. package/dist/plugin/types/services.d.ts +10 -4
  46. package/dist/plugin/when.d.ts +78 -4
  47. package/dist/plugin/when.js +182 -14
  48. package/dist/slint-version.d.ts +7 -0
  49. package/dist/slint-version.js +7 -0
  50. package/dist/wire.d.ts +18 -0
  51. package/dist/wire.js +12 -0
  52. package/llms.txt +4 -4
  53. package/package.json +2 -2
@@ -0,0 +1,55 @@
1
+ import type { Runtime } from "./runtime.ts";
2
+ import type { ScopeImpl } from "./scope.ts";
3
+ import type { Trigger } from "./types.ts";
4
+ /**
5
+ * The player's limits on triggers (`plugin.trigger`): how many a plugin
6
+ * declares and the player keeps conditions for, and how long a name is.
7
+ * A 65th trigger throws `limit`; the player refuses a 65th kept condition
8
+ * with `limit` too. A position list holds at most `TIMELINE_LIMITS.ranges`
9
+ * ranges.
10
+ *
11
+ * @example Stay within the limit
12
+ * A plugin that makes one trigger per item of a list keeps the list
13
+ * within the number of triggers the player allows.
14
+ * ```ts
15
+ * import { TRIGGER_LIMITS } from "@vestra-player/sdk/testing";
16
+ *
17
+ * const names = ["sleep", "wake"].slice(0, TRIGGER_LIMITS.triggers);
18
+ * ```
19
+ *
20
+ * @since 1.10.0
21
+ *
22
+ * @category Testing
23
+ */
24
+ export declare const TRIGGER_LIMITS: Readonly<{
25
+ /** Triggers a plugin declares, and conditions the player keeps for it. */
26
+ triggers: 64;
27
+ /** Characters of a trigger's name. */
28
+ nameChars: 64;
29
+ }>;
30
+ /** One plugin's triggers (a runtime service). */
31
+ export declare class TriggerRegistry {
32
+ #private;
33
+ readonly runtime: Runtime;
34
+ constructor(runtime: Runtime);
35
+ /** `activate()` begins: triggers may be declared until `end`. */
36
+ begin(): void;
37
+ /**
38
+ * The synchronous part of `activate()` is over: the player learns what
39
+ * was declared (only when something was), and what came early is
40
+ * delivered.
41
+ */
42
+ end(): void;
43
+ /**
44
+ * `plugin.trigger(name?)` of `scope`. Only the plugin itself declares
45
+ * triggers, not a sub-scope (`plugin.scope()`): a trigger's condition is
46
+ * the plugin's, kept by the player beyond any one run, while a sub-scope
47
+ * is disposed within a run, and a kept condition whose functions went
48
+ * with it would go on waking the plugin for nothing.
49
+ */
50
+ declare(scope: ScopeImpl, name?: string): Trigger;
51
+ /** Nobody handles `trigger`'s notifications (T14): the player drops its condition. */
52
+ unhandled(trigger: number | string): void;
53
+ }
54
+ /** The triggers of `runtime`'s plugin. */
55
+ export declare const triggersOf: (runtime: Runtime) => TriggerRegistry;
@@ -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";
@@ -94,12 +94,12 @@ export interface Disposable {
94
94
  * | code | meaning |
95
95
  * | --- | --- |
96
96
  * | `stale` | A condition no longer held (the source or the subtitle selection changed); nothing was done. See {@link Conditional}. |
97
- * | `capability` | The call is for the player's built-in plugins only (`commands.list`, `commands.run`, `palette.lead`) and this plugin is not one. |
97
+ * | `capability` | The call is for the player's built-in plugins only (`commands.list`, `commands.run`, `palette.lead`, `app.openWindow`) and this plugin is not one. |
98
98
  * | `invalid` | A bad argument (out of range, malformed, unknown id). |
99
99
  * | `missing` | The target does not exist (an empty panel position, a missing file, an unknown command). |
100
100
  * | `busy` | The player is still doing a previous call of the same kind (a dialog is open, a step is in flight). |
101
101
  * | `conflict` | Another plugin owns the resource (the sequence provider, the display layer, the backdrop, a menu path, the command palette's lead row), or this plugin already uses the id (a window or command id). |
102
- * | `unavailable` | Not possible right now (no ready media, no user interface, fullscreen, a disabled core action). |
102
+ * | `unavailable` | Not possible right now (no ready media, no user interface, fullscreen, a disabled core action), or not in a temporary window (saving a secret, changing the plugin library; {@link AppInfo.temporary}). |
103
103
  * | `unsupported` | Not possible on this system, build or source (a platform gap, software rendering, a source that cannot seek, a filter on a generated stream, an export the player cannot make as asked: a stream copy, a source bitrate it cannot tell). |
104
104
  * | `backend` | Loading or resolving failed, or mpv failed a command or a property of {@link Mpv}; `message` is the backend's reason. |
105
105
  * | `refused` | {@link Mpv}: a command or a property the pass-through does not carry (it would start a program, load code, write files, end the player, or change what the player manages); `message` names it and why. |
@@ -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},
@@ -975,6 +1011,40 @@ export interface AppInfo {
975
1011
  /** The linked Slint version, e.g. `"1.18.1"`. */
976
1012
  readonly slint: string;
977
1013
  };
1014
+ /**
1015
+ * `true` when this player is a temporary window (a guest, like a
1016
+ * browser's guest window): a second player the main one started with
1017
+ * {@link App.openWindow} (the playlist's Play in New Window), or one the
1018
+ * user started with `--guest`. Absent in the main player and in a player
1019
+ * before 0.10.3.
1020
+ *
1021
+ * @remarks
1022
+ * A temporary window runs with a temporary profile that is deleted when
1023
+ * it closes. Only the player's own settings (look, language, keys,
1024
+ * rendering, window layout) are copied from the main window. A plugin's
1025
+ * data directory (its options, history and lists included) and its
1026
+ * session storage start empty and are discarded when the window closes;
1027
+ * nothing a plugin writes reaches the main window. The plugin library and the keychain are
1028
+ * the main window's, read-only: {@link Secrets.get} reads the main
1029
+ * window's values, while {@link Secrets.set}, {@link Secrets.delete},
1030
+ * {@link PackageCandidate.install}, {@link Packages.uninstall} and
1031
+ * {@link Packages.setEnabled} reject with `unavailable`. A plugin that
1032
+ * does background work for the user's profile (an automatic update
1033
+ * check, a sync) skips it here; work the user starts still runs.
1034
+ *
1035
+ * @example Skip the automatic update check in a temporary window
1036
+ * The main window checks for updates once a day; a temporary window
1037
+ * leaves it to the main one. A check the user asks for still runs.
1038
+ * ```ts
1039
+ * declare function checkForUpdates(): Promise<void>;
1040
+ * // ---cut---
1041
+ * if (!plugin.app.temporary) void checkForUpdates();
1042
+ * ```
1043
+ *
1044
+ * @player 0.10.3
1045
+ * @since 1.10.1
1046
+ */
1047
+ readonly temporary?: boolean;
978
1048
  }
979
1049
  /**
980
1050
  * `plugin.app`: the fixed facts plus the system, read on request.
@@ -1021,6 +1091,50 @@ export interface App extends AppInfo {
1021
1091
  * @since 1.0.0
1022
1092
  */
1023
1093
  system(): Promise<SystemInfo>;
1094
+ /**
1095
+ * Open the files in a new temporary window: a second player with a
1096
+ * temporary profile ({@link AppInfo.temporary}) that plays the first file
1097
+ * and lists them all. Resolves once its process started, with its
1098
+ * process id. **Built-in plugins only.**
1099
+ *
1100
+ * @remarks
1101
+ * The playlist's Play in New Window. The new window is a separate player
1102
+ * process and keeps running after this one quits; it shares nothing with
1103
+ * the calling plugin.
1104
+ *
1105
+ * @param paths 1 to 64 absolute paths of existing files, in play order.
1106
+ *
1107
+ * @throws `capability` (rejects) The plugin is not built into the player.
1108
+ * @throws `invalid` (rejects) No path, more than 64, or a path that is not
1109
+ * an absolute path.
1110
+ * @throws `missing` (rejects) A path that is not an existing file.
1111
+ * @throws `unsupported` (rejects) On macOS (not yet), or a player without
1112
+ * windows.
1113
+ * @throws `failed` (rejects) The new player did not start.
1114
+ *
1115
+ * @example Play the selected files in a new window
1116
+ * The playlist's row menu opens the selected local files in a temporary
1117
+ * window, which plays them without changing the main window's list or
1118
+ * settings.
1119
+ * ```ts
1120
+ * declare const selected: string[];
1121
+ * // ---cut---
1122
+ * try {
1123
+ * const { pid } = await plugin.app.openWindow(selected.slice(0, 64));
1124
+ * console.log("temporary window", pid);
1125
+ * } catch (error) {
1126
+ * // `capability`: not a built-in plugin; `unsupported`: macOS.
1127
+ * if ((error as PluginError).code !== "capability") throw error;
1128
+ * }
1129
+ * ```
1130
+ *
1131
+ * @player 0.10.3
1132
+ * @since 1.10.1
1133
+ */
1134
+ openWindow(paths: readonly string[]): Promise<{
1135
+ /** The new player's process id. */
1136
+ readonly pid: number;
1137
+ }>;
1024
1138
  }
1025
1139
  /**
1026
1140
  * What {@link App.system} reports.