@livespace/plugin-studio 0.0.0-stage → 0.2.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 (78) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +122 -2
  3. package/dist/auth.js +220 -0
  4. package/dist/cli.js +67 -0
  5. package/dist/keys.js +42 -0
  6. package/dist/manifest.js +16 -0
  7. package/dist/page/domain/address.js +79 -0
  8. package/dist/page/domain/connection.js +2 -0
  9. package/dist/page/domain/display.js +38 -0
  10. package/dist/page/domain/draft.js +80 -0
  11. package/dist/page/domain/failures-store.js +41 -0
  12. package/dist/page/domain/failures.js +75 -0
  13. package/dist/page/domain/layout-store.js +119 -0
  14. package/dist/page/domain/log-store.js +37 -0
  15. package/dist/page/domain/navigation-store.js +17 -0
  16. package/dist/page/domain/plugin-data.js +70 -0
  17. package/dist/page/domain/plugin-store.js +257 -0
  18. package/dist/page/domain/probe-store.js +100 -0
  19. package/dist/page/domain/profile-session.js +115 -0
  20. package/dist/page/domain/profile-store.js +51 -0
  21. package/dist/page/domain/samples.js +221 -0
  22. package/dist/page/domain/secrets-store.js +159 -0
  23. package/dist/page/domain/secrets.js +135 -0
  24. package/dist/page/domain/settings-data.js +64 -0
  25. package/dist/page/domain/settings-session.js +206 -0
  26. package/dist/page/domain/settings-store.js +62 -0
  27. package/dist/page/domain/slot-store.js +50 -0
  28. package/dist/page/domain/studio.js +162 -0
  29. package/dist/page/domain/surface-store.js +71 -0
  30. package/dist/page/domain/toasts-store.js +60 -0
  31. package/dist/page/domain/view-data.js +122 -0
  32. package/dist/page/domain/view-store.js +132 -0
  33. package/dist/page/host.css +907 -0
  34. package/dist/page/index.html +31 -0
  35. package/dist/page/main.js +43 -0
  36. package/dist/page/plugin-studio-logo.svg +4 -0
  37. package/dist/page/reload.svg +1 -0
  38. package/dist/page/transport/browser-storage.js +41 -0
  39. package/dist/page/transport/console-mirror.js +48 -0
  40. package/dist/page/transport/embed.js +142 -0
  41. package/dist/page/transport/plugin-keys.js +68 -0
  42. package/dist/page/transport/studio-api.js +74 -0
  43. package/dist/page/ui/app.js +61 -0
  44. package/dist/page/ui/boot.js +44 -0
  45. package/dist/page/ui/brand.js +3 -0
  46. package/dist/page/ui/context.js +11 -0
  47. package/dist/page/ui/json-field.js +19 -0
  48. package/dist/page/ui/json-tokens.js +40 -0
  49. package/dist/page/ui/json-view.js +11 -0
  50. package/dist/page/ui/memo.js +23 -0
  51. package/dist/page/ui/messages.js +34 -0
  52. package/dist/page/ui/page-switcher.js +11 -0
  53. package/dist/page/ui/plugin-frame.js +43 -0
  54. package/dist/page/ui/profile-page.js +13 -0
  55. package/dist/page/ui/resize-handle.js +63 -0
  56. package/dist/page/ui/root.js +9 -0
  57. package/dist/page/ui/section.js +19 -0
  58. package/dist/page/ui/settings/config-form.js +59 -0
  59. package/dist/page/ui/settings/secret-field.js +22 -0
  60. package/dist/page/ui/settings/settings-page.js +60 -0
  61. package/dist/page/ui/sidebar/custom-settings-section.js +10 -0
  62. package/dist/page/ui/sidebar/failures-section.js +27 -0
  63. package/dist/page/ui/sidebar/installation-section.js +30 -0
  64. package/dist/page/ui/sidebar/metadata-section.js +9 -0
  65. package/dist/page/ui/sidebar/plugin-section.js +18 -0
  66. package/dist/page/ui/sidebar/preferences-section.js +16 -0
  67. package/dist/page/ui/sidebar/record-section.js +17 -0
  68. package/dist/page/ui/sidebar/viewer-section.js +8 -0
  69. package/dist/page/ui/sidebar-group.js +4 -0
  70. package/dist/page/ui/splash.js +6 -0
  71. package/dist/page/ui/status.js +10 -0
  72. package/dist/page/ui/surface.js +23 -0
  73. package/dist/page/ui/theme.js +13 -0
  74. package/dist/page/ui/toasts.js +31 -0
  75. package/dist/page/ui/top-bar.js +37 -0
  76. package/dist/server.js +519 -0
  77. package/dist/start.js +108 -0
  78. package/package.json +55 -5
@@ -0,0 +1,257 @@
1
+ /**
2
+ * What the studio says the plugin is: its address, its client id, how accounts add it (which
3
+ * decides where its keys are), and its catalog manifest. The server holds it, for the tokens and
4
+ * the exchange. The address is also kept in this browser. Also what the plugin last declared about
5
+ * where it fits, its metadata, until it's embedded afresh.
6
+ */
7
+ import { batch, computed, signal, } from "@preact/signals";
8
+ import { Refused, } from "../transport/studio-api.js";
9
+ import { timers, Draft } from "./draft.js";
10
+ import { backendEnv, readManifest, recordChoicesFor, urlProblem, } from "./plugin-data.js";
11
+ export const NO_ADDRESS = { pluginUrl: null, cliUrl: null };
12
+ /** Until the server answers, a plugin with no address. */
13
+ const UNKNOWN = {
14
+ pluginUrl: null,
15
+ clientId: "",
16
+ distribution: "url",
17
+ manifest: null,
18
+ };
19
+ export class PluginStore {
20
+ deps;
21
+ #state = signal(UNKNOWN);
22
+ #addressProblem = signal(undefined);
23
+ #clientIdProblem = signal(undefined);
24
+ #manifestNote = signal(undefined);
25
+ #reloads = signal(0);
26
+ #metadata = signal(null);
27
+ /** The client id as typed, sent once typing pauses. */
28
+ clientIdDraft;
29
+ /** The catalog manifest as typed; one that reads as JSON is sent. */
30
+ manifestDraft;
31
+ pluginUrl;
32
+ /** The plugin's origin, which its messages must come from. */
33
+ pluginOrigin;
34
+ clientId;
35
+ distribution;
36
+ /** A catalog plugin's manifest, read; `undefined` for one added by URL, or no manifest. */
37
+ catalogManifest;
38
+ /** `plugin:<id>` from a catalog manifest, when it differs from the client id. */
39
+ suggestedClientId;
40
+ /**
41
+ * Where it can be installed, as how accounts add it decides: by URL, where the plugin declares
42
+ * (`supportedContextTypes`); from the catalog, where its manifest says (`allowedObjectTypes`).
43
+ * Every type and the apps menu when nothing says otherwise.
44
+ */
45
+ recordChoices;
46
+ /** What a backend's environment needs, to trust the dev account. */
47
+ env;
48
+ constructor(deps) {
49
+ this.deps = deps;
50
+ const scheduler = deps.scheduler ?? timers;
51
+ this.pluginUrl = computed(() => this.#state.value.pluginUrl);
52
+ this.pluginOrigin = computed(() => {
53
+ const url = this.pluginUrl.value;
54
+ return url === null ? null : new URL(url).origin;
55
+ });
56
+ this.clientId = computed(() => this.#state.value.clientId);
57
+ this.distribution = computed(() => this.#state.value.distribution);
58
+ this.catalogManifest = computed(() => {
59
+ const { distribution, manifest } = this.#state.value;
60
+ return distribution === "catalog" && manifest !== null
61
+ ? readManifest(JSON.stringify(manifest))
62
+ : undefined;
63
+ });
64
+ this.suggestedClientId = computed(() => {
65
+ const read = this.catalogManifest.value;
66
+ const suggested = read?.ok ? read.clientId : undefined;
67
+ return suggested === this.clientId.value ? undefined : suggested;
68
+ });
69
+ this.recordChoices = computed(() => {
70
+ if (this.distribution.value === "catalog") {
71
+ const read = this.catalogManifest.value;
72
+ return recordChoicesFor(read?.ok ? read.objectTypes : null);
73
+ }
74
+ return recordChoicesFor(this.#metadata.value?.supportedContextTypes ?? null);
75
+ });
76
+ this.env = computed(() => backendEnv(deps.studioOrigin, this.clientId.value));
77
+ this.clientIdDraft = new Draft("", (text) => ({ ok: true, value: text }), "", {
78
+ later: (run, ms) => scheduler.later(() => {
79
+ run();
80
+ void this.#sendClientId();
81
+ }, ms),
82
+ cancel: scheduler.cancel,
83
+ });
84
+ this.manifestDraft = new Draft("", (text) => {
85
+ if (text.trim() === "") {
86
+ return { ok: true, value: null };
87
+ }
88
+ const read = readManifest(text);
89
+ return read.ok ? { ok: true, value: read.manifest } : read;
90
+ }, null, {
91
+ later: (run, ms) => scheduler.later(() => {
92
+ run();
93
+ void this.#sendManifest();
94
+ }, ms),
95
+ cancel: scheduler.cancel,
96
+ });
97
+ }
98
+ /** Bumped for a fresh embed of the same address: the pages are keyed by it. */
99
+ get reloads() {
100
+ return this.#reloads;
101
+ }
102
+ /** Whether `text` is the address embedded now, so embedding it means a reload. */
103
+ isCurrent(text) {
104
+ return text.trim() === this.pluginUrl.value;
105
+ }
106
+ /** Embeds the plugin afresh at the same address: a new iframe, a new handshake. */
107
+ reload = () => {
108
+ batch(() => {
109
+ this.#addressProblem.value = undefined;
110
+ this.#metadata.value = null;
111
+ this.#reloads.value++;
112
+ });
113
+ };
114
+ /**
115
+ * What the plugin last declared about where it fits, already through `sanitizePluginMetadata`;
116
+ * `null` until it's heard, and again once the plugin is embedded afresh.
117
+ */
118
+ get metadata() {
119
+ return this.#metadata;
120
+ }
121
+ hearMetadata = (metadata) => {
122
+ this.#metadata.value = metadata;
123
+ };
124
+ get addressProblem() {
125
+ return this.#addressProblem;
126
+ }
127
+ get clientIdProblem() {
128
+ return this.#clientIdProblem;
129
+ }
130
+ /** What happened to the manifest last: loaded, cleared, or why it wasn't used. */
131
+ get manifestNote() {
132
+ return this.#manifestNote;
133
+ }
134
+ /**
135
+ * Reads the server's state, then the address saved here: it's embedded again unless the
136
+ * studio was started with a different one.
137
+ */
138
+ async load() {
139
+ this.#adopt(await this.deps.api.plugin());
140
+ const startedWith = this.pluginUrl.value;
141
+ const saved = this.deps.storage.read(NO_ADDRESS);
142
+ if (saved.cliUrl !== startedWith) {
143
+ this.deps.storage.write({
144
+ cliUrl: startedWith,
145
+ pluginUrl: startedWith ?? saved.pluginUrl,
146
+ });
147
+ }
148
+ const wanted = this.deps.storage.read(NO_ADDRESS).pluginUrl;
149
+ if (wanted !== null && wanted !== startedWith) {
150
+ if ((await this.setAddress(wanted)) !== undefined) {
151
+ this.#addressProblem.value = undefined;
152
+ this.#saveAddress(startedWith);
153
+ }
154
+ }
155
+ }
156
+ /** Embeds another page; why it can't be, otherwise. */
157
+ setAddress = async (text) => {
158
+ const problem = urlProblem(text, this.deps.studioOrigin) ??
159
+ (await this.#change({ pluginUrl: text.trim() }));
160
+ this.#addressProblem.value = problem;
161
+ if (problem === undefined) {
162
+ this.#saveAddress(this.pluginUrl.value);
163
+ }
164
+ return problem;
165
+ };
166
+ clearAddressProblem = () => {
167
+ this.#addressProblem.value = undefined;
168
+ };
169
+ adoptClientId = async (clientId) => {
170
+ this.clientIdDraft.set(clientId);
171
+ await this.#sendClientId();
172
+ };
173
+ setDistribution = async (distribution) => {
174
+ if (distribution === this.distribution.value) {
175
+ return;
176
+ }
177
+ await this.#change({ distribution });
178
+ this.deps.log("note", `Distribution: ${distribution === "url" ? "added by URL" : "catalog"}`);
179
+ };
180
+ /** Loads the project's `plugin-manifest.json` into the editor, and sends it. */
181
+ loadProjectManifest = async () => {
182
+ const manifest = await this.deps.api.projectManifest();
183
+ if (manifest === null) {
184
+ this.#manifestNote.value =
185
+ "The project has no plugin-manifest.json: `npx @livespace/plugin-cli manifest init` makes one.";
186
+ return;
187
+ }
188
+ this.manifestDraft.set(JSON.stringify(manifest, null, 2));
189
+ await this.#change({ manifest });
190
+ this.#manifestNote.value = "Loaded from plugin-manifest.json.";
191
+ };
192
+ async #sendClientId() {
193
+ const clientId = this.clientIdDraft.committed.value;
194
+ if (clientId === this.clientId.value) {
195
+ this.#clientIdProblem.value = undefined;
196
+ return;
197
+ }
198
+ const problem = await this.#change({ clientId });
199
+ this.#clientIdProblem.value = problem;
200
+ if (problem === undefined) {
201
+ this.deps.log("note", `Client id: ${clientId}`);
202
+ }
203
+ }
204
+ async #sendManifest() {
205
+ const parsed = this.manifestDraft.parsed.value;
206
+ if (!parsed.ok) {
207
+ this.#manifestNote.value = undefined;
208
+ return;
209
+ }
210
+ await this.#change({ manifest: parsed.value });
211
+ this.#manifestNote.value =
212
+ parsed.value === null ? "No manifest: no keys." : undefined;
213
+ }
214
+ /** Sends a change; the server's refusal comes back as text. */
215
+ async #change(changes) {
216
+ try {
217
+ this.#adopt(await this.deps.api.changePlugin(changes));
218
+ return undefined;
219
+ }
220
+ catch (error) {
221
+ if (error instanceof Refused) {
222
+ return error.message;
223
+ }
224
+ throw error;
225
+ }
226
+ }
227
+ #adopt(state) {
228
+ batch(() => {
229
+ const before = this.#state.value;
230
+ this.#state.value = state;
231
+ // Another plugin, or another build of it, declares afresh.
232
+ if (before.pluginUrl !== state.pluginUrl) {
233
+ this.#metadata.value = null;
234
+ }
235
+ // The editors follow the server, unless they're what changed it.
236
+ if (this.clientIdDraft.committed.value !== state.clientId) {
237
+ this.clientIdDraft.set(state.clientId);
238
+ }
239
+ if (before.manifest !== state.manifest && state.manifest !== null) {
240
+ const read = this.manifestDraft.parsed.value;
241
+ const same = read.ok &&
242
+ JSON.stringify(read.value) === JSON.stringify(state.manifest);
243
+ if (!same) {
244
+ this.manifestDraft.set(JSON.stringify(state.manifest, null, 2));
245
+ }
246
+ }
247
+ });
248
+ }
249
+ #saveAddress(pluginUrl) {
250
+ const saved = this.deps.storage.read(NO_ADDRESS);
251
+ this.deps.storage.write({ ...saved, pluginUrl });
252
+ }
253
+ dispose() {
254
+ this.clientIdDraft.dispose();
255
+ this.manifestDraft.dispose();
256
+ }
257
+ }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * The config probe: a hidden embed in config mode, as the plugins-2 settings screen makes one, which
3
+ * asks the plugin for its form and hears what it declares about itself. It runs whenever a plugin
4
+ * is embedded, on either page, so what the plugin declares is known from the start: the studio's
5
+ * stand-in for what Livespace learnt when the plugin was installed. One per embed of an address.
6
+ */
7
+ import { MSG_CONFIG_FORM, MSG_PLUGIN_METADATA, isSecretField, isValueField, sanitizeConfigForm, sanitizePluginMetadata, } from "@livespace/plugin-sdk";
8
+ import { batch, computed, effect, signal, } from "@preact/signals";
9
+ import { Embed } from "../transport/embed.js";
10
+ import { CONFIG_FORM_TIMEOUT_MS } from "./settings-data.js";
11
+ export class ProbeStore {
12
+ deps;
13
+ #form = signal(null);
14
+ #unusable = signal(false);
15
+ #timedOut = signal(false);
16
+ formStatus;
17
+ constructor(deps) {
18
+ this.deps = deps;
19
+ this.formStatus = computed(() => {
20
+ if (this.#unusable.value) {
21
+ return { kind: "unusable" };
22
+ }
23
+ const form = this.#form.value;
24
+ if (form === null) {
25
+ return this.#timedOut.value ? { kind: "timedOut" } : { kind: "asking" };
26
+ }
27
+ return form.fields.some((field) => isValueField(field) || isSecretField(field))
28
+ ? { kind: "ready" }
29
+ : { kind: "empty" };
30
+ });
31
+ }
32
+ /** The plugin's form, already through `sanitizeConfigForm`; `null` until it answers. */
33
+ get form() {
34
+ return this.#form;
35
+ }
36
+ /**
37
+ * A new embed of the probe: asked afresh, so what the last one said goes. Zero size and
38
+ * transparent in the page, never `display: none`, which may never load.
39
+ */
40
+ connect = (post) => {
41
+ batch(() => {
42
+ this.#form.value = null;
43
+ this.#unusable.value = false;
44
+ this.#timedOut.value = false;
45
+ });
46
+ const { log, view, scheduler } = this.deps;
47
+ const timer = scheduler.later(() => {
48
+ if (this.#form.peek() === null) {
49
+ this.#timedOut.value = true;
50
+ }
51
+ }, CONFIG_FORM_TIMEOUT_MS);
52
+ const embed = new Embed({
53
+ post,
54
+ state: () => ({
55
+ mode: "config",
56
+ preferences: view.preferences.value,
57
+ context: { user: null },
58
+ config: null,
59
+ fullscreen: false,
60
+ }),
61
+ log: (direction, type, payload) => log.append(direction, type, payload, "probe"),
62
+ });
63
+ // The preferences reach it as they change, as on the settings screen.
64
+ const stop = effect(() => {
65
+ view.preferences.value;
66
+ embed.update();
67
+ });
68
+ return {
69
+ receive: (data) => {
70
+ embed.receive(data);
71
+ const { type, payload } = (data ?? {});
72
+ this.#hear(type, payload);
73
+ },
74
+ loaded: () => embed.loaded(),
75
+ dispose: () => {
76
+ stop();
77
+ scheduler.cancel(timer);
78
+ embed.dispose();
79
+ },
80
+ };
81
+ };
82
+ #hear(type, payload) {
83
+ if (type === MSG_CONFIG_FORM) {
84
+ const form = sanitizeConfigForm(payload, { secrets: true });
85
+ batch(() => {
86
+ this.#unusable.value = form === null;
87
+ if (form !== null) {
88
+ this.#form.value = form;
89
+ }
90
+ });
91
+ }
92
+ else if (type === MSG_PLUGIN_METADATA) {
93
+ const { metadata, dropped } = sanitizePluginMetadata(payload);
94
+ if (dropped.length > 0) {
95
+ this.deps.log.append("note", `metadata: dropped ${dropped.join(", ")}, as Livespace would`, undefined, "probe");
96
+ }
97
+ this.deps.metadata.hearMetadata(metadata);
98
+ }
99
+ }
100
+ }
@@ -0,0 +1,115 @@
1
+ /**
2
+ * One embed on the profile page, as plugin-slot speaks to it: the handshake and its updates, and
3
+ * answers to the plugin's requests. API credentials are refused, as there's no real account behind
4
+ * the studio. Plugin tokens come from the dev account, unless a failure switch says otherwise.
5
+ */
6
+ import { MSG_API_CREDENTIALS, MSG_MODAL_SIZE, MSG_NOTIFY, MSG_REQUEST_API_CREDENTIALS, MSG_REQUEST_SESSION_TOKEN, MSG_SESSION_TOKEN, } from "@livespace/plugin-sdk";
7
+ import { effect } from "@preact/signals";
8
+ import { Embed } from "../transport/embed.js";
9
+ import { credentialsAnswer, tokenPlan } from "./failures.js";
10
+ /** A request's id, or undefined: plugin-slot ignores a request it can't answer by id. */
11
+ const requestIdOf = (payload) => {
12
+ const requestId = payload?.requestId;
13
+ return typeof requestId === "string" && requestId !== ""
14
+ ? requestId
15
+ : undefined;
16
+ };
17
+ export class ProfileSession {
18
+ deps;
19
+ #embed;
20
+ #stops = [];
21
+ #lastTokenExpired = false;
22
+ constructor(deps) {
23
+ this.deps = deps;
24
+ this.#embed = new Embed({
25
+ post: deps.post,
26
+ log: deps.log.append,
27
+ state: () => ({
28
+ mode: "view",
29
+ preferences: deps.view.preferences.value,
30
+ context: deps.view.context.value,
31
+ config: deps.settings.saved.value,
32
+ fullscreen: deps.surface.display.value === "fullscreen",
33
+ }),
34
+ });
35
+ // What the plugin is sent follows the sidebar and the saved settings.
36
+ this.#stops.push(effect(() => {
37
+ deps.view.preferences.value;
38
+ deps.view.context.value;
39
+ deps.settings.saved.value;
40
+ this.#embed.update();
41
+ }));
42
+ // Fullscreen's edges, as plugin-slot sends them.
43
+ let fullscreen = deps.surface.display.peek() === "fullscreen";
44
+ this.#stops.push(effect(() => {
45
+ const now = deps.surface.display.value === "fullscreen";
46
+ if (now !== fullscreen) {
47
+ fullscreen = now;
48
+ this.#embed.fullscreenChanged(now);
49
+ }
50
+ }));
51
+ }
52
+ /** A message from this embed's iframe, on its origin. */
53
+ receive = (data) => {
54
+ this.#embed.receive(data);
55
+ const { type, payload } = (data ?? {});
56
+ // Like plugin-slot, these are handled whenever they come, handshake or not.
57
+ if (type === MSG_MODAL_SIZE) {
58
+ this.deps.surface.modalSize(payload);
59
+ }
60
+ else if (type === MSG_NOTIFY) {
61
+ this.deps.toasts.notify(payload);
62
+ }
63
+ else if (type === MSG_REQUEST_SESSION_TOKEN) {
64
+ void this.#answerSessionToken(payload);
65
+ }
66
+ else if (type === MSG_REQUEST_API_CREDENTIALS) {
67
+ this.#answerCredentials(payload);
68
+ }
69
+ };
70
+ /** The iframe's `load`. */
71
+ loaded = () => {
72
+ this.#embed.loaded();
73
+ };
74
+ #answerCredentials(payload) {
75
+ const requestId = requestIdOf(payload);
76
+ if (requestId === undefined) {
77
+ return;
78
+ }
79
+ const answer = credentialsAnswer(requestId, this.deps.failures.failures.value.credentials);
80
+ if (answer === null) {
81
+ this.deps.log.append("note", "Not answered, as switched: the SDK times out");
82
+ return;
83
+ }
84
+ this.#embed.reply(MSG_API_CREDENTIALS, answer);
85
+ }
86
+ /** A plugin token for the viewer, from the dev account, as plugin-slot gets one from Livespace. */
87
+ async #answerSessionToken(payload) {
88
+ const requestId = requestIdOf(payload);
89
+ if (requestId === undefined) {
90
+ return;
91
+ }
92
+ const plan = tokenPlan(this.deps.failures.failures.value.token, this.#lastTokenExpired);
93
+ if (plan.kind === "none") {
94
+ this.deps.log.append("note", "Not answered, as switched: the SDK times out");
95
+ return;
96
+ }
97
+ if (plan.kind === "error") {
98
+ this.#embed.reply(MSG_SESSION_TOKEN, { requestId, error: plan.code });
99
+ return;
100
+ }
101
+ this.#lastTokenExpired = plan.expired;
102
+ const sub = this.deps.view.context.value.user?.id ?? "";
103
+ const answer = await this.deps.api.sessionToken(sub, plan.expired).then((token) => ({ requestId, ...token }), () => ({ requestId, error: "unauthenticated" }));
104
+ if (plan.expired) {
105
+ this.deps.log.append("note", "This token is already expired, as switched: expect a 401 and a retry");
106
+ }
107
+ this.#embed.reply(MSG_SESSION_TOKEN, answer);
108
+ }
109
+ dispose() {
110
+ for (const stop of this.#stops) {
111
+ stop();
112
+ }
113
+ this.#embed.dispose();
114
+ }
115
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The profile page, as plugin-slot shows a plugin on a record: one view-mode embed in Livespace's
3
+ * chrome, inline in its slot, its dialog and fullscreen. Each embed gets a `ProfileSession`, which
4
+ * answers its messages.
5
+ */
6
+ import { ProfileSession } from "./profile-session.js";
7
+ import { SurfaceStore } from "./surface-store.js";
8
+ export class ProfileStore {
9
+ deps;
10
+ surface;
11
+ /** Embeds open now: none while the settings page shows, or before the first. */
12
+ #live = 0;
13
+ /** Asked for with no embed to show it: the next one starts there. */
14
+ #pending = null;
15
+ constructor(deps) {
16
+ this.deps = deps;
17
+ this.surface = new SurfaceStore({ log: deps.log.append }, deps.slot.size);
18
+ }
19
+ /** A new embed of the plugin: its session answers it through `post`. */
20
+ connect = (post) => {
21
+ this.surface.reset();
22
+ this.deps.toasts.reset();
23
+ if (this.#pending !== null) {
24
+ this.surface.show(this.#pending);
25
+ this.#pending = null;
26
+ }
27
+ const session = new ProfileSession({
28
+ ...this.deps,
29
+ surface: this.surface,
30
+ post,
31
+ });
32
+ this.#live++;
33
+ return {
34
+ receive: session.receive,
35
+ loaded: session.loaded,
36
+ dispose: () => {
37
+ this.#live--;
38
+ session.dispose();
39
+ },
40
+ };
41
+ };
42
+ /** Shows the plugin inline, in its dialog or fullscreen: now, or as the next embed starts. */
43
+ show = (display) => {
44
+ if (this.#live > 0) {
45
+ this.surface.show(display);
46
+ }
47
+ else {
48
+ this.#pending = display;
49
+ }
50
+ };
51
+ }