@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
@@ -124,7 +124,7 @@ export interface TimelineLayer extends Disposable {
124
124
  * marks. Disposing the layer, its scope or stopping the plugin removes both,
125
125
  * and a new source clears every layer.
126
126
  *
127
- * At most 100 ranges (`TIMELINE_LIMITS.ranges`), `0 ≤ start < end`, `end`
127
+ * At most 1000 ranges (`TIMELINE_LIMITS.ranges`), `0 ≤ start < end`, `end`
128
128
  * within the media while its duration is known, titles ≤ 100 characters.
129
129
  * Ranges may overlap. Failures go to `onError`; with `ifCurrent`, a
130
130
  * write for another source is dropped silently. A title is data or a
@@ -260,6 +260,16 @@ export interface AudioTrack {
260
260
  title: string | null;
261
261
  /** Language tag, `null` when unknown. */
262
262
  language: string | null;
263
+ /**
264
+ * The container stream number (`ffmpeg -map 0:<n>`) of an embedded track,
265
+ * when the player can vouch for it; `null` for external tracks and when
266
+ * the player cannot tell, as for a subtitle track. The SDK always sets
267
+ * it; it is optional only so that an `AudioTrack` written out in full
268
+ * before 1.10.0 (a test's fixture) still type-checks.
269
+ *
270
+ * @since 1.10.0
271
+ */
272
+ streamIndex?: number | null;
263
273
  }
264
274
  /**
265
275
  * An immutable view of the player. Keep one to make a later call conditional on it.
@@ -853,6 +863,45 @@ export interface PlaybackStats {
853
863
  /** Seconds of media buffered ahead by the demuxer. */
854
864
  cacheSeconds: number | null;
855
865
  }
866
+ /**
867
+ * An open stream of the player's state ({@link Player.stream}).
868
+ *
869
+ * @remarks
870
+ * While it is open the player keeps the plugin running. It ends with
871
+ * {@link StateStream.stop}, when the scope that opened it is disposed, and
872
+ * when the plugin stops; it is not kept through standby.
873
+ *
874
+ * @example Follow the state while a panel is shown
875
+ * The stream lives only as long as what shows the state: it is opened
876
+ * with the scope of the panel's content and ends when that scope is
877
+ * disposed, so the plugin may go to standby afterwards.
878
+ * ```ts
879
+ * declare function draw(state: PlayerState): void;
880
+ * // ---cut---
881
+ * const shown = plugin.scope();
882
+ * shown.player.stream(draw); // stops when `shown` is disposed
883
+ * // … later, when the panel goes away:
884
+ * await shown.dispose();
885
+ * ```
886
+ *
887
+ * @since 1.10.0
888
+ * @category Playback
889
+ */
890
+ export interface StateStream {
891
+ /**
892
+ * Stop the stream: its listener is not called again. A second call does
893
+ * nothing. When no other stream is open, nothing of the streams keeps the
894
+ * plugin running any more.
895
+ *
896
+ * @throws Never.
897
+ *
898
+ * @example See Player.stream
899
+ * {@link Player.stream}
900
+ *
901
+ * @since 1.10.0
902
+ */
903
+ stop(): void;
904
+ }
856
905
  /**
857
906
  * `plugin.player`: playback state and controls.
858
907
  *
@@ -925,6 +974,75 @@ export interface Player {
925
974
  * @since 1.0.0
926
975
  */
927
976
  onChange(listener: (state: PlayerState) => void): Disposable;
977
+ /**
978
+ * Follow the player's state while something needs it continuously: a
979
+ * window or panel that shows the position, a meter.
980
+ *
981
+ * @remarks
982
+ * `listener` runs with the current state as soon as the SDK has it (one
983
+ * read of the player's state right after the call), then with every
984
+ * change the player sends: about ten times a second while playing, at
985
+ * once when the phase, pause, speed or volume changes. Before each call
986
+ * the SDK updates {@link Player.state}; after each call an update pass
987
+ * runs, so {@link View.bind} functions and command states that read
988
+ * `plugin.player.state` follow the stream. It needs no
989
+ * {@link Player.subscribe}: the stream itself makes the state follow the
990
+ * player while it is open.
991
+ *
992
+ * **While a stream is open the player keeps the plugin running**: it is
993
+ * a listener (its log names it `a stream opened at main.ts:12`). Open it
994
+ * when the thing that shows the state appears and stop it when that goes
995
+ * away; nothing of a stopped stream keeps the plugin running. A plugin
996
+ * that only needs to know that something happened (a file opened, ready,
997
+ * ended) uses {@link Player.on} with a source event instead, which lets
998
+ * it go to standby.
999
+ *
1000
+ * Several streams may be open; each listener is called. A stream ends
1001
+ * with {@link StateStream.stop}, when its scope is disposed, and when the
1002
+ * plugin stops; it is not kept through standby. After the last one ends
1003
+ * `plugin.player.state` keeps the last snapshot, and without
1004
+ * {@link Player.subscribe} it no longer follows changes made elsewhere.
1005
+ * {@link Player.subscribe} and {@link Player.onChange} keep working as
1006
+ * before.
1007
+ *
1008
+ * The player judges what keeps a plugin running when the plugin becomes
1009
+ * ready. A stream that is open then counts as it does today for a
1010
+ * listener held at the ready: the plugin keeps running for that run, also
1011
+ * after the stream stops. Open the stream after `activate`, when what
1012
+ * shows the state appears.
1013
+ *
1014
+ * Errors the listener throws or rejects with go to
1015
+ * {@link Scope.onError}, as for every handler.
1016
+ *
1017
+ * @param listener - Receives the state now and at every change.
1018
+ * @returns The open stream; it is stopped with the scope.
1019
+ *
1020
+ * @throws `invalid` (throws) `listener` is not a function.
1021
+ *
1022
+ * @example Show the time left while a window is open
1023
+ * A tool window shows how much of the file is left, updated as it plays.
1024
+ * The stream starts when the window is shown and stops when the user
1025
+ * closes it, so the plugin can go to standby afterwards.
1026
+ * ```ts
1027
+ * const view = plugin.windows.create("ui/left.slint");
1028
+ * let feed: StateStream | undefined;
1029
+ * view.onClose(() => feed?.stop()); // closed: nothing keeps the plugin running
1030
+ * plugin.commands.add({
1031
+ * id: "left",
1032
+ * path: ["Tools", "Time Left"],
1033
+ * run: async () => {
1034
+ * await view.show();
1035
+ * feed?.stop(); // one stream per window, also when shown again
1036
+ * feed = plugin.player.stream((s) =>
1037
+ * view.set({ text: `${Math.round((s.duration ?? 0) - (s.position ?? 0))} s` }),
1038
+ * );
1039
+ * },
1040
+ * });
1041
+ * ```
1042
+ *
1043
+ * @since 1.10.0
1044
+ */
1045
+ stream(listener: (state: PlayerState) => void): StateStream;
928
1046
  /**
929
1047
  * Listen to one event of the player, with its data as the player sent
930
1048
  * it: no state copy, no comparison.
@@ -987,6 +1105,45 @@ export interface Player {
987
1105
  * @since 1.7.0
988
1106
  */
989
1107
  on<E extends HostEvent>(event: E, listener: (data: EventPayload<E>) => void): Disposable;
1108
+ /**
1109
+ * Listen to one of the player's discrete source events: a source starts
1110
+ * to open, is ready, or ended.
1111
+ *
1112
+ * @remarks
1113
+ * `event` is a {@link SourceEvent} name; `listener` gets that event's
1114
+ * data, typed {@link SourceEventPayloads}, with `state` as a
1115
+ * {@link PlayerState}. The events arrive from the first listener,
1116
+ * without {@link Player.subscribe}, and a listener of one does not keep
1117
+ * the plugin running: the player starts a plugin in standby to deliver
1118
+ * them, and an event that arrives while the plugin starts waits for its
1119
+ * activation. `source.ended` with `reason: "quit"` reaches only a
1120
+ * plugin that is running when the player quits. As for every
1121
+ * {@link Player.on} listener, no full update pass follows it.
1122
+ *
1123
+ * @param event - The event's name.
1124
+ * @param listener - Receives each event's data.
1125
+ * @returns A handle that stops listening; it is disposed with the scope.
1126
+ *
1127
+ * @throws `invalid` (throws) `event` is not an event name, or
1128
+ * `listener` is not a function.
1129
+ *
1130
+ * @example Remember where each file was left
1131
+ * A plugin stores the position at which each file stopped playing. The
1132
+ * ended source's last state comes with the event, also when another
1133
+ * file replaced it.
1134
+ * ```ts
1135
+ * const saved = plugin.storage.json("positions", {
1136
+ * defaults: {} as Record<string, number>,
1137
+ * });
1138
+ * plugin.player.on("source.ended", ({ state, reason }) => {
1139
+ * if (reason === "natural" || state.source === null) return;
1140
+ * saved.save({ ...saved.value, [state.source]: state.position ?? 0 });
1141
+ * });
1142
+ * ```
1143
+ *
1144
+ * @since 1.10.0
1145
+ */
1146
+ on<E extends SourceEvent>(event: E, listener: (data: SourceEventPayloads[E]) => void): Disposable;
990
1147
  /**
991
1148
  * Read the state from the player now.
992
1149
  *
@@ -1781,6 +1938,91 @@ export interface EventPayloads {
1781
1938
  * @category Playback
1782
1939
  */
1783
1940
  export type EventPayload<E extends HostEvent> = EventPayloads[E];
1941
+ /**
1942
+ * The player's discrete source events: one event per thing that happened
1943
+ * to a source, which {@link Player.on} listens to by name.
1944
+ *
1945
+ * @remarks
1946
+ * `source.opening`: a new source starts to open. `source.ready`: it is
1947
+ * loaded. `source.ended`: it left playback. For one source the order is
1948
+ * `opening`, `ready`, `ended` (no `ready` when it could not be
1949
+ * opened); when a source is replaced, the old one's `ended` comes before
1950
+ * the new one's `opening`. Unlike `source.state` they arrive without
1951
+ * {@link Player.subscribe}, and a listener of one does not keep the plugin
1952
+ * running: the player starts a plugin in standby to deliver them.
1953
+ * {@link SourceEventPayloads} says what each one carries.
1954
+ *
1955
+ * @example Scan each file once it is loaded
1956
+ * A plugin looks at every file the player loads. The listener gets the
1957
+ * state at that moment, so the plugin needs no stream of its own and the
1958
+ * player may stop it between files.
1959
+ * ```ts
1960
+ * declare function scan(path: string): Promise<void>;
1961
+ * // ---cut---
1962
+ * plugin.player.on("source.ready", async ({ state }) => {
1963
+ * if (state.source !== null) await scan(state.source);
1964
+ * });
1965
+ * ```
1966
+ *
1967
+ * @since 1.10.0
1968
+ * @category Playback
1969
+ */
1970
+ export type SourceEvent = "source.opening" | "source.ready" | "source.ended";
1971
+ /**
1972
+ * The data each {@link SourceEvent} carries, as the listener of
1973
+ * {@link Player.on} gets it.
1974
+ *
1975
+ * @remarks
1976
+ * `state` is the SDK's {@link PlayerState}, not the player's wire shape,
1977
+ * so it can be passed as `ifCurrent` and to {@link Player.isCurrent}.
1978
+ * The state of an opening or a ready is also taken into
1979
+ * `plugin.player.state` when it is newer than what the plugin has.
1980
+ *
1981
+ * @since 1.10.0
1982
+ * @category Playback
1983
+ */
1984
+ export interface SourceEventPayloads {
1985
+ /**
1986
+ * A new source starts to open (also the same file again, and
1987
+ * {@link Player.restart}). `state` is the player's state at that moment,
1988
+ * as a {@link PlayerState} (pass it as `ifCurrent` or to
1989
+ * {@link Player.isCurrent}); `previous` names the source that was open
1990
+ * before it, `null` when nothing was.
1991
+ */
1992
+ "source.opening": {
1993
+ /** The player's state as the source starts to open. */
1994
+ state: PlayerState;
1995
+ /** The source open before this one (`null`: nothing was open). */
1996
+ previous: {
1997
+ /** The previous source's id (`PlayerState.sourceId`). */
1998
+ sourceId: number;
1999
+ } | null;
2000
+ };
2001
+ /**
2002
+ * The source is loaded: its duration, tracks and seekability are known.
2003
+ * Once per opening, after its `source.opening`.
2004
+ */
2005
+ "source.ready": {
2006
+ /** The player's state as the source became ready. */
2007
+ state: PlayerState;
2008
+ };
2009
+ /**
2010
+ * A source left playback: it played to its end (`natural`), was closed
2011
+ * (`stopped`), another source took its place (`replaced`), it could not
2012
+ * be played (`error`, with the failure text in `error`), or the player
2013
+ * quits (`quit`, sent only to a plugin that is running then). `state` is
2014
+ * **that** source's last state, with its last position, not the
2015
+ * player's current one.
2016
+ */
2017
+ "source.ended": {
2018
+ /** The ended source's last state, as a {@link PlayerState}. */
2019
+ state: PlayerState;
2020
+ /** Why it left playback. */
2021
+ reason: "natural" | "stopped" | "replaced" | "error" | "quit";
2022
+ /** The failure text, when `reason` is `error` and the player has one. */
2023
+ error?: string;
2024
+ };
2025
+ }
1784
2026
  /**
1785
2027
  * A value of mpv, as JSON: what a property holds, a command returns or an
1786
2028
  * argument of `set` is (mpv's "node": nothing, a flag, a number, a string,
@@ -1794,8 +2036,9 @@ export type MpvValue = null | boolean | number | string | MpvValue[] | {
1794
2036
  };
1795
2037
  /**
1796
2038
  * `plugin.mpv`: mpv's own commands and properties, sent to the player's
1797
- * mpv as they are. Needs the capability `mpv`, which the store shows the
1798
- * user before installing.
2039
+ * mpv as they are. The capability scan puts `command` and `set` in its
2040
+ * `player` category, which the store shows the user before installing
2041
+ * ([Capability scan](../../../docs/reference/scan.md)).
1799
2042
  *
1800
2043
  * @example Turn mpv's debanding on and show whether it took effect
1801
2044
  * A plugin offers mpv's `deband` option, which the plugin API has no member
@@ -1838,9 +2081,11 @@ export type MpvValue = null | boolean | number | string | MpvValue[] | {
1838
2081
  * the upscaler, motion smoothing) stay in the chain beside it.
1839
2082
  *
1840
2083
  * A property a plugin set keeps its value after the plugin stops, until
1841
- * the player quits (what the capability `mpv` discloses); a plugin that
1842
- * wants it back sets it back. Observers end with the plugin. A filter
1843
- * or an observer keeps the plugin running.
2084
+ * the player quits (what the scan's `player` category discloses); a
2085
+ * plugin that wants it back sets it back. Observers end with the plugin.
2086
+ * A filter or an observer keeps the plugin running from the moment it is
2087
+ * added for the rest of that run, also after it is removed: the player
2088
+ * judges again at the plugin's next start.
1844
2089
  *
1845
2090
  * @see [mpv](../../../docs/reference/mpv.md)
1846
2091
  * @since 1.8.0
@@ -1929,12 +2174,15 @@ export interface Mpv {
1929
2174
  set(name: string, value: MpvValue): Promise<void>;
1930
2175
  /**
1931
2176
  * Hear an mpv property's value now and after each change. The player
1932
- * sends the newest value at most every 100 ms. While a listener exists
1933
- * the player keeps the plugin running.
2177
+ * sends the newest value at most every 100 ms. From the first listener
2178
+ * on the player keeps the plugin running for the rest of the run, also
2179
+ * after the handle is disposed. As after every handler, an update pass
2180
+ * follows each call of the listener.
1934
2181
  *
1935
2182
  * @param name - The property, as mpv names it.
1936
2183
  * @param listener - Receives the value: first the current one, then
1937
- * each change.
2184
+ * each change. A listener added while the plugin already observes the
2185
+ * property gets its current value too, just after `observe` returns.
1938
2186
  * @returns A handle that stops listening; it is disposed with the scope.
1939
2187
  *
1940
2188
  * @example Follow the frame rate after filters
@@ -1,9 +1,20 @@
1
1
  /**
2
- * `plugin.when`: conditions on the player's own state that the player evaluates
3
- * for a command's `enabled` and `checked`. The wire form is `commands.register`
4
- * / `commands.update` `when`.
2
+ * `plugin.when`: conditions on the player's own state that the player
3
+ * evaluates: a command's `enabled` and `checked`, and since 1.10.0 a
4
+ * trigger's condition (`plugin.trigger().firesOn`). The wire form is
5
+ * `commands.register` / `commands.update` `when` and `triggers.set` `when`.
5
6
  */
6
7
  import type { Condition, LanguagePreference, PlayerState, ThemeId, When } from "./types.ts";
8
+ /** A numeric atom's interval: `min ≤ value < max`, at least one bound. */
9
+ export type IntervalJson = {
10
+ min?: number;
11
+ max?: number;
12
+ };
13
+ /** A position list in the wire form: `[start, end?]` pairs, and a lead. */
14
+ export type PositionJson = {
15
+ ranges: ([number] | [number, number | null])[];
16
+ lead?: number;
17
+ };
7
18
  /** A condition in the wire form: an object of exactly one key. */
8
19
  export type ConditionJson = {
9
20
  phase: PlayerState["phase"][];
@@ -19,6 +30,30 @@ export type ConditionJson = {
19
30
  theme: ThemeId;
20
31
  } | {
21
32
  language: LanguagePreference;
33
+ } | {
34
+ duration: IntervalJson;
35
+ } | {
36
+ volume: IntervalJson;
37
+ } | {
38
+ rate: IntervalJson;
39
+ } | {
40
+ audioTrack: number;
41
+ } | {
42
+ subtitleTrack: string;
43
+ } | {
44
+ audioDevice: string;
45
+ } | {
46
+ muted: boolean;
47
+ } | {
48
+ maximized: boolean;
49
+ } | {
50
+ pip: boolean;
51
+ } | {
52
+ looping: boolean;
53
+ } | {
54
+ time: IntervalJson;
55
+ } | {
56
+ position: PositionJson;
22
57
  } | {
23
58
  all: ConditionJson[];
24
59
  } | {
@@ -26,7 +61,10 @@ export type ConditionJson = {
26
61
  } | {
27
62
  not: ConditionJson;
28
63
  };
29
- /** What conditions read, in the SDK's names. */
64
+ /**
65
+ * What conditions read, in the SDK's names. The fields added in 1.10.0 are
66
+ * optional: missing, a fact reads as unknown (false).
67
+ */
30
68
  export interface ConditionFacts {
31
69
  phase: PlayerState["phase"];
32
70
  paused: boolean;
@@ -35,12 +73,48 @@ export interface ConditionFacts {
35
73
  fullscreen: boolean;
36
74
  theme: string;
37
75
  language: string;
76
+ /** Seconds; `null` while unknown. */
77
+ duration?: number | null;
78
+ volume?: number;
79
+ rate?: number;
80
+ muted?: boolean;
81
+ audioTrack?: number | null;
82
+ subtitleTrack?: string | null;
83
+ audioDevice?: string | null;
84
+ looping?: boolean;
85
+ maximized?: boolean;
86
+ pip?: boolean;
87
+ /** The current source's id. */
88
+ sourceId?: number;
89
+ /** The position; `null` without one. */
90
+ position?: number | null;
91
+ /** The wall clock, Unix milliseconds. */
92
+ time?: number;
93
+ /** The source a position list was bound to (T12). */
94
+ bound?: number | null;
38
95
  }
39
96
  /** Whether `value` is a condition made by `plugin.when`. */
40
97
  export declare const isCondition: (value: unknown) => value is Condition;
41
98
  /** A condition's wire form; throws for anything else. */
42
99
  export declare function conditionJson(condition: Condition): ConditionJson;
100
+ /** Whether a wire condition reads the position or the clock (a trigger's only). */
101
+ export declare function readsPositionOrTime(json: ConditionJson): boolean;
102
+ /** How many position lists a wire condition has (at most one is allowed). */
103
+ export declare function positionLists(json: ConditionJson): number;
43
104
  /** The one `plugin.when` (conditions hold no plugin state). */
44
105
  export declare const WHEN: When;
106
+ /** One evaluation: whether it holds and, when its position list does, the range. */
107
+ export interface Truth {
108
+ holds: boolean;
109
+ /** The index of the range the position is in, when its position list holds. */
110
+ range: number | null;
111
+ }
112
+ /**
113
+ * The range of `list` the position `at` is in (`start − lead ≤ at < end`):
114
+ * the earliest start when several are, as the player does.
115
+ */
116
+ export declare function matchedRange(list: PositionJson, at: number): number | null;
117
+ /** Evaluate a wire condition as the player does, with its range (the test host). */
118
+ export declare function evaluateTruth(json: ConditionJson, facts: ConditionFacts): Truth;
45
119
  /** Evaluate a wire condition as the player does (for the test host). */
46
120
  export declare function evaluate(json: ConditionJson, facts: ConditionFacts): boolean;
@@ -1,5 +1,7 @@
1
1
  import { LOCALES } from "../i18n.js";
2
2
  import { THEMES } from "./appearance.js";
3
+ import { failure } from "./errors.js";
4
+ import { TIMELINE_LIMITS } from "./timeline.js";
3
5
  const PHASES = [
4
6
  "empty",
5
7
  "loading",
@@ -51,7 +53,83 @@ export function conditionJson(condition) {
51
53
  throw new TypeError("a condition comes from plugin.when");
52
54
  return ConditionImpl.json(condition);
53
55
  }
56
+ /** Whether a wire condition reads the position or the clock (a trigger's only). */
57
+ export function readsPositionOrTime(json) {
58
+ if ("position" in json || "time" in json)
59
+ return true;
60
+ if ("all" in json)
61
+ return json.all.some(readsPositionOrTime);
62
+ if ("any" in json)
63
+ return json.any.some(readsPositionOrTime);
64
+ if ("not" in json)
65
+ return readsPositionOrTime(json.not);
66
+ return false;
67
+ }
68
+ /** How many position lists a wire condition has (at most one is allowed). */
69
+ export function positionLists(json) {
70
+ if ("position" in json)
71
+ return 1;
72
+ if ("all" in json)
73
+ return json.all.reduce((n, c) => n + positionLists(c), 0);
74
+ if ("any" in json)
75
+ return json.any.reduce((n, c) => n + positionLists(c), 0);
76
+ if ("not" in json)
77
+ return positionLists(json.not);
78
+ return 0;
79
+ }
54
80
  const make = (json) => new ConditionImpl(json);
81
+ const finite = (value) => typeof value === "number" && Number.isFinite(value);
82
+ /** A numeric atom's `{ min?, max? }`, checked. */
83
+ function interval(name, bounds) {
84
+ const shape = `${name} takes { min?, max? }: finite numbers, at least one, min < max`;
85
+ if (!bounds || typeof bounds !== "object")
86
+ throw new TypeError(shape);
87
+ const { min, max, ...rest } = bounds;
88
+ if (Object.keys(rest).length)
89
+ throw new TypeError(shape);
90
+ if (min === undefined && max === undefined)
91
+ throw new TypeError(shape);
92
+ if ((min !== undefined && !finite(min)) ||
93
+ (max !== undefined && !finite(max)))
94
+ throw new TypeError(shape);
95
+ if (finite(min) && finite(max) && min >= max)
96
+ throw new TypeError(shape);
97
+ const json = {};
98
+ if (min !== undefined)
99
+ json.min = min;
100
+ if (max !== undefined)
101
+ json.max = max;
102
+ return json;
103
+ }
104
+ function identity(name, id) {
105
+ if (typeof id !== "string" || id.length === 0 || id.length > 256)
106
+ throw new TypeError(`${name} takes an id (a string of 1–256 characters)`);
107
+ return id;
108
+ }
109
+ /** `when.position.in`'s ranges and lead in the wire form. */
110
+ function positionList(ranges, options = {}) {
111
+ const list = [...(ranges ?? [])];
112
+ if (list.length > TIMELINE_LIMITS.ranges)
113
+ throw failure("limit", `a position list holds at most ${TIMELINE_LIMITS.ranges} ranges`);
114
+ const pairs = list.map((range) => {
115
+ const { start, end } = (range ?? {});
116
+ if (!finite(start) || start < 0)
117
+ throw new TypeError("a range starts at a finite number of seconds, 0 or more");
118
+ if (end === undefined || end === null)
119
+ return [start];
120
+ if (!finite(end) || end <= start)
121
+ throw new TypeError("a range ends after its start");
122
+ return [start, end];
123
+ });
124
+ const json = { ranges: pairs };
125
+ const lead = options?.lead;
126
+ if (lead !== undefined) {
127
+ if (!finite(lead) || lead < 0)
128
+ throw new TypeError("lead is a number of seconds, 0 or more");
129
+ json.lead = lead;
130
+ }
131
+ return json;
132
+ }
55
133
  /** The one `plugin.when` (conditions hold no plugin state). */
56
134
  export const WHEN = Object.freeze({
57
135
  open: make({ not: { phase: ["empty"] } }),
@@ -60,6 +138,10 @@ export const WHEN = Object.freeze({
60
138
  paused: make({ all: [{ phase: ["ready"] }, { paused: true }] }),
61
139
  stream: make({ kind: "stream" }),
62
140
  fullscreen: make({ fullscreen: true }),
141
+ muted: make({ muted: true }),
142
+ maximized: make({ maximized: true }),
143
+ pip: make({ pip: true }),
144
+ looping: make({ looping: true }),
63
145
  phase(...phases) {
64
146
  if (!phases.length || phases.some((phase) => !PHASES.includes(phase)))
65
147
  throw new TypeError(`phase is one or more of ${PHASES.join(", ")}`);
@@ -81,26 +163,112 @@ export const WHEN = Object.freeze({
81
163
  throw new TypeError(`language is "system" or one of ${LOCALES.map((l) => `"${l}"`).join(", ")}`);
82
164
  return make({ language });
83
165
  },
166
+ duration: (bounds) => make({ duration: interval("duration", bounds) }),
167
+ volume: (bounds) => make({ volume: interval("volume", bounds) }),
168
+ rate: (bounds) => make({ rate: interval("rate", bounds) }),
169
+ time: (bounds) => make({ time: interval("time", bounds) }),
170
+ audioTrack(id) {
171
+ if (!Number.isInteger(id))
172
+ throw new TypeError("audioTrack takes a track id (an integer)");
173
+ return make({ audioTrack: id });
174
+ },
175
+ subtitleTrack: (id) => make({ subtitleTrack: identity("subtitleTrack", id) }),
176
+ audioDevice: (id) => make({ audioDevice: identity("audioDevice", id) }),
177
+ position: Object.freeze({
178
+ in: (ranges, options) => make({ position: positionList(ranges, options) }),
179
+ }),
84
180
  });
85
- /** Evaluate a wire condition as the player does (for the test host). */
86
- export function evaluate(json, facts) {
181
+ /**
182
+ * The range of `list` the position `at` is in (`start − lead ≤ at < end`):
183
+ * the earliest start when several are, as the player does.
184
+ */
185
+ export function matchedRange(list, at) {
186
+ const lead = list.lead ?? 0;
187
+ let found = null;
188
+ let earliest = Infinity;
189
+ list.ranges.forEach(([start, end], index) => {
190
+ const from = start - lead;
191
+ if (from <= at && at < (end ?? Infinity) && from < earliest) {
192
+ earliest = from;
193
+ found = index;
194
+ }
195
+ });
196
+ return found;
197
+ }
198
+ /** Evaluate a wire condition as the player does, with its range (the test host). */
199
+ export function evaluateTruth(json, facts) {
200
+ const plain = (holds) => ({ holds, range: null });
201
+ const inside = (bounds, value) => typeof value === "number" &&
202
+ (bounds.min === undefined || bounds.min <= value) &&
203
+ (bounds.max === undefined || value < bounds.max);
87
204
  if ("phase" in json)
88
- return json.phase.includes(facts.phase);
205
+ return plain(json.phase.includes(facts.phase));
89
206
  if ("paused" in json)
90
- return facts.paused === json.paused;
207
+ return plain(facts.paused === json.paused);
91
208
  if ("kind" in json)
92
- return facts.kind === json.kind;
209
+ return plain(facts.kind === json.kind);
93
210
  if ("can" in json)
94
- return facts.can[json.can] === true;
211
+ return plain(facts.can[json.can] === true);
95
212
  if ("fullscreen" in json)
96
- return facts.fullscreen === json.fullscreen;
213
+ return plain(facts.fullscreen === json.fullscreen);
97
214
  if ("theme" in json)
98
- return facts.theme === json.theme;
215
+ return plain(facts.theme === json.theme);
99
216
  if ("language" in json)
100
- return facts.language === json.language;
101
- if ("all" in json)
102
- return json.all.every((each) => evaluate(each, facts));
103
- if ("any" in json)
104
- return json.any.some((each) => evaluate(each, facts));
105
- return !evaluate(json.not, facts);
217
+ return plain(facts.language === json.language);
218
+ if ("duration" in json)
219
+ return plain(inside(json.duration, facts.duration));
220
+ if ("volume" in json)
221
+ return plain(inside(json.volume, facts.volume ?? 100));
222
+ if ("rate" in json)
223
+ return plain(inside(json.rate, facts.rate ?? 1));
224
+ if ("time" in json)
225
+ return plain(inside(json.time, facts.time));
226
+ if ("audioTrack" in json)
227
+ return plain((facts.audioTrack ?? null) === json.audioTrack);
228
+ if ("subtitleTrack" in json)
229
+ return plain((facts.subtitleTrack ?? null) === json.subtitleTrack);
230
+ if ("audioDevice" in json)
231
+ return plain((facts.audioDevice ?? null) === json.audioDevice);
232
+ if ("muted" in json)
233
+ return plain((facts.muted ?? false) === json.muted);
234
+ if ("maximized" in json)
235
+ return plain((facts.maximized ?? false) === json.maximized);
236
+ if ("pip" in json)
237
+ return plain((facts.pip ?? false) === json.pip);
238
+ if ("looping" in json)
239
+ return plain((facts.looping ?? false) === json.looping);
240
+ if ("position" in json) {
241
+ const position = facts.position;
242
+ const range = typeof position === "number" &&
243
+ facts.bound !== null &&
244
+ facts.bound !== undefined &&
245
+ facts.bound === facts.sourceId
246
+ ? matchedRange(json.position, position)
247
+ : null;
248
+ return { holds: range !== null, range };
249
+ }
250
+ if ("all" in json) {
251
+ let range = null;
252
+ for (const each of json.all) {
253
+ const truth = evaluateTruth(each, facts);
254
+ if (!truth.holds)
255
+ return plain(false);
256
+ range ??= truth.range;
257
+ }
258
+ return { holds: true, range };
259
+ }
260
+ if ("any" in json) {
261
+ const truth = plain(false);
262
+ for (const each of json.any) {
263
+ const one = evaluateTruth(each, facts);
264
+ truth.holds ||= one.holds;
265
+ truth.range ??= one.range;
266
+ }
267
+ return truth;
268
+ }
269
+ return plain(!evaluateTruth(json.not, facts).holds);
270
+ }
271
+ /** Evaluate a wire condition as the player does (for the test host). */
272
+ export function evaluate(json, facts) {
273
+ return evaluateTruth(json, facts).holds;
106
274
  }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * The Slint version the player links (its `Cargo.toml` pins `=1.18.1`): the
3
+ * compiler that `vestra-plugin check` asks of an installed `slint-ui` package,
4
+ * and `TestHost`'s `app.components.slint`. Changes with a player that
5
+ * upgrades Slint.
6
+ */
7
+ export declare const PLAYER_SLINT = "1.18.1";