@sanity/workbench 0.1.0-alpha.16 → 0.1.0-alpha.18

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.
@@ -0,0 +1,86 @@
1
+ import {
2
+ createInstance,
3
+ type FederationInstance,
4
+ } from "@sanity/federation/runtime";
5
+ import { log } from "@sanity/federation/runtime/plugins/log";
6
+ import { type ActorRefFrom, assign, setup } from "xstate";
7
+
8
+ import { logger } from "../core/log";
9
+ import { remoteLogic } from "./remote.machine";
10
+
11
+ type RemotesContext = {
12
+ instance: FederationInstance;
13
+ children: Map<string, ActorRefFrom<typeof remoteLogic>>;
14
+ };
15
+
16
+ type RemotesEvent =
17
+ | { type: "remote.load.request"; id: string; entry: string }
18
+ | { type: "remote.settled"; id: string; status: "loaded" | "error" };
19
+
20
+ /**
21
+ * Supervisor machine for federated application remotes.
22
+ *
23
+ * Owns a single {@link FederationInstance} (the `workbench-applications`
24
+ * instance) and spawns one {@link remoteLogic} child per requested
25
+ * remote. The supervisor is invoked at the root of the OS machine for
26
+ * inspector visibility (`[sanity-workbench:os:remotes]`) — boot does
27
+ * not gate on it.
28
+ *
29
+ * @internal
30
+ */
31
+ export const remotesLogic = setup({
32
+ types: {
33
+ context: {} as RemotesContext,
34
+ events: {} as RemotesEvent,
35
+ },
36
+ actors: {
37
+ remote: remoteLogic,
38
+ },
39
+ guards: {
40
+ remoteNotKnown: ({ context }, params: { id: string }) =>
41
+ !context.children.has(params.id),
42
+ },
43
+ actions: {
44
+ spawnRemote: assign({
45
+ children: ({ context, spawn }, params: { id: string; entry: string }) => {
46
+ const ref = spawn("remote", {
47
+ id: params.id,
48
+ systemId: `remotes.${params.id}`,
49
+ input: {
50
+ id: params.id,
51
+ entry: params.entry,
52
+ instance: context.instance,
53
+ },
54
+ });
55
+ return new Map(context.children).set(params.id, ref);
56
+ },
57
+ }),
58
+ },
59
+ }).createMachine({
60
+ id: "remotes",
61
+ context: () => ({
62
+ instance: createInstance({
63
+ name: "workbench-applications",
64
+ plugins: [log(logger.debug)],
65
+ }),
66
+ children: new Map(),
67
+ }),
68
+ on: {
69
+ "remote.load.request": {
70
+ guard: {
71
+ type: "remoteNotKnown",
72
+ params: ({ event }) => ({ id: event.id }),
73
+ },
74
+ actions: [
75
+ {
76
+ type: "spawnRemote",
77
+ params: ({ event }) => ({ id: event.id, entry: event.entry }),
78
+ },
79
+ ],
80
+ },
81
+ // Reserved for future supervision/retry. Forwarded by per-remote
82
+ // children on entry to `loaded` or `error`; currently a no-op so the
83
+ // event is part of the typed surface rather than a silent unknown.
84
+ "remote.settled": {},
85
+ },
86
+ });
@@ -3,6 +3,7 @@ import { raise, sendTo, setup, type ActorOptions } from "xstate";
3
3
 
4
4
  import { authLogic } from "./auth.machine";
5
5
  import { inspect } from "./inspect";
6
+ import { remotesLogic } from "./remotes.machine";
6
7
  import {
7
8
  type SystemPreferencesAdapter,
8
9
  systemPreferencesLogic,
@@ -57,12 +58,14 @@ export const os = setup({
57
58
  auth: "auth";
58
59
  telemetry: "telemetry";
59
60
  "system-preferences": "systemPreferences";
61
+ remotes: "remotes";
60
62
  },
61
63
  },
62
64
  actors: {
63
65
  auth: authLogic,
64
66
  telemetry: telemetryLogic,
65
67
  systemPreferences: systemPreferencesLogic,
68
+ remotes: remotesLogic,
66
69
  },
67
70
  guards: {
68
71
  hasTag: (_, params: { hasTag: boolean }) => params.hasTag,
@@ -143,6 +146,11 @@ export const os = setup({
143
146
  src: "systemPreferences",
144
147
  input: ({ context }) => ({ adapter: context.systemPreferencesAdapter }),
145
148
  },
149
+ {
150
+ id: "remotes",
151
+ systemId: "remotes",
152
+ src: "remotes",
153
+ },
146
154
  ],
147
155
  states: {
148
156
  booting: {