@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.
- package/CHANGELOG.md +44 -7
- package/bin/vestra-plugin.mjs +89 -8
- package/capability-scan.json +20 -0
- package/dist/api-requests.d.ts +4 -3
- package/dist/api-requests.js +11 -3
- package/dist/commands.js +1 -0
- package/dist/condition-table.d.ts +1889 -0
- package/dist/condition-table.js +562 -0
- package/dist/docs-tree.json.gz +0 -0
- package/dist/i18n-tools.d.ts +7 -0
- package/dist/i18n-tools.js +66 -3
- package/dist/player-runtime.d.ts +3 -0
- package/dist/player-runtime.js +3 -0
- package/dist/plugin/connection.d.ts +13 -2
- package/dist/plugin/connection.js +67 -4
- package/dist/plugin/index.d.ts +7 -0
- package/dist/plugin/index.js +13 -0
- package/dist/plugin/mpv.js +39 -5
- package/dist/plugin/player/events.d.ts +14 -5
- package/dist/plugin/player/events.js +19 -3
- package/dist/plugin/player.d.ts +16 -0
- package/dist/plugin/player.js +77 -1
- package/dist/plugin/scope.d.ts +4 -1
- package/dist/plugin/scope.js +8 -0
- package/dist/plugin/testing/commands.d.ts +5 -0
- package/dist/plugin/testing/commands.js +23 -24
- package/dist/plugin/testing/index.d.ts +134 -0
- package/dist/plugin/testing/index.js +180 -21
- package/dist/plugin/testing/media.d.ts +6 -0
- package/dist/plugin/testing/media.js +15 -2
- package/dist/plugin/testing/services.js +50 -1
- package/dist/plugin/testing/source-events.d.ts +28 -0
- package/dist/plugin/testing/source-events.js +66 -0
- package/dist/plugin/testing/transport.js +5 -0
- package/dist/plugin/testing/triggers.d.ts +82 -0
- package/dist/plugin/testing/triggers.js +448 -0
- package/dist/plugin/timeline.d.ts +3 -3
- package/dist/plugin/timeline.js +3 -3
- package/dist/plugin/triggers.d.ts +55 -0
- package/dist/plugin/triggers.js +233 -0
- package/dist/plugin/types/base.d.ts +117 -3
- package/dist/plugin/types/commands.d.ts +374 -1
- package/dist/plugin/types/packages.d.ts +10 -1
- package/dist/plugin/types/playback.d.ts +257 -9
- package/dist/plugin/types/services.d.ts +10 -4
- package/dist/plugin/when.d.ts +78 -4
- package/dist/plugin/when.js +182 -14
- package/dist/slint-version.d.ts +7 -0
- package/dist/slint-version.js +7 -0
- package/dist/wire.d.ts +18 -0
- package/dist/wire.js +12 -0
- package/llms.txt +4 -4
- package/package.json +2 -2
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const inPlayback = (s) => s.source !== null && (s.phase === "loading" || s.phase === "ready");
|
|
2
|
+
const samePlaythrough = (a, b) => a.stamp.generation === b.stamp.generation && a.cycle === b.cycle;
|
|
3
|
+
/** `source.ended`'s data for a playthrough that ended in `state`. */
|
|
4
|
+
function endedIn(state) {
|
|
5
|
+
const reason = state.terminalReason;
|
|
6
|
+
if (state.phase === "error")
|
|
7
|
+
return reason !== null && typeof reason === "object"
|
|
8
|
+
? { state, reason: "error", error: reason.failed }
|
|
9
|
+
: { state, reason: "error" };
|
|
10
|
+
return {
|
|
11
|
+
state,
|
|
12
|
+
reason: reason === "natural" || reason === "replaced" ? reason : "stopped",
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/** The events `next` adds to `previous`, in order. */
|
|
16
|
+
export function sourceEvents(previous, next, reload = false) {
|
|
17
|
+
const events = [];
|
|
18
|
+
const open = next.source !== null;
|
|
19
|
+
const same = samePlaythrough(previous, next);
|
|
20
|
+
const resumed = !reload &&
|
|
21
|
+
previous.source !== null &&
|
|
22
|
+
previous.stamp.generation === next.stamp.generation &&
|
|
23
|
+
previous.cycle !== next.cycle &&
|
|
24
|
+
previous.phase === "ended" &&
|
|
25
|
+
next.phase !== "loading";
|
|
26
|
+
const begins = open && !same && !resumed;
|
|
27
|
+
if (inPlayback(previous) && !(same && inPlayback(next))) {
|
|
28
|
+
if (same && open)
|
|
29
|
+
events.push(["source.ended", endedIn(next)]);
|
|
30
|
+
else if (!open && next.stamp.generation === previous.stamp.generation)
|
|
31
|
+
events.push(["source.ended", { state: previous, reason: "stopped" }]);
|
|
32
|
+
else
|
|
33
|
+
events.push(["source.ended", { state: previous, reason: "replaced" }]);
|
|
34
|
+
}
|
|
35
|
+
if (begins) {
|
|
36
|
+
events.push([
|
|
37
|
+
"source.opening",
|
|
38
|
+
{
|
|
39
|
+
state: next,
|
|
40
|
+
previous: previous.source !== null
|
|
41
|
+
? { sourceId: previous.stamp.generation }
|
|
42
|
+
: null,
|
|
43
|
+
},
|
|
44
|
+
]);
|
|
45
|
+
if (next.phase === "ready" || next.phase === "ended")
|
|
46
|
+
events.push(["source.ready", { state: next }]);
|
|
47
|
+
if (next.phase === "ended" || next.phase === "error")
|
|
48
|
+
events.push(["source.ended", endedIn(next)]);
|
|
49
|
+
}
|
|
50
|
+
else if (same && previous.phase === "loading" && open) {
|
|
51
|
+
if (next.phase === "ready" || next.phase === "ended")
|
|
52
|
+
events.push(["source.ready", { state: next }]);
|
|
53
|
+
// Loading straight to its end: ready, then ended.
|
|
54
|
+
const at = events.findIndex(([name]) => name === "source.ended");
|
|
55
|
+
if (next.phase === "ended" && at >= 0)
|
|
56
|
+
events.push(...events.splice(at, 1));
|
|
57
|
+
}
|
|
58
|
+
return events;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* `source.ended` with `reason: "quit"` for a source in playback (the
|
|
62
|
+
* player quits; only a running plugin gets it).
|
|
63
|
+
*/
|
|
64
|
+
export function quitEvent(state) {
|
|
65
|
+
return inPlayback(state) ? { state, reason: "quit" } : null;
|
|
66
|
+
}
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
import { rmSync } from "node:fs";
|
|
10
10
|
import { HostError } from "../../wire.js";
|
|
11
11
|
import { WAKE_EVENTS } from "../connection.js";
|
|
12
|
+
import { hearEvent } from "./triggers.js";
|
|
12
13
|
export const tick = () => new Promise((resolve) => setImmediate(resolve));
|
|
13
14
|
/**
|
|
14
15
|
* Refusals for a position or resolver another plugin holds: not activation
|
|
@@ -49,6 +50,10 @@ export function on(_host, inner, event, listener) {
|
|
|
49
50
|
}
|
|
50
51
|
/** Implements `TestHost.emit`. */
|
|
51
52
|
export function emit(host, inner, event, data) {
|
|
53
|
+
// Trigger conditions first (the player evaluates them before it sends
|
|
54
|
+
// the state); a parked plugin hears nothing, a wake event wakes it.
|
|
55
|
+
if (hearEvent(host, inner, event, data))
|
|
56
|
+
return;
|
|
52
57
|
if ([
|
|
53
58
|
"source.state",
|
|
54
59
|
"playback.backend",
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Json } from "../../wire.ts";
|
|
2
|
+
import { matchedRange } from "../when.ts";
|
|
3
|
+
import type { ConditionFacts, ConditionJson } from "../when.ts";
|
|
4
|
+
import type { TestHost } from "./index.ts";
|
|
5
|
+
import type { Handler, Internals } from "./internals.ts";
|
|
6
|
+
/**
|
|
7
|
+
* One kept trigger condition, as `TestHost.triggers` lists it.
|
|
8
|
+
*
|
|
9
|
+
* @since 1.10.0
|
|
10
|
+
* @category Testing
|
|
11
|
+
*/
|
|
12
|
+
export interface KeptTrigger {
|
|
13
|
+
/** The trigger: its name, or its place among the unnamed ones. */
|
|
14
|
+
readonly trigger: number | string;
|
|
15
|
+
/** The condition in the wire form. */
|
|
16
|
+
readonly when: Json;
|
|
17
|
+
/** Its sequence number. */
|
|
18
|
+
readonly seq: number;
|
|
19
|
+
/** It holds now ("entered"). */
|
|
20
|
+
readonly entered: boolean;
|
|
21
|
+
}
|
|
22
|
+
interface Kept {
|
|
23
|
+
trigger: number | string;
|
|
24
|
+
when: ConditionJson;
|
|
25
|
+
seq: number;
|
|
26
|
+
/** The source its position list belongs to. */
|
|
27
|
+
bound: number | null;
|
|
28
|
+
/** Entered, with the range it is in. */
|
|
29
|
+
entered: {
|
|
30
|
+
range: number | null;
|
|
31
|
+
} | null;
|
|
32
|
+
/** The run that gave it. */
|
|
33
|
+
run: number;
|
|
34
|
+
}
|
|
35
|
+
/** What the player keeps for the plugin's triggers. */
|
|
36
|
+
declare class TriggerState {
|
|
37
|
+
readonly kept: Map<string, Kept>;
|
|
38
|
+
seq: number;
|
|
39
|
+
/** The run (process generation): a park, a wake and an upgrade start one. */
|
|
40
|
+
run: number;
|
|
41
|
+
/** How many unnamed triggers this version declared. */
|
|
42
|
+
unnamed: number | null;
|
|
43
|
+
/** The virtual wall clock (Unix ms). */
|
|
44
|
+
now: number;
|
|
45
|
+
parked: boolean;
|
|
46
|
+
/** The wake events the plugin listened to when it was parked. */
|
|
47
|
+
wakes: Set<string>;
|
|
48
|
+
/** What waits for the next start: (merge key, event, data). */
|
|
49
|
+
backlog: [string, string, unknown][];
|
|
50
|
+
waking: Promise<void> | undefined;
|
|
51
|
+
/** The backlog is delivered now (at the woken plugin's ready). */
|
|
52
|
+
flushing: boolean;
|
|
53
|
+
/** Starts the plugin again (`startPlugin`); `entry`: other code (an upgrade). */
|
|
54
|
+
start: ((entry?: (plugin: never) => unknown) => Promise<void>) | undefined;
|
|
55
|
+
/** Stops the running plugin with a reason. */
|
|
56
|
+
stop: ((reason: string) => Promise<void>) | undefined;
|
|
57
|
+
/** Evaluating: no evaluation inside another. */
|
|
58
|
+
busy: boolean;
|
|
59
|
+
}
|
|
60
|
+
export declare function triggerState(host: TestHost): TriggerState;
|
|
61
|
+
/** The facts the player's conditions read, from the host. */
|
|
62
|
+
export declare function conditionFacts(host: TestHost, state?: TriggerState): ConditionFacts;
|
|
63
|
+
/** Evaluates `only` (or every kept condition) and sends what flipped (T1, T10). */
|
|
64
|
+
export declare function evaluateTriggers(host: TestHost, inner: Internals, only?: Kept): void;
|
|
65
|
+
/**
|
|
66
|
+
* `emit` of `event`: the trigger conditions are evaluated first for the
|
|
67
|
+
* player's own state changes; a wake event for a parked plugin that listened
|
|
68
|
+
* to it is held and wakes it (`true`: not delivered now).
|
|
69
|
+
*/
|
|
70
|
+
export declare function hearEvent(host: TestHost, inner: Internals, event: string, data: unknown): boolean;
|
|
71
|
+
/** `TestHost.triggers`. */
|
|
72
|
+
export declare function keptTriggers(host: TestHost): KeptTrigger[];
|
|
73
|
+
/** `TestHost.advance`: the clock and, while playing, the position move by `ms`. */
|
|
74
|
+
export declare function advance(host: TestHost, inner: Internals, ms: number): Promise<void>;
|
|
75
|
+
/** `TestHost.park`: the player puts the plugin in standby. */
|
|
76
|
+
export declare function park(host: TestHost, _inner: Internals): Promise<void>;
|
|
77
|
+
/** `TestHost.upgrade`: another version replaces the plugin (§4.2). */
|
|
78
|
+
export declare function upgrade(host: TestHost, _inner: Internals, entry?: (plugin: never) => unknown): Promise<void>;
|
|
79
|
+
/** The default handlers of this area's wire methods, by method name. */
|
|
80
|
+
export declare function triggersHandlers(host: TestHost, inner: Internals): Record<string, Handler>;
|
|
81
|
+
/** `matchedRange` for tests of the host's position lists. */
|
|
82
|
+
export { matchedRange };
|
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Triggers in the fake player (1.10.0, events-and-triggers.md §4.11): the
|
|
3
|
+
* conditions a plugin hands over, kept and evaluated by the player's rules
|
|
4
|
+
* (`evaluateTruth`, the agreement table), a virtual clock that moves the
|
|
5
|
+
* playback position and the wall clock (`advance`), standby and wake
|
|
6
|
+
* (`park`), and an upgrade (`upgrade`: unnamed conditions cleared, named
|
|
7
|
+
* kept).
|
|
8
|
+
*
|
|
9
|
+
* Part of `TestHost` (`./index.ts`): functions over the host and its private
|
|
10
|
+
* state. Internal to the SDK; not exported from `@vestra-player/sdk/testing`.
|
|
11
|
+
*/
|
|
12
|
+
import { HostError } from "../../wire.js";
|
|
13
|
+
import { WAKE_EVENTS } from "../connection.js";
|
|
14
|
+
import { TIMELINE_LIMITS } from "../timeline.js";
|
|
15
|
+
import { TRIGGER_LIMITS } from "../triggers.js";
|
|
16
|
+
import { evaluateTruth, matchedRange, positionLists } from "../when.js";
|
|
17
|
+
/** What the player keeps for the plugin's triggers. */
|
|
18
|
+
class TriggerState {
|
|
19
|
+
kept = new Map();
|
|
20
|
+
seq = 0;
|
|
21
|
+
/** The run (process generation): a park, a wake and an upgrade start one. */
|
|
22
|
+
run = 1;
|
|
23
|
+
/** How many unnamed triggers this version declared. */
|
|
24
|
+
unnamed = null;
|
|
25
|
+
/** The virtual wall clock (Unix ms). */
|
|
26
|
+
now = Date.now();
|
|
27
|
+
parked = false;
|
|
28
|
+
/** The wake events the plugin listened to when it was parked. */
|
|
29
|
+
wakes = new Set();
|
|
30
|
+
/** What waits for the next start: (merge key, event, data). */
|
|
31
|
+
backlog = [];
|
|
32
|
+
waking;
|
|
33
|
+
/** The backlog is delivered now (at the woken plugin's ready). */
|
|
34
|
+
flushing = false;
|
|
35
|
+
/** Starts the plugin again (`startPlugin`); `entry`: other code (an upgrade). */
|
|
36
|
+
start;
|
|
37
|
+
/** Stops the running plugin with a reason. */
|
|
38
|
+
stop;
|
|
39
|
+
/** Evaluating: no evaluation inside another. */
|
|
40
|
+
busy = false;
|
|
41
|
+
}
|
|
42
|
+
const states = new WeakMap();
|
|
43
|
+
export function triggerState(host) {
|
|
44
|
+
let state = states.get(host);
|
|
45
|
+
if (!state) {
|
|
46
|
+
state = new TriggerState();
|
|
47
|
+
states.set(host, state);
|
|
48
|
+
}
|
|
49
|
+
return state;
|
|
50
|
+
}
|
|
51
|
+
const key = (trigger) => JSON.stringify(trigger);
|
|
52
|
+
/** The facts the player's conditions read, from the host. */
|
|
53
|
+
export function conditionFacts(host, state) {
|
|
54
|
+
const source = host.source;
|
|
55
|
+
const capabilities = source.capabilities;
|
|
56
|
+
const target = host.subtitles.selection.target;
|
|
57
|
+
return {
|
|
58
|
+
phase: source.phase,
|
|
59
|
+
paused: source.paused,
|
|
60
|
+
kind: source.kind === "producer" ? "stream" : "media",
|
|
61
|
+
can: {
|
|
62
|
+
pause: capabilities.pause,
|
|
63
|
+
seek: capabilities.seek,
|
|
64
|
+
rate: capabilities.rate,
|
|
65
|
+
restart: capabilities.restart,
|
|
66
|
+
close: capabilities.close,
|
|
67
|
+
snapshot: capabilities.snapshotSource,
|
|
68
|
+
next: source.sequence?.next === true,
|
|
69
|
+
previous: source.sequence?.previous === true,
|
|
70
|
+
},
|
|
71
|
+
fullscreen: host.mainWindow.fullscreen,
|
|
72
|
+
theme: host.appearance,
|
|
73
|
+
language: host.language,
|
|
74
|
+
duration: typeof source.duration === "number" ? source.duration : null,
|
|
75
|
+
volume: source.output.volume,
|
|
76
|
+
rate: source.rate,
|
|
77
|
+
muted: source.output.muted,
|
|
78
|
+
audioTrack: source.audioTracks?.find((t) => t.selected)?.trackId ?? null,
|
|
79
|
+
subtitleTrack: !target
|
|
80
|
+
? null
|
|
81
|
+
: target.kind === "embedded"
|
|
82
|
+
? `embedded:${target.id}`
|
|
83
|
+
: `external:${target.owner}:${target.resourceId}:${target.contentRevision}`,
|
|
84
|
+
audioDevice: source.audioOutput?.active ?? null,
|
|
85
|
+
looping: !!source.loop,
|
|
86
|
+
maximized: host.mainWindow.maximized && !host.mainWindow.fullscreen,
|
|
87
|
+
pip: host.mainWindow.pictureInPicture.active,
|
|
88
|
+
sourceId: source.stamp.generation,
|
|
89
|
+
position: source.position,
|
|
90
|
+
time: (state ?? triggerState(host)).now,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/** The player's refusal of a condition it cannot keep, in its words. */
|
|
94
|
+
function check(json) {
|
|
95
|
+
if (!json || typeof json !== "object" || Object.keys(json).length !== 1)
|
|
96
|
+
throw new HostError("invalid", "a condition is an object with one key");
|
|
97
|
+
const condition = json;
|
|
98
|
+
if (positionLists(condition) > 1)
|
|
99
|
+
throw new HostError("invalid", "a condition has at most one position list");
|
|
100
|
+
const lists = (c) => "position" in c
|
|
101
|
+
? [c.position]
|
|
102
|
+
: "all" in c
|
|
103
|
+
? c.all.flatMap(lists)
|
|
104
|
+
: "any" in c
|
|
105
|
+
? c.any.flatMap(lists)
|
|
106
|
+
: "not" in c
|
|
107
|
+
? lists(c.not)
|
|
108
|
+
: [];
|
|
109
|
+
for (const list of lists(condition))
|
|
110
|
+
if (list.ranges.length > TIMELINE_LIMITS.ranges)
|
|
111
|
+
throw new HostError("limit", `a position list holds at most ${TIMELINE_LIMITS.ranges} ranges`);
|
|
112
|
+
return condition;
|
|
113
|
+
}
|
|
114
|
+
function parseTrigger(value) {
|
|
115
|
+
if (typeof value === "number" && Number.isInteger(value) && value >= 0) {
|
|
116
|
+
if (value >= TRIGGER_LIMITS.triggers)
|
|
117
|
+
throw new HostError("limit", `a plugin has at most ${TRIGGER_LIMITS.triggers} triggers`);
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
if (typeof value === "string" &&
|
|
121
|
+
value.length > 0 &&
|
|
122
|
+
[...value].length <= TRIGGER_LIMITS.nameChars)
|
|
123
|
+
return value;
|
|
124
|
+
throw new HostError("invalid", "trigger is a name or a place");
|
|
125
|
+
}
|
|
126
|
+
/** The position list of `when`, if it has one. */
|
|
127
|
+
function listOf(when) {
|
|
128
|
+
if ("position" in when)
|
|
129
|
+
return when.position;
|
|
130
|
+
if ("all" in when)
|
|
131
|
+
return when.all.map(listOf).find((l) => l) ?? null;
|
|
132
|
+
if ("any" in when)
|
|
133
|
+
return when.any.map(listOf).find((l) => l) ?? null;
|
|
134
|
+
if ("not" in when)
|
|
135
|
+
return listOf(when.not);
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
/** The nearest time bound of `when` after `now`. */
|
|
139
|
+
function nextTime(when, now) {
|
|
140
|
+
const nearest = (values) => {
|
|
141
|
+
let best = null;
|
|
142
|
+
for (const value of values)
|
|
143
|
+
if (typeof value === "number" &&
|
|
144
|
+
value > now &&
|
|
145
|
+
(best === null || value < best))
|
|
146
|
+
best = value;
|
|
147
|
+
return best;
|
|
148
|
+
};
|
|
149
|
+
if ("time" in when)
|
|
150
|
+
return nearest([when.time.min, when.time.max]);
|
|
151
|
+
if ("all" in when)
|
|
152
|
+
return nearest(when.all.map((c) => nextTime(c, now)));
|
|
153
|
+
if ("any" in when)
|
|
154
|
+
return nearest(when.any.map((c) => nextTime(c, now)));
|
|
155
|
+
if ("not" in when)
|
|
156
|
+
return nextTime(when.not, now);
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
/** One flip, as the player sends it. */
|
|
160
|
+
function notify(host, inner, kept, edge, range) {
|
|
161
|
+
const data = {
|
|
162
|
+
trigger: kept.trigger,
|
|
163
|
+
seq: kept.seq,
|
|
164
|
+
edge,
|
|
165
|
+
state: structuredClone(host.source),
|
|
166
|
+
};
|
|
167
|
+
const list = listOf(kept.when);
|
|
168
|
+
if (range !== null && list) {
|
|
169
|
+
const [start, end] = list.ranges[range];
|
|
170
|
+
data.range = { start, end: end ?? null, index: range };
|
|
171
|
+
}
|
|
172
|
+
deliver(host, inner, `triggers.fired ${key(kept.trigger)} ${edge}`, "triggers.fired", data);
|
|
173
|
+
}
|
|
174
|
+
/** Evaluates `only` (or every kept condition) and sends what flipped (T1, T10). */
|
|
175
|
+
export function evaluateTriggers(host, inner, only) {
|
|
176
|
+
const state = triggerState(host);
|
|
177
|
+
if (state.busy)
|
|
178
|
+
return;
|
|
179
|
+
state.busy = true;
|
|
180
|
+
try {
|
|
181
|
+
const facts = conditionFacts(host, state);
|
|
182
|
+
for (const kept of only ? [only] : [...state.kept.values()]) {
|
|
183
|
+
const truth = evaluateTruth(kept.when, { ...facts, bound: kept.bound });
|
|
184
|
+
const was = kept.entered;
|
|
185
|
+
if (!was && truth.holds) {
|
|
186
|
+
kept.entered = { range: truth.range };
|
|
187
|
+
notify(host, inner, kept, "enter", truth.range);
|
|
188
|
+
}
|
|
189
|
+
else if (was && !truth.holds) {
|
|
190
|
+
kept.entered = null;
|
|
191
|
+
notify(host, inner, kept, "leave", was.range);
|
|
192
|
+
}
|
|
193
|
+
else if (was && truth.holds && was.range !== truth.range) {
|
|
194
|
+
kept.entered = { range: truth.range };
|
|
195
|
+
notify(host, inner, kept, "leave", was.range);
|
|
196
|
+
notify(host, inner, kept, "enter", truth.range);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
finally {
|
|
201
|
+
state.busy = false;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** Sends one notification or wake event; held, and a wake, while parked. */
|
|
205
|
+
function deliver(host, inner, merge, event, data) {
|
|
206
|
+
const state = triggerState(host);
|
|
207
|
+
if (!state.parked && !state.waking) {
|
|
208
|
+
host.emit(event, data);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
// The backlog keeps the newest of each key, in their order (§4.9).
|
|
212
|
+
state.backlog = state.backlog.filter(([other]) => other !== merge);
|
|
213
|
+
state.backlog.push([merge, event, data]);
|
|
214
|
+
wake(host, inner);
|
|
215
|
+
}
|
|
216
|
+
/** Starts a parked plugin and delivers what waits for it, at its ready. */
|
|
217
|
+
function wake(host, inner) {
|
|
218
|
+
const state = triggerState(host);
|
|
219
|
+
if (state.waking || !state.parked || !state.start)
|
|
220
|
+
return;
|
|
221
|
+
const start = state.start;
|
|
222
|
+
inner.inFlight++;
|
|
223
|
+
state.waking = (async () => {
|
|
224
|
+
try {
|
|
225
|
+
state.parked = false;
|
|
226
|
+
await start();
|
|
227
|
+
state.flushing = true;
|
|
228
|
+
for (;;) {
|
|
229
|
+
const next = state.backlog.shift();
|
|
230
|
+
if (!next)
|
|
231
|
+
break;
|
|
232
|
+
host.emit(next[1], next[2]);
|
|
233
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
finally {
|
|
237
|
+
state.flushing = false;
|
|
238
|
+
state.waking = undefined;
|
|
239
|
+
inner.inFlight--;
|
|
240
|
+
}
|
|
241
|
+
})();
|
|
242
|
+
state.waking.catch((error) => console.error("# TestHost wake:", error));
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* `emit` of `event`: the trigger conditions are evaluated first for the
|
|
246
|
+
* player's own state changes; a wake event for a parked plugin that listened
|
|
247
|
+
* to it is held and wakes it (`true`: not delivered now).
|
|
248
|
+
*/
|
|
249
|
+
export function hearEvent(host, inner, event, data) {
|
|
250
|
+
if (event === "source.state" ||
|
|
251
|
+
event === "window.state" ||
|
|
252
|
+
event === "appearance.changed")
|
|
253
|
+
evaluateTriggers(host, inner);
|
|
254
|
+
const state = states.get(host);
|
|
255
|
+
if (!state || state.flushing || (!state.parked && !state.waking))
|
|
256
|
+
return false;
|
|
257
|
+
// Starting: what is not a wake event reaches it now, as the player sends
|
|
258
|
+
// it; a wake event waits behind what woke it.
|
|
259
|
+
if (!state.wakes.has(event))
|
|
260
|
+
return state.parked;
|
|
261
|
+
const source = data
|
|
262
|
+
?.state?.stamp?.generation;
|
|
263
|
+
const merge = event.startsWith("source.") ? `${event} ${source}` : event;
|
|
264
|
+
deliver(host, inner, merge, event, data);
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
/** `TestHost.triggers`. */
|
|
268
|
+
export function keptTriggers(host) {
|
|
269
|
+
return [...triggerState(host).kept.values()].map((kept) => Object.freeze({
|
|
270
|
+
trigger: kept.trigger,
|
|
271
|
+
when: structuredClone(kept.when),
|
|
272
|
+
seq: kept.seq,
|
|
273
|
+
entered: kept.entered !== null,
|
|
274
|
+
}));
|
|
275
|
+
}
|
|
276
|
+
/** `TestHost.advance`: the clock and, while playing, the position move by `ms`. */
|
|
277
|
+
export async function advance(host, inner, ms) {
|
|
278
|
+
if (!(ms >= 0) || !Number.isFinite(ms))
|
|
279
|
+
throw new TypeError("advance takes a number of milliseconds, 0 or more");
|
|
280
|
+
const state = triggerState(host);
|
|
281
|
+
let left = ms;
|
|
282
|
+
let moved = false;
|
|
283
|
+
for (let guard = 0; guard < 100_000; guard++) {
|
|
284
|
+
const source = host.source;
|
|
285
|
+
const running = source.phase === "ready" && !source.paused && source.position !== null;
|
|
286
|
+
const rate = source.rate > 0 ? source.rate : 1;
|
|
287
|
+
const position = source.position ?? 0;
|
|
288
|
+
// The next place a condition can change at: a boundary of a position
|
|
289
|
+
// list bound to this source, or a time bound.
|
|
290
|
+
let step = left;
|
|
291
|
+
let boundary = null;
|
|
292
|
+
if (running)
|
|
293
|
+
for (const kept of state.kept.values()) {
|
|
294
|
+
const list = listOf(kept.when);
|
|
295
|
+
if (!list || kept.bound !== source.stamp.generation)
|
|
296
|
+
continue;
|
|
297
|
+
for (const [start, end] of list.ranges)
|
|
298
|
+
for (const at of [start - (list.lead ?? 0), end ?? Infinity])
|
|
299
|
+
if (at > position) {
|
|
300
|
+
const wait = ((at - position) / rate) * 1000;
|
|
301
|
+
if (wait <= step) {
|
|
302
|
+
step = wait;
|
|
303
|
+
boundary = at;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
let timed = false;
|
|
308
|
+
for (const kept of state.kept.values()) {
|
|
309
|
+
const due = nextTime(kept.when, state.now);
|
|
310
|
+
if (due !== null && due - state.now <= step) {
|
|
311
|
+
step = due - state.now;
|
|
312
|
+
boundary = running ? boundary : null;
|
|
313
|
+
timed = true;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
state.now += step;
|
|
317
|
+
left -= step;
|
|
318
|
+
if (running && step > 0) {
|
|
319
|
+
const at = boundary !== null && !timed
|
|
320
|
+
? boundary
|
|
321
|
+
: position + (rate * step) / 1000;
|
|
322
|
+
host.source = { ...host.source, position: at };
|
|
323
|
+
moved = true;
|
|
324
|
+
}
|
|
325
|
+
evaluateTriggers(host, inner);
|
|
326
|
+
await host.settle();
|
|
327
|
+
if (left <= 0)
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
// The position as the player's next state push says it.
|
|
331
|
+
if (moved)
|
|
332
|
+
host.setSource({ position: host.source.position });
|
|
333
|
+
}
|
|
334
|
+
/** `TestHost.park`: the player puts the plugin in standby. */
|
|
335
|
+
export async function park(host, _inner) {
|
|
336
|
+
const state = triggerState(host);
|
|
337
|
+
if (state.parked)
|
|
338
|
+
return;
|
|
339
|
+
if (!state.stop)
|
|
340
|
+
throw new Error("park needs a plugin started with startPlugin");
|
|
341
|
+
state.wakes = new Set([...host.subscriptions].filter((event) => WAKE_EVENTS.includes(event)));
|
|
342
|
+
await state.stop("idle");
|
|
343
|
+
state.parked = true;
|
|
344
|
+
state.run++;
|
|
345
|
+
}
|
|
346
|
+
/** `TestHost.upgrade`: another version replaces the plugin (§4.2). */
|
|
347
|
+
export async function upgrade(host, _inner, entry) {
|
|
348
|
+
const state = triggerState(host);
|
|
349
|
+
if (!state.start || !state.stop)
|
|
350
|
+
throw new Error("upgrade needs a plugin started with startPlugin");
|
|
351
|
+
if (!state.parked)
|
|
352
|
+
await state.stop("upgrade");
|
|
353
|
+
for (const [id, kept] of [...state.kept])
|
|
354
|
+
if (typeof kept.trigger === "number")
|
|
355
|
+
state.kept.delete(id);
|
|
356
|
+
state.unnamed = null;
|
|
357
|
+
state.parked = false;
|
|
358
|
+
state.run++;
|
|
359
|
+
await state.start(entry);
|
|
360
|
+
await host.settle();
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* What a replaced condition keeps of "entered" (the player's
|
|
364
|
+
* `Triggers::set`): a range list for the same source whose position is
|
|
365
|
+
* still in the very range it was entered with (the same start, end and
|
|
366
|
+
* lead; its place may move) stays entered, so the replacement sends no
|
|
367
|
+
* second `enter`; anything else is entered anew at once (T3, T6).
|
|
368
|
+
*/
|
|
369
|
+
function carried(host, old, when, bound) {
|
|
370
|
+
if (!old?.entered || old.entered.range === null || bound === null)
|
|
371
|
+
return null;
|
|
372
|
+
if (old.bound !== bound)
|
|
373
|
+
return null;
|
|
374
|
+
const before = listOf(old.when);
|
|
375
|
+
const after = listOf(when);
|
|
376
|
+
if (!before || !after || (before.lead ?? 0) !== (after.lead ?? 0))
|
|
377
|
+
return null;
|
|
378
|
+
const truth = evaluateTruth(when, { ...conditionFacts(host), bound });
|
|
379
|
+
if (!truth.holds || truth.range === null)
|
|
380
|
+
return null;
|
|
381
|
+
const was = before.ranges[old.entered.range];
|
|
382
|
+
const now = after.ranges[truth.range];
|
|
383
|
+
return was &&
|
|
384
|
+
now &&
|
|
385
|
+
was[0] === now[0] &&
|
|
386
|
+
(was[1] ?? null) === (now[1] ?? null)
|
|
387
|
+
? { range: truth.range }
|
|
388
|
+
: null;
|
|
389
|
+
}
|
|
390
|
+
/** The default handlers of this area's wire methods, by method name. */
|
|
391
|
+
export function triggersHandlers(host, inner) {
|
|
392
|
+
return {
|
|
393
|
+
"triggers.set": (p) => {
|
|
394
|
+
const state = triggerState(host);
|
|
395
|
+
const trigger = parseTrigger(p.trigger);
|
|
396
|
+
const when = check(p.when);
|
|
397
|
+
const bound = listOf(when) ? host.source.stamp.generation : null;
|
|
398
|
+
const id = key(trigger);
|
|
399
|
+
const kept = state.kept.get(id);
|
|
400
|
+
if (kept &&
|
|
401
|
+
JSON.stringify(kept.when) === JSON.stringify(when) &&
|
|
402
|
+
kept.bound === bound) {
|
|
403
|
+
kept.run = state.run;
|
|
404
|
+
return { seq: kept.seq };
|
|
405
|
+
}
|
|
406
|
+
if (!kept && state.kept.size >= TRIGGER_LIMITS.triggers)
|
|
407
|
+
throw new HostError("limit", `a plugin keeps at most ${TRIGGER_LIMITS.triggers} trigger conditions`);
|
|
408
|
+
// Still in the range it was entered with: entered, under the same
|
|
409
|
+
// number (its `enter` may still be on its way to the plugin).
|
|
410
|
+
const entered = carried(host, kept, when, bound);
|
|
411
|
+
const fresh = {
|
|
412
|
+
trigger,
|
|
413
|
+
when: structuredClone(when),
|
|
414
|
+
seq: entered && kept ? kept.seq : ++state.seq,
|
|
415
|
+
bound,
|
|
416
|
+
entered,
|
|
417
|
+
run: state.run,
|
|
418
|
+
};
|
|
419
|
+
state.kept.set(id, fresh);
|
|
420
|
+
// After the answer, as the plugin sees it: entered at once (T3).
|
|
421
|
+
setImmediate(() => evaluateTriggers(host, inner, fresh));
|
|
422
|
+
return { seq: fresh.seq };
|
|
423
|
+
},
|
|
424
|
+
"triggers.off": (p) => {
|
|
425
|
+
const state = triggerState(host);
|
|
426
|
+
state.kept.delete(key(parseTrigger(p.trigger)));
|
|
427
|
+
return { seq: ++state.seq };
|
|
428
|
+
},
|
|
429
|
+
"triggers.declare": (p) => {
|
|
430
|
+
const state = triggerState(host);
|
|
431
|
+
const names = Array.isArray(p.names) ? p.names.map(String) : [];
|
|
432
|
+
const unnamed = typeof p.unnamed === "number" ? p.unnamed : 0;
|
|
433
|
+
for (const [id, kept] of [...state.kept]) {
|
|
434
|
+
const drop = typeof kept.trigger === "string"
|
|
435
|
+
? !names.includes(kept.trigger)
|
|
436
|
+
: kept.run !== state.run &&
|
|
437
|
+
(kept.trigger >= unnamed ||
|
|
438
|
+
(state.unnamed !== null && state.unnamed !== unnamed));
|
|
439
|
+
if (drop)
|
|
440
|
+
state.kept.delete(id);
|
|
441
|
+
}
|
|
442
|
+
state.unnamed = unnamed;
|
|
443
|
+
return null;
|
|
444
|
+
},
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
/** `matchedRange` for tests of the host's position lists. */
|
|
448
|
+
export { matchedRange };
|
|
@@ -9,7 +9,7 @@ import type { Timeline, TimelineMark, TimelineRange } from "./types.ts";
|
|
|
9
9
|
* player's tests check that these agree with the player.
|
|
10
10
|
*
|
|
11
11
|
* @example Keep a long list of ranges inside the limit
|
|
12
|
-
* The plugin publishes the first
|
|
12
|
+
* The plugin publishes the first 1000 of its ranges; the player refuses a
|
|
13
13
|
* longer list whole.
|
|
14
14
|
* ```ts
|
|
15
15
|
* import type { Plugin } from "@vestra-player/sdk";
|
|
@@ -33,8 +33,8 @@ import type { Timeline, TimelineMark, TimelineRange } from "./types.ts";
|
|
|
33
33
|
export declare const TIMELINE_LIMITS: Readonly<{
|
|
34
34
|
/** Marks in one layer (`TimelineLayer.set`) (1000). */
|
|
35
35
|
readonly marks: 1000;
|
|
36
|
-
/** Ranges in one layer (`TimelineLayer.setRanges`) (100). */
|
|
37
|
-
readonly ranges:
|
|
36
|
+
/** Ranges in one layer (`TimelineLayer.setRanges`) (1000; 100 before SDK 1.10.0). */
|
|
37
|
+
readonly ranges: 1000;
|
|
38
38
|
/** Characters of a mark's or a range's title, as shown (100). */
|
|
39
39
|
readonly titleChars: 100;
|
|
40
40
|
}>;
|
package/dist/plugin/timeline.js
CHANGED
|
@@ -8,7 +8,7 @@ import { failure, quiet } from "./errors.js";
|
|
|
8
8
|
* player's tests check that these agree with the player.
|
|
9
9
|
*
|
|
10
10
|
* @example Keep a long list of ranges inside the limit
|
|
11
|
-
* The plugin publishes the first
|
|
11
|
+
* The plugin publishes the first 1000 of its ranges; the player refuses a
|
|
12
12
|
* longer list whole.
|
|
13
13
|
* ```ts
|
|
14
14
|
* import type { Plugin } from "@vestra-player/sdk";
|
|
@@ -32,8 +32,8 @@ import { failure, quiet } from "./errors.js";
|
|
|
32
32
|
export const TIMELINE_LIMITS = Object.freeze({
|
|
33
33
|
/** Marks in one layer (`TimelineLayer.set`) (1000). */
|
|
34
34
|
marks: 1000,
|
|
35
|
-
/** Ranges in one layer (`TimelineLayer.setRanges`) (100). */
|
|
36
|
-
ranges:
|
|
35
|
+
/** Ranges in one layer (`TimelineLayer.setRanges`) (1000; 100 before SDK 1.10.0). */
|
|
36
|
+
ranges: 1000,
|
|
37
37
|
/** Characters of a mark's or a range's title, as shown (100). */
|
|
38
38
|
titleChars: 100,
|
|
39
39
|
});
|