@vestra-player/sdk 1.8.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.
- package/CHANGELOG.md +64 -2
- package/README.md +2 -2
- package/bin/vestra-plugin.mjs +98 -106
- package/capability-scan.json +21 -1
- package/dist/api-requests.d.ts +15 -39
- package/dist/api-requests.js +28 -50
- package/dist/capability-scan.d.ts +1 -1
- package/dist/capability-scan.js +8 -9
- 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/docs.d.ts +1 -1
- package/dist/docs.js +1 -1
- package/dist/i18n-tools.d.ts +7 -0
- package/dist/i18n-tools.js +66 -3
- package/dist/index.d.ts +4 -5
- package/dist/manifest.d.ts +5 -0
- package/dist/manifest.js +9 -24
- package/dist/member-uses.d.ts +61 -0
- package/dist/member-uses.js +160 -0
- package/dist/player-runtime.d.ts +4 -2
- package/dist/player-runtime.js +4 -2
- package/dist/plugin/appearance.d.ts +1 -1
- package/dist/plugin/appearance.js +2 -2
- package/dist/plugin/bindings.d.ts +1 -1
- package/dist/plugin/bindings.js +2 -2
- package/dist/plugin/commands.js +1 -12
- package/dist/plugin/connection.d.ts +38 -8
- package/dist/plugin/connection.js +126 -37
- package/dist/plugin/drops.js +1 -1
- package/dist/plugin/index.d.ts +7 -0
- package/dist/plugin/index.js +22 -15
- package/dist/plugin/media.js +0 -12
- package/dist/plugin/mpv.js +39 -12
- package/dist/plugin/packages.js +1 -2
- package/dist/plugin/player/events.d.ts +17 -7
- package/dist/plugin/player/events.js +26 -9
- package/dist/plugin/player.d.ts +37 -13
- package/dist/plugin/player.js +133 -22
- package/dist/plugin/scope.d.ts +4 -1
- package/dist/plugin/scope.js +8 -0
- package/dist/plugin/services.d.ts +4 -4
- package/dist/plugin/services.js +7 -29
- package/dist/plugin/settings.js +1 -1
- package/dist/plugin/shaders.js +0 -2
- package/dist/plugin/stream.d.ts +2 -1
- package/dist/plugin/stream.js +2 -2
- package/dist/plugin/testing/commands.d.ts +3 -10
- package/dist/plugin/testing/commands.js +24 -37
- package/dist/plugin/testing/index.d.ts +133 -116
- package/dist/plugin/testing/index.js +198 -125
- package/dist/plugin/testing/media.d.ts +6 -0
- package/dist/plugin/testing/media.js +15 -2
- package/dist/plugin/testing/processing.js +1 -1
- package/dist/plugin/testing/services.js +0 -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 +5 -6
- package/dist/plugin/timeline.js +7 -11
- package/dist/plugin/triggers.d.ts +55 -0
- package/dist/plugin/triggers.js +233 -0
- package/dist/plugin/types/base.d.ts +77 -87
- package/dist/plugin/types/commands.d.ts +384 -68
- package/dist/plugin/types/packages.d.ts +17 -55
- package/dist/plugin/types/playback.d.ts +382 -152
- package/dist/plugin/types/plugin-wire.d.ts +6 -36
- package/dist/plugin/types/processing.d.ts +10 -49
- package/dist/plugin/types/services.d.ts +11 -70
- package/dist/plugin/types/snapshots.d.ts +7 -60
- package/dist/plugin/types/sources.d.ts +2 -11
- package/dist/plugin/types/subtitles.d.ts +5 -26
- package/dist/plugin/types/ui-content.d.ts +1 -16
- package/dist/plugin/types/ui-layout.d.ts +53 -79
- package/dist/plugin/types/ui-locale.d.ts +5 -23
- package/dist/plugin/types/ui-shell.d.ts +13 -59
- package/dist/plugin/view/layout.js +1 -1
- package/dist/plugin/view.js +0 -2
- package/dist/plugin/when.d.ts +78 -4
- package/dist/plugin/when.js +182 -14
- package/dist/plugin/window.d.ts +1 -1
- package/dist/plugin/window.js +1 -1
- package/dist/plugin/wire-facade.js +1 -2
- package/dist/processing.d.ts +2 -3
- package/dist/protocol.d.ts +14 -0
- package/dist/protocol.js +14 -0
- package/dist/request-queue.js +1 -1
- package/dist/slint-version.d.ts +7 -0
- package/dist/slint-version.js +7 -0
- package/dist/subtitles.d.ts +1 -1
- package/dist/ts-plugin.d.ts +8 -15
- package/dist/ts-plugin.js +3 -141
- package/dist/wire.d.ts +22 -12
- package/dist/wire.js +17 -8
- package/llms.txt +6 -6
- package/package.json +1 -2
- package/dist/capabilities.d.ts +0 -107
- package/dist/capabilities.js +0 -69
- package/dist/capability-check.d.ts +0 -176
- package/dist/capability-check.js +0 -461
- package/dist/capability-rules.d.ts +0 -125
- package/dist/capability-rules.js +0 -192
- package/dist/host-capabilities.d.ts +0 -10
- package/dist/host-capabilities.js +0 -492
|
@@ -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,14 +33,14 @@ 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
|
}>;
|
|
41
41
|
/**
|
|
42
|
-
* One per plugin: read
|
|
43
|
-
* current from `timeline.changed` (
|
|
42
|
+
* One per plugin: read by `plugin.player.read()`, then kept
|
|
43
|
+
* current from `timeline.changed` (after `plugin.player.subscribe()`) once the plugin uses it
|
|
44
44
|
* (or listens to that event with `player.on`).
|
|
45
45
|
*/
|
|
46
46
|
export declare class TimelineService {
|
|
@@ -49,7 +49,6 @@ export declare class TimelineService {
|
|
|
49
49
|
ranges: readonly TimelineRange[];
|
|
50
50
|
layer: boolean;
|
|
51
51
|
constructor(runtime: Runtime);
|
|
52
|
-
init(): Promise<void>;
|
|
53
52
|
start(): void;
|
|
54
53
|
accept(lists: WireTimeline): void;
|
|
55
54
|
read(): Promise<void>;
|
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
|
});
|
|
@@ -51,8 +51,8 @@ const viewRanges = (ranges) => Object.freeze(ranges.map((range) => Object.freeze
|
|
|
51
51
|
plugin: range.plugin,
|
|
52
52
|
})));
|
|
53
53
|
/**
|
|
54
|
-
* One per plugin: read
|
|
55
|
-
* current from `timeline.changed` (
|
|
54
|
+
* One per plugin: read by `plugin.player.read()`, then kept
|
|
55
|
+
* current from `timeline.changed` (after `plugin.player.subscribe()`) once the plugin uses it
|
|
56
56
|
* (or listens to that event with `player.on`).
|
|
57
57
|
*/
|
|
58
58
|
export class TimelineService {
|
|
@@ -63,10 +63,8 @@ export class TimelineService {
|
|
|
63
63
|
layer = false;
|
|
64
64
|
constructor(runtime) {
|
|
65
65
|
this.#runtime = runtime;
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (this.#runtime.can("playback.read"))
|
|
69
|
-
await this.read();
|
|
66
|
+
// `plugin.player.read()` reads the lists through the connection.
|
|
67
|
+
runtime.rereads.set("timeline.changed", () => this.read());
|
|
70
68
|
}
|
|
71
69
|
start() {
|
|
72
70
|
if (this.#started)
|
|
@@ -119,8 +117,6 @@ export function timeline(scope) {
|
|
|
119
117
|
layer() {
|
|
120
118
|
if (service().layer)
|
|
121
119
|
throw new Error("a plugin has one timeline layer; reuse it");
|
|
122
|
-
if (!scope.runtime.can("timeline.marks"))
|
|
123
|
-
throw failure("capability", "timeline.marks must be declared in manifest");
|
|
124
120
|
service().layer = true;
|
|
125
121
|
return new Layer(scope);
|
|
126
122
|
},
|
|
@@ -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;
|