@vestra-player/sdk 1.8.0 → 1.9.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 (86) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/README.md +2 -2
  3. package/bin/vestra-plugin.mjs +11 -100
  4. package/capability-scan.json +3 -1
  5. package/dist/api-requests.d.ts +11 -36
  6. package/dist/api-requests.js +18 -47
  7. package/dist/capability-scan.d.ts +1 -1
  8. package/dist/capability-scan.js +8 -9
  9. package/dist/docs-tree.json.gz +0 -0
  10. package/dist/docs.d.ts +1 -1
  11. package/dist/docs.js +1 -1
  12. package/dist/index.d.ts +4 -5
  13. package/dist/manifest.d.ts +5 -0
  14. package/dist/manifest.js +9 -24
  15. package/dist/member-uses.d.ts +61 -0
  16. package/dist/member-uses.js +160 -0
  17. package/dist/player-runtime.d.ts +1 -2
  18. package/dist/player-runtime.js +1 -2
  19. package/dist/plugin/appearance.d.ts +1 -1
  20. package/dist/plugin/appearance.js +2 -2
  21. package/dist/plugin/bindings.d.ts +1 -1
  22. package/dist/plugin/bindings.js +2 -2
  23. package/dist/plugin/commands.js +1 -12
  24. package/dist/plugin/connection.d.ts +26 -7
  25. package/dist/plugin/connection.js +68 -36
  26. package/dist/plugin/drops.js +1 -1
  27. package/dist/plugin/index.js +9 -15
  28. package/dist/plugin/media.js +0 -12
  29. package/dist/plugin/mpv.js +0 -7
  30. package/dist/plugin/packages.js +1 -2
  31. package/dist/plugin/player/events.d.ts +4 -3
  32. package/dist/plugin/player/events.js +7 -6
  33. package/dist/plugin/player.d.ts +21 -13
  34. package/dist/plugin/player.js +56 -21
  35. package/dist/plugin/services.d.ts +4 -4
  36. package/dist/plugin/services.js +7 -29
  37. package/dist/plugin/settings.js +1 -1
  38. package/dist/plugin/shaders.js +0 -2
  39. package/dist/plugin/stream.d.ts +2 -1
  40. package/dist/plugin/stream.js +2 -2
  41. package/dist/plugin/testing/commands.d.ts +3 -10
  42. package/dist/plugin/testing/commands.js +6 -15
  43. package/dist/plugin/testing/index.d.ts +26 -116
  44. package/dist/plugin/testing/index.js +30 -104
  45. package/dist/plugin/testing/processing.js +1 -1
  46. package/dist/plugin/testing/services.js +0 -1
  47. package/dist/plugin/timeline.d.ts +2 -3
  48. package/dist/plugin/timeline.js +4 -8
  49. package/dist/plugin/types/base.d.ts +40 -86
  50. package/dist/plugin/types/commands.d.ts +10 -67
  51. package/dist/plugin/types/packages.d.ts +17 -55
  52. package/dist/plugin/types/playback.d.ts +126 -144
  53. package/dist/plugin/types/plugin-wire.d.ts +6 -36
  54. package/dist/plugin/types/processing.d.ts +10 -49
  55. package/dist/plugin/types/services.d.ts +11 -70
  56. package/dist/plugin/types/snapshots.d.ts +7 -60
  57. package/dist/plugin/types/sources.d.ts +2 -11
  58. package/dist/plugin/types/subtitles.d.ts +5 -26
  59. package/dist/plugin/types/ui-content.d.ts +1 -16
  60. package/dist/plugin/types/ui-layout.d.ts +53 -79
  61. package/dist/plugin/types/ui-locale.d.ts +5 -23
  62. package/dist/plugin/types/ui-shell.d.ts +13 -59
  63. package/dist/plugin/view/layout.js +1 -1
  64. package/dist/plugin/view.js +0 -2
  65. package/dist/plugin/window.d.ts +1 -1
  66. package/dist/plugin/window.js +1 -1
  67. package/dist/plugin/wire-facade.js +1 -2
  68. package/dist/processing.d.ts +2 -3
  69. package/dist/protocol.d.ts +14 -0
  70. package/dist/protocol.js +14 -0
  71. package/dist/request-queue.js +1 -1
  72. package/dist/subtitles.d.ts +1 -1
  73. package/dist/ts-plugin.d.ts +8 -15
  74. package/dist/ts-plugin.js +3 -141
  75. package/dist/wire.d.ts +7 -12
  76. package/dist/wire.js +6 -8
  77. package/llms.txt +4 -4
  78. package/package.json +1 -2
  79. package/dist/capabilities.d.ts +0 -107
  80. package/dist/capabilities.js +0 -69
  81. package/dist/capability-check.d.ts +0 -176
  82. package/dist/capability-check.js +0 -461
  83. package/dist/capability-rules.d.ts +0 -125
  84. package/dist/capability-rules.js +0 -192
  85. package/dist/host-capabilities.d.ts +0 -10
  86. package/dist/host-capabilities.js +0 -492
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Where a plugin's TypeScript uses SDK members: the type-aware resolution
3
+ * the capability scan (`capability-scan.ts`) reads a plugin's code with. A
4
+ * use is a property access, an element access with a literal name or a
5
+ * destructured property whose symbol is the SDK's declaration of a member
6
+ * asked for, however the object was reached (`plugin.player.seek`,
7
+ * `const { player } = plugin; player.seek`, a parameter typed `Player`, a
8
+ * `Panel` from `panels.mount`). Not detected: access with a computed name
9
+ * (`player[name]`), values typed `any` or `unknown`, plain JavaScript
10
+ * without types.
11
+ *
12
+ * Requests sent by name (`plugin.wire.request`) are found too, with the
13
+ * method when its type fixes it to one string.
14
+ *
15
+ * The TypeScript module is passed in, never imported: the CLI uses the
16
+ * SDK's own compiler.
17
+ *
18
+ * @internal
19
+ */
20
+ import type TS from "typescript";
21
+ /** @internal The module plugins import the API types from. */
22
+ export declare const SDK_MODULE = "@vestra-player/sdk";
23
+ /** @internal One use of an SDK member, where {@link findMemberUses} found it. */
24
+ export interface MemberUse {
25
+ readonly file: string;
26
+ /** 1-based line and column of the member's name. */
27
+ readonly line: number;
28
+ readonly column: number;
29
+ /** The member, as it was asked for (`Clipboard.readText`). */
30
+ readonly api: string;
31
+ }
32
+ /**
33
+ * @internal A request sent by name (`plugin.wire.request("clipboard.readText")`):
34
+ * the method when the types fix it to one string, else `undefined` (a
35
+ * computed name, a union of several, the member taken as a value).
36
+ */
37
+ export interface RequestUse {
38
+ readonly file: string;
39
+ readonly line: number;
40
+ readonly column: number;
41
+ readonly method: string | undefined;
42
+ }
43
+ /**
44
+ * Every use, in `files`, of the SDK members named by `apis`
45
+ * (`Interface.member`), and every request sent by name (`Wire.request`),
46
+ * in file and position order. It reads no manifest (the capability scan,
47
+ * `capability-scan.ts`, gives it its lists).
48
+ *
49
+ * `resolved` is `false` when the program has no import of
50
+ * `@vestra-player/sdk` that resolves: nothing can be found by type then.
51
+ *
52
+ * @internal
53
+ */
54
+ export declare function findMemberUses(ts: typeof TS, program: TS.Program, options: {
55
+ readonly files: readonly string[];
56
+ readonly apis: Iterable<string>;
57
+ }): {
58
+ resolved: boolean;
59
+ uses: MemberUse[];
60
+ requests: RequestUse[];
61
+ };
@@ -0,0 +1,160 @@
1
+ /** @internal The module plugins import the API types from. */
2
+ export const SDK_MODULE = "@vestra-player/sdk";
3
+ /** The members that send a request by the method name they are given. */
4
+ const SENDERS = ["Wire.request"];
5
+ /**
6
+ * The module `@vestra-player/sdk` as the program resolved it: from the
7
+ * first import or export of it in the program's own (non-declaration) files.
8
+ */
9
+ function sdkModule(ts, program, checker) {
10
+ for (const file of program.getSourceFiles()) {
11
+ if (file.isDeclarationFile || program.isSourceFileFromExternalLibrary(file))
12
+ continue;
13
+ for (const statement of file.statements) {
14
+ const specifier = ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)
15
+ ? statement.moduleSpecifier
16
+ : undefined;
17
+ if (specifier &&
18
+ ts.isStringLiteral(specifier) &&
19
+ specifier.text === SDK_MODULE) {
20
+ const module = checker.getSymbolAtLocation(specifier);
21
+ if (module)
22
+ return module;
23
+ }
24
+ }
25
+ }
26
+ return undefined;
27
+ }
28
+ /** The SDK's declarations of the members named (`Interface.member`). */
29
+ function resolveMembers(ts, program, apis) {
30
+ const members = new Map();
31
+ const checker = program.getTypeChecker();
32
+ const module = sdkModule(ts, program, checker);
33
+ if (!module)
34
+ return members;
35
+ const exports = new Map();
36
+ for (const symbol of checker.getExportsOfModule(module))
37
+ exports.set(symbol.name, symbol.flags & ts.SymbolFlags.Alias
38
+ ? checker.getAliasedSymbol(symbol)
39
+ : symbol);
40
+ for (const api of apis) {
41
+ const [head, ...path] = api.split(".");
42
+ const root = exports.get(head);
43
+ if (!root || !path.length)
44
+ continue;
45
+ let type = checker.getDeclaredTypeOfSymbol(root);
46
+ let property;
47
+ for (const name of path) {
48
+ property = type?.getProperty(name);
49
+ type = property && checker.getTypeOfSymbol(property);
50
+ }
51
+ for (const declaration of property?.declarations ?? [])
52
+ if (!members.has(declaration))
53
+ members.set(declaration, api);
54
+ }
55
+ return members;
56
+ }
57
+ /**
58
+ * The strings an expression's type fixes: a string literal, a constant of
59
+ * one, or a union of them (the caller takes one only); `undefined` when
60
+ * the type allows any other.
61
+ */
62
+ function literalStrings(ts, checker, node) {
63
+ if (!node)
64
+ return undefined;
65
+ if (ts.isStringLiteralLike(node))
66
+ return [node.text];
67
+ const type = checker.getTypeAtLocation(node);
68
+ const parts = type.isUnion() ? type.types : [type];
69
+ const values = [];
70
+ for (const part of parts) {
71
+ if (!part.isStringLiteral())
72
+ return undefined;
73
+ values.push(part.value);
74
+ }
75
+ return values.length ? values : undefined;
76
+ }
77
+ /**
78
+ * Every use, in `files`, of the SDK members named by `apis`
79
+ * (`Interface.member`), and every request sent by name (`Wire.request`),
80
+ * in file and position order. It reads no manifest (the capability scan,
81
+ * `capability-scan.ts`, gives it its lists).
82
+ *
83
+ * `resolved` is `false` when the program has no import of
84
+ * `@vestra-player/sdk` that resolves: nothing can be found by type then.
85
+ *
86
+ * @internal
87
+ */
88
+ export function findMemberUses(ts, program, options) {
89
+ const uses = [];
90
+ const requests = [];
91
+ const members = resolveMembers(ts, program, options.apis);
92
+ const senders = resolveMembers(ts, program, SENDERS);
93
+ const resolved = members.size > 0 || senders.size > 0;
94
+ if (!resolved)
95
+ return { resolved, uses, requests };
96
+ const checker = program.getTypeChecker();
97
+ const apiOf = (table, symbol) => {
98
+ for (const declaration of symbol?.declarations ?? []) {
99
+ const api = table.get(declaration);
100
+ if (api)
101
+ return api;
102
+ }
103
+ return undefined;
104
+ };
105
+ for (const fileName of options.files) {
106
+ const file = program.getSourceFile(fileName);
107
+ if (!file)
108
+ continue;
109
+ const position = (name) => {
110
+ const { line, character } = file.getLineAndCharacterOfPosition(name.getStart(file));
111
+ return { file: file.fileName, line: line + 1, column: character + 1 };
112
+ };
113
+ const seen = (name, symbol) => {
114
+ const api = apiOf(members, symbol);
115
+ if (api)
116
+ uses.push({ ...position(name), api });
117
+ if (!apiOf(senders, symbol))
118
+ return;
119
+ // A direct call with one literal name is a request of that name;
120
+ // anything else (a computed name, the member taken as a value) is a
121
+ // request the scan cannot name.
122
+ const access = name.parent;
123
+ const call = access?.parent;
124
+ const direct = access &&
125
+ (ts.isPropertyAccessExpression(access) ||
126
+ ts.isElementAccessExpression(access)) &&
127
+ call &&
128
+ ts.isCallExpression(call) &&
129
+ call.expression === access
130
+ ? call
131
+ : undefined;
132
+ const values = direct
133
+ ? literalStrings(ts, checker, direct.arguments[0])
134
+ : undefined;
135
+ requests.push({
136
+ ...position(name),
137
+ method: values?.length === 1 ? values[0] : undefined,
138
+ });
139
+ };
140
+ const visit = (node) => {
141
+ if (ts.isPropertyAccessExpression(node))
142
+ seen(node.name, checker.getSymbolAtLocation(node.name));
143
+ else if (ts.isElementAccessExpression(node) &&
144
+ ts.isStringLiteralLike(node.argumentExpression))
145
+ seen(node.argumentExpression, checker.getSymbolAtLocation(node.argumentExpression));
146
+ else if (ts.isBindingElement(node) &&
147
+ ts.isObjectBindingPattern(node.parent)) {
148
+ const name = node.propertyName ?? node.name;
149
+ const text = ts.isIdentifier(name) || ts.isStringLiteralLike(name)
150
+ ? name.text
151
+ : undefined;
152
+ if (text !== undefined)
153
+ seen(name, checker.getTypeAtLocation(node.parent).getProperty(text));
154
+ }
155
+ ts.forEachChild(node, visit);
156
+ };
157
+ visit(file);
158
+ }
159
+ return { resolved, uses, requests };
160
+ }
@@ -11,8 +11,7 @@ export { createPlugin } from "./plugin/index.ts";
11
11
  export type { PluginInstance, PluginManifest, RuntimeOptions, } from "./plugin/index.ts";
12
12
  export { HostError } from "./wire.ts";
13
13
  export type { Json, Transport } from "./wire.ts";
14
- export { API_VERSION, CAPABILITIES, CAPABILITY_INFO, MESSAGE_LIMIT, } from "./capabilities.ts";
15
- export { HOST_CAPABILITIES } from "./host-capabilities.ts";
14
+ export { API_VERSION, MESSAGE_LIMIT } from "./protocol.ts";
16
15
  export { BAR_BUTTON_ICONS, BAR_BUTTON_LIMITS, BUILTIN_COMMANDS, BUILTIN_SUBMENUS, COMMAND_LIMITS, MENU_IDS, } from "./commands.ts";
17
16
  export { BINDING_INPUTS } from "./plugin/bindings.ts";
18
17
  export { MEDIA_SOURCE_LIMITS } from "./plugin/media.ts";
@@ -9,8 +9,7 @@
9
9
  */
10
10
  export { createPlugin } from "./plugin/index.js";
11
11
  export { HostError } from "./wire.js";
12
- export { API_VERSION, CAPABILITIES, CAPABILITY_INFO, MESSAGE_LIMIT, } from "./capabilities.js";
13
- export { HOST_CAPABILITIES } from "./host-capabilities.js";
12
+ export { API_VERSION, MESSAGE_LIMIT } from "./protocol.js";
14
13
  export { BAR_BUTTON_ICONS, BAR_BUTTON_LIMITS, BUILTIN_COMMANDS, BUILTIN_SUBMENUS, COMMAND_LIMITS, MENU_IDS, } from "./commands.js";
15
14
  export { BINDING_INPUTS } from "./plugin/bindings.js";
16
15
  export { MEDIA_SOURCE_LIMITS } from "./plugin/media.js";
@@ -5,7 +5,7 @@ import type { Appearance, LanguagePreference, ThemeId } from "./types.ts";
5
5
  export declare const THEMES: readonly ThemeId[];
6
6
  /**
7
7
  * One per plugin: the value comes with the `window.state` read before
8
- * activation; with `events`, `appearance.changed` keeps it current once
8
+ * activation; after `plugin.player.subscribe()`, `appearance.changed` keeps it current once
9
9
  * the plugin uses it (or listens to that event with `player.on`).
10
10
  */
11
11
  declare class AppearanceService {
@@ -1,4 +1,4 @@
1
- /** The player's palette (`plugin.appearance`; `ui.theme` to change it). */
1
+ /** The player's palette (`plugin.appearance`). */
2
2
  import { failure } from "./errors.js";
3
3
  import { windowOf } from "./window.js";
4
4
  import { LOCALES } from "../i18n.js";
@@ -9,7 +9,7 @@ const preference = (value) => value === "system" || LOCALES.includes(value)
9
9
  export const THEMES = ["vestra", "vestra-light"];
10
10
  /**
11
11
  * One per plugin: the value comes with the `window.state` read before
12
- * activation; with `events`, `appearance.changed` keeps it current once
12
+ * activation; after `plugin.player.subscribe()`, `appearance.changed` keeps it current once
13
13
  * the plugin uses it (or listens to that event with `player.on`).
14
14
  */
15
15
  class AppearanceService {
@@ -76,7 +76,7 @@ export declare const BINDING_INPUTS: Readonly<{
76
76
  /** Throws `invalid` unless `text` is a canonical binding; returns it unchanged. */
77
77
  export declare function canonical(text: string): string;
78
78
  export declare function checkBindings(bindings: readonly string[]): string[];
79
- /** `plugin.bindings` (`bindings.manage`): the table and the user overlay. */
79
+ /** `plugin.bindings`: the table and the user overlay. */
80
80
  export declare const bindingsApi: (scope: ScopeImpl) => Bindings;
81
81
  /**
82
82
  * How the player's menus write a canonical binding (`bindings.rs`
@@ -177,7 +177,7 @@ const reference = (ref) => {
177
177
  throw failure("invalid", "a binding ref names plugin and command");
178
178
  return { plugin: ref.plugin, command: ref.command };
179
179
  };
180
- /** `plugin.bindings` (`bindings.manage`): the table and the user overlay. */
180
+ /** `plugin.bindings`: the table and the user overlay. */
181
181
  export const bindingsApi = (scope) => ({
182
182
  async list() {
183
183
  const table = await scope.runtime.host.bindings.list();
@@ -209,7 +209,7 @@ export const bindingsApi = (scope) => ({
209
209
  await scope.runtime.host.bindings.reset(ref === undefined ? undefined : reference(ref));
210
210
  },
211
211
  onChange(listener) {
212
- const watch = scope.runtime.on("bindings.changed", () => scope.run(listener), "bindings.manage");
212
+ const watch = scope.runtime.on("bindings.changed", () => scope.run(listener), null);
213
213
  return scope.add({
214
214
  dispose: async () => {
215
215
  scope.forget(watch);
@@ -304,10 +304,7 @@ class KeysService {
304
304
  return;
305
305
  this.#started = true;
306
306
  const runtime = this.#runtime;
307
- // Without `commands` the plugin has no commands: every read is [].
308
- if (!runtime.can("commands"))
309
- return;
310
- runtime.on(KEYS, (data) => this.#accept(data), "commands", true);
307
+ runtime.on(KEYS, (data) => this.#accept(data), null, true);
311
308
  // Subscribed first: a change after this answer comes as an event.
312
309
  runtime.host.commands.keys().then((answer) => this.#accept(answer),
313
310
  // `unsupported`: no keys, as without a binding.
@@ -359,11 +356,9 @@ export const commands = (scope) => ({
359
356
  return service.get(id);
360
357
  },
361
358
  list: async () => {
362
- needs(scope, "commands.run");
363
359
  return scope.runtime.host.commands.list();
364
360
  },
365
361
  async run(id, options) {
366
- needs(scope, "commands.run");
367
362
  if (typeof id !== "string" || id === "")
368
363
  throw failure("invalid", "run takes a command id: <plugin>/<id>");
369
364
  const { ticket, step, of, then } = (options ?? {});
@@ -394,11 +389,6 @@ export const commands = (scope) => ({
394
389
  });
395
390
  },
396
391
  });
397
- /** Refuse at once without `capability`, as the player does. */
398
- function needs(scope, capability) {
399
- if (!scope.runtime.can(capability))
400
- throw failure("capability", `${capability} must be declared in manifest`);
401
- }
402
392
  // ---------------------------------------------------------------- palette lead
403
393
  /** `PaletteLeadOptions` checked and in wire form; the player checks lengths. */
404
394
  function leadJson(options) {
@@ -441,7 +431,6 @@ class LeadHandle {
441
431
  #event;
442
432
  #disposed = false;
443
433
  constructor(scope, options) {
444
- needs(scope, "palette.lead");
445
434
  const first = leadJson(options);
446
435
  this.#scope = scope;
447
436
  const host = scope.runtime.host.palette;
@@ -12,9 +12,14 @@ export type Why = () => string;
12
12
  * the palette, a resolver), not playback.
13
13
  */
14
14
  export declare const WAKE_EVENTS: readonly string[];
15
+ /**
16
+ * What the runtime reads of the plugin's manifest: its id. Nothing in the
17
+ * manifest switches behaviour (1.9.0): event delivery and the start-up
18
+ * reads are the plugin's explicit calls (`plugin.player.subscribe()`,
19
+ * `plugin.player.read()`).
20
+ */
15
21
  export interface PluginManifest {
16
22
  id: string;
17
- capabilities: string[];
18
23
  }
19
24
  /** How the plugin is run: the player's runner, or `TestHost`. */
20
25
  export interface RuntimeOptions {
@@ -63,6 +68,11 @@ export declare class Connection {
63
68
  * `REQUEST_LIMITS` in flight, the rest wait in order (`request-queue.ts`).
64
69
  */
65
70
  readonly requests: RequestQueue;
71
+ /**
72
+ * The player's state is read again after the plugin's own actions:
73
+ * `plugin.player.read()` was called.
74
+ */
75
+ reads: boolean;
66
76
  readonly views: Map<string, (callback: string, args: Json[]) => void>;
67
77
  readonly commands: Map<string, () => void>;
68
78
  /**
@@ -104,7 +114,14 @@ export declare class Connection {
104
114
  */
105
115
  readonly features: ReadonlySet<string>;
106
116
  constructor(transport: Transport, manifest: PluginManifest, directory: string, dataDirectory?: string, app?: unknown, options?: RuntimeOptions);
107
- can(capability: string): boolean;
117
+ /** Event delivery is on (`plugin.player.subscribe()` was called). */
118
+ get delivering(): boolean;
119
+ /**
120
+ * Turn event delivery on (`plugin.player.subscribe()`): every later
121
+ * listener is subscribed, and so is what was added before and heard
122
+ * nothing. Idempotent.
123
+ */
124
+ deliver(): void;
108
125
  /** Read the state copy kept by `event` again, if one exists; never rejects. */
109
126
  reread(event: string): Promise<void>;
110
127
  /** Lazily created singletons shared by every scope of this plugin. */
@@ -133,17 +150,19 @@ export declare class Connection {
133
150
  callSite(): () => string | undefined;
134
151
  /**
135
152
  * Host events with one `events.subscribe` per name, however many listeners.
136
- * Without the `events` capability the listener simply never fires.
153
+ * An event that needs delivery (`"events"`) is subscribed only once
154
+ * `plugin.player.subscribe()` was called; until then its listener simply
155
+ * never fires.
137
156
  * `mirror`: the SDK's own copy of the state (`plugin.player.state` and
138
157
  * the like), not a listener of the plugin's (see {@link EventGroup}).
139
158
  */
140
159
  on(event: string, listener: Listener,
141
- /** The capability the event needs; `null`: none. */
142
- needs?: string | null, mirror?: boolean): Disposable;
160
+ /** `"events"`: it arrives after `plugin.player.subscribe()`; `null`: always. */
161
+ needs?: "events" | null, mirror?: boolean): Disposable;
143
162
  /**
144
163
  * Count `event` as listened to while held: a watcher of a service's
145
164
  * state, a binding that reads it. Nothing when nothing subscribed to it
146
- * (no capability, or the service did not start).
165
+ * (no `plugin.player.subscribe()`, or the service did not start).
147
166
  */
148
167
  hold(event: string, why?: Why): Disposable;
149
168
  /**
@@ -154,7 +173,7 @@ export declare class Connection {
154
173
  listening(): Map<string, string[]>;
155
174
  /**
156
175
  * One request by its method name, as given (`plugin.wire.request`): nothing
157
- * added, nothing checked here, the player checks the capability. Every other
176
+ * added, nothing checked here; the player checks the request. Every other
158
177
  * part of the SDK uses the typed clients (`host`); `scripts/check-layers.mjs`
159
178
  * keeps it so. It waits in the request queue like every other.
160
179
  */
@@ -88,8 +88,18 @@ export class Connection {
88
88
  */
89
89
  requests;
90
90
  #events = new Map();
91
- /** The missing `events` warning was given (once per plugin). */
91
+ /** The missing `subscribe()` warning was given (once per plugin). */
92
92
  #warnedEvents = false;
93
+ /**
94
+ * Event delivery is on: `plugin.player.subscribe()` was called
95
+ * ({@link Connection.deliver}).
96
+ */
97
+ #delivering = false;
98
+ /**
99
+ * The player's state is read again after the plugin's own actions:
100
+ * `plugin.player.read()` was called.
101
+ */
102
+ reads = false;
93
103
  views = new Map();
94
104
  commands = new Map();
95
105
  /**
@@ -169,8 +179,24 @@ export class Connection {
169
179
  this.commands.get(data.id)?.();
170
180
  });
171
181
  }
172
- can(capability) {
173
- return this.manifest.capabilities.includes(capability);
182
+ /** Event delivery is on (`plugin.player.subscribe()` was called). */
183
+ get delivering() {
184
+ return this.#delivering;
185
+ }
186
+ /**
187
+ * Turn event delivery on (`plugin.player.subscribe()`): every later
188
+ * listener is subscribed, and so is what was added before and heard
189
+ * nothing. Idempotent.
190
+ */
191
+ deliver() {
192
+ if (this.#delivering)
193
+ return;
194
+ this.#delivering = true;
195
+ for (const [event, group] of this.#events)
196
+ if (group.unheard) {
197
+ group.unheard = false;
198
+ this.#open(event, group);
199
+ }
174
200
  }
175
201
  /** Read the state copy kept by `event` again, if one exists; never rejects. */
176
202
  async reread(event) {
@@ -275,12 +301,14 @@ export class Connection {
275
301
  }
276
302
  /**
277
303
  * Host events with one `events.subscribe` per name, however many listeners.
278
- * Without the `events` capability the listener simply never fires.
304
+ * An event that needs delivery (`"events"`) is subscribed only once
305
+ * `plugin.player.subscribe()` was called; until then its listener simply
306
+ * never fires.
279
307
  * `mirror`: the SDK's own copy of the state (`plugin.player.state` and
280
308
  * the like), not a listener of the plugin's (see {@link EventGroup}).
281
309
  */
282
310
  on(event, listener,
283
- /** The capability the event needs; `null`: none. */
311
+ /** `"events"`: it arrives after `plugin.player.subscribe()`; `null`: always. */
284
312
  needs = "events", mirror = false) {
285
313
  let entry = this.#events.get(event);
286
314
  const created = !entry;
@@ -302,34 +330,12 @@ export class Connection {
302
330
  group.active++;
303
331
  group.whys.set(token, listenerWhy(this.callSite()));
304
332
  }
305
- if (created && needs === "events" && !this.can(needs))
333
+ if (created && needs === "events" && !this.#delivering)
306
334
  group.unheard = true;
307
335
  if (!mirror && group.unheard)
308
336
  this.#warnUnheard(event);
309
- if (created && (needs === null || this.can(needs))) {
310
- group.told = group.active > 0;
311
- const whys = group.told ? allWhys(group) : undefined;
312
- group.toldWhy = whys?.join("\n");
313
- group.off = this.host.events.subscribe(event, (data) => {
314
- // Split once: the state copies first, each on its own (one that
315
- // throws stops neither the others nor the plugin's listeners),
316
- // then the plugin's listeners in order.
317
- const mirrors = [];
318
- const others = [];
319
- for (const each of group.listeners)
320
- (group.mirrors.has(each) ? mirrors : others).push(each);
321
- for (const each of mirrors)
322
- try {
323
- each(data);
324
- }
325
- catch (error) {
326
- this.reportMirror(error);
327
- }
328
- for (const each of others)
329
- each(data);
330
- }, !group.told, whys);
331
- this.starting(group.off, (error) => console.error(`[${this.manifest.id}] subscribe ${event}:`, error));
332
- }
337
+ if (created && !group.unheard)
338
+ this.#open(event, group);
333
339
  else
334
340
  this.#tell(event, group);
335
341
  let listening = true;
@@ -353,10 +359,35 @@ export class Connection {
353
359
  },
354
360
  };
355
361
  }
362
+ /** Subscribe `group` to `event` at the player, as it stands now. */
363
+ #open(event, group) {
364
+ group.told = group.active > 0;
365
+ const whys = group.told ? allWhys(group) : undefined;
366
+ group.toldWhy = whys?.join("\n");
367
+ group.off = this.host.events.subscribe(event, (data) => {
368
+ // Split once: the state copies first, each on its own (one that
369
+ // throws stops neither the others nor the plugin's listeners),
370
+ // then the plugin's listeners in order.
371
+ const mirrors = [];
372
+ const others = [];
373
+ for (const each of group.listeners)
374
+ (group.mirrors.has(each) ? mirrors : others).push(each);
375
+ for (const each of mirrors)
376
+ try {
377
+ each(data);
378
+ }
379
+ catch (error) {
380
+ this.reportMirror(error);
381
+ }
382
+ for (const each of others)
383
+ each(data);
384
+ }, !group.told, whys);
385
+ this.starting(group.off, (error) => console.error(`[${this.manifest.id}] subscribe ${event}:`, error));
386
+ }
356
387
  /**
357
388
  * Count `event` as listened to while held: a watcher of a service's
358
389
  * state, a binding that reads it. Nothing when nothing subscribed to it
359
- * (no capability, or the service did not start).
390
+ * (no `plugin.player.subscribe()`, or the service did not start).
360
391
  */
361
392
  hold(event, why) {
362
393
  const group = this.#events.get(event);
@@ -383,15 +414,16 @@ export class Connection {
383
414
  }
384
415
  /**
385
416
  * A development warning, once per plugin, no behaviour change: the plugin
386
- * follows `event` (an `onChange`, a `bind` that reads the state) without the
387
- * `events` capability, so changes made elsewhere (the user, the player,
388
- * another plugin) never reach it; the state moves only after its own calls.
417
+ * follows `event` (a listener, an `onChange`, a `bind` that reads the
418
+ * state) before `plugin.player.subscribe()`, so changes made elsewhere
419
+ * (the user, the player, another plugin) never reach it; the state moves
420
+ * only after its own calls.
389
421
  */
390
422
  #warnUnheard(event) {
391
423
  if (this.#warnedEvents)
392
424
  return;
393
425
  this.#warnedEvents = true;
394
- console.warn(`[${this.manifest.id}] follows ${event} (onChange, or a bind() that reads the state) without the "events" capability: changes made elsewhere will not reach it, only its own calls refresh the state. Declare "events" in manifest.json.`);
426
+ console.warn(`[${this.manifest.id}] follows ${event} (a listener, onChange, or a bind() that reads the state) before plugin.player.subscribe(): changes made elsewhere will not reach it, only its own calls refresh the state. Call plugin.player.subscribe() first in activate().`);
395
427
  }
396
428
  /**
397
429
  * Tell the player when `group` changes between mirror and listener, and
@@ -430,7 +462,7 @@ export class Connection {
430
462
  }
431
463
  /**
432
464
  * One request by its method name, as given (`plugin.wire.request`): nothing
433
- * added, nothing checked here, the player checks the capability. Every other
465
+ * added, nothing checked here; the player checks the request. Every other
434
466
  * part of the SDK uses the typed clients (`host`); `scripts/check-layers.mjs`
435
467
  * keeps it so. It waits in the request queue like every other.
436
468
  */
@@ -1,4 +1,4 @@
1
- /** File drops (`plugin.drops`, `Panel.onDrop`; capability `ui.drop`). */
1
+ /** File drops (`plugin.drops`, `Panel.onDrop`). */
2
2
  import { failure } from "./errors.js";
3
3
  const modifiers = (raw) => Object.freeze({
4
4
  alt: raw?.alt === true,
@@ -2,7 +2,6 @@ import { Runtime } from "./runtime.js";
2
2
  import { stopReason } from "./connection.js";
3
3
  import { failure } from "./errors.js";
4
4
  import { ScopeImpl } from "./scope.js";
5
- import { playerOf } from "./player.js";
6
5
  import { windowOf } from "./window.js";
7
6
  import { timelineOf } from "./timeline.js";
8
7
  export async function createPlugin(transport, manifest, directory, dataDirectory,
@@ -11,20 +10,15 @@ app,
11
10
  /** `TestHost` sets `strictDeclarations`; the player's runner does not. */
12
11
  options) {
13
12
  const runtime = new Runtime(transport, manifest, directory, dataDirectory, app, options);
14
- // Player, window and timeline state are synchronous for plugins: read them
15
- // first. With `events` the copy of the player's state follows the events
16
- // from the start, subscribed before the read, as before 1.7.0: a read in
17
- // any later handler is current. Without `events` nothing can be
18
- // subscribed, and the copy starts on its first use. Window and timeline
19
- // subscribe on their first use.
20
- const player = playerOf(runtime);
21
- if (runtime.can("events"))
22
- void player.start(false);
23
- await Promise.all([
24
- player.init(),
25
- windowOf(runtime).init(),
26
- timelineOf(runtime).init(),
27
- ]);
13
+ // The window state is synchronous for plugins: read it first. The
14
+ // player's state and the timeline are the plugin's explicit calls, first
15
+ // in `activate`: `plugin.player.subscribe()` (event delivery, the copy
16
+ // of the player's state follows from then on) and
17
+ // `await plugin.player.read()` (state and timeline read once). Window and
18
+ // timeline subscribe on their first use. The timeline's copy exists from
19
+ // the start: it tells the connection how `read()` reads the lists.
20
+ timelineOf(runtime);
21
+ await windowOf(runtime).init();
28
22
  const root = new ScopeImpl(runtime);
29
23
  // A runaway of the update passes goes to the plugin's error handlers.
30
24
  runtime.passes.report = (error) => root.report(error);