moi-computer 0.12.1-next.1 → 0.12.1-next.2
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/client/AGENTS.md +3 -2
- package/client/api/app-config.ts +3 -9
- package/client/api/workspace-keys.ts +2 -1
- package/client/app/app-provider.tsx +25 -0
- package/client/app/routes/workspace-route.tsx +4 -0
- package/client/app/shell/sidebar-layout.tsx +19 -13
- package/client/dev/index.html +5 -5
- package/client/features/applets/api.ts +1 -1
- package/client/features/chat/connection/chat-frames.ts +2 -2
- package/client/features/chat/sessions/api.ts +1 -1
- package/client/features/chat/use-chat.ts +1 -1
- package/client/features/collab/host-state.ts +24 -7
- package/client/features/collab/local-user.ts +1 -1
- package/client/features/collab/proxy-user.ts +4 -9
- package/client/features/demo/demo-dialog.tsx +25 -79
- package/client/features/dev/workspace-setup-preview.tsx +43 -16
- package/client/features/harness/device-login-steps.tsx +2 -2
- package/client/features/home/api.ts +29 -9
- package/client/features/home/home-page.tsx +7 -4
- package/client/features/scratchpad/scratchpad.tsx +3 -6
- package/client/features/settings/api.ts +14 -8
- package/client/features/workspace/api.ts +12 -16
- package/client/features/workspace/setup/workspace-login-state.tsx +4 -4
- package/client/features/workspace/setup/workspace-setup.tsx +2 -1
- package/client/features/workspace/workspace-content.tsx +12 -52
- package/client/features/workspace/workspace-frame.tsx +76 -0
- package/client/features/workspace/workspace-layout-context.tsx +2 -0
- package/client/features/workspace/workspace-pending-screen.tsx +26 -0
- package/client/features/workspace/workspace-screen.tsx +3 -3
- package/client/index.html +28 -9
- package/client/index.tsx +7 -2
- package/client/lib/browser-tab-id.ts +4 -0
- package/client/main.tsx +9 -6
- package/client/runtime/app-context.ts +20 -0
- package/client/runtime/use-workspace-events.ts +29 -153
- package/client/runtime/workspace-events.ts +151 -0
- package/dist/chunk-00rh9df4.js +2 -0
- package/dist/chunk-4nh7y1hq.css +1 -0
- package/dist/{chunk-jvyvwwv3.js → chunk-am4agx6v.js} +1 -1
- package/dist/chunk-hbpdv7vd.js +1 -0
- package/dist/chunk-hmka9rt5.js +2 -0
- package/dist/chunk-mjjregkn.js +10 -0
- package/dist/chunk-vt6zybej.js +2 -0
- package/dist/chunk-wbkwf6cc.js +68 -0
- package/dist/chunk-xjyfyz64.js +1 -0
- package/dist/{chunk-q0gbdts4.js → chunk-y8nbwp21.js} +71 -71
- package/dist/chunk-ysfdcew7.js +1 -0
- package/dist/index.html +29 -10
- package/lib/ids.ts +2 -2
- package/package.json +1 -1
- package/server/api/index.ts +41 -22
- package/server/applets/thumbnails.ts +8 -5
- package/server/harness/codex/thread-list.ts +4 -0
- package/server/static.ts +4 -2
- package/server/vendor.ts +23 -30
- package/server/web.ts +3 -2
- package/server/workspaces/layout.ts +11 -6
- package/dist/chunk-08ndj2rg.js +0 -2
- package/dist/chunk-2x040tmg.js +0 -68
- package/dist/chunk-3smxq0ts.js +0 -1
- package/dist/chunk-68ch5hs8.js +0 -2
- package/dist/chunk-9vg3xa81.js +0 -2
- package/dist/chunk-c2p00p9j.css +0 -1
- package/dist/chunk-cs833y9j.js +0 -1
- package/dist/chunk-nwevqe86.js +0 -10
- package/dist/chunk-y5y3kj67.js +0 -1
- /package/client/vendor/react/{development → 19.2.4/development}/_impl/react-dom-client.js +0 -0
- /package/client/vendor/react/{development → 19.2.4/development}/_impl/react-dom.js +0 -0
- /package/client/vendor/react/{development → 19.2.4/development}/_impl/react-jsx-dev-runtime.js +0 -0
- /package/client/vendor/react/{development → 19.2.4/development}/_impl/react-jsx-runtime.js +0 -0
- /package/client/vendor/react/{development → 19.2.4/development}/_impl/react.js +0 -0
- /package/client/vendor/react/{development → 19.2.4/development}/react-dom-client.js +0 -0
- /package/client/vendor/react/{development → 19.2.4/development}/react-dom.js +0 -0
- /package/client/vendor/react/{development → 19.2.4/development}/react-jsx-dev-runtime.js +0 -0
- /package/client/vendor/react/{development → 19.2.4/development}/react-jsx-runtime.js +0 -0
- /package/client/vendor/react/{development → 19.2.4/development}/react.js +0 -0
- /package/client/vendor/react/{production → 19.2.4/production}/_impl/react-dom-client.js +0 -0
- /package/client/vendor/react/{production → 19.2.4/production}/_impl/react-dom.js +0 -0
- /package/client/vendor/react/{production → 19.2.4/production}/_impl/react-jsx-dev-runtime.js +0 -0
- /package/client/vendor/react/{production → 19.2.4/production}/_impl/react-jsx-runtime.js +0 -0
- /package/client/vendor/react/{production → 19.2.4/production}/_impl/react.js +0 -0
- /package/client/vendor/react/{production → 19.2.4/production}/react-dom-client.js +0 -0
- /package/client/vendor/react/{production → 19.2.4/production}/react-dom.js +0 -0
- /package/client/vendor/react/{production → 19.2.4/production}/react-jsx-dev-runtime.js +0 -0
- /package/client/vendor/react/{production → 19.2.4/production}/react-jsx-runtime.js +0 -0
- /package/client/vendor/react/{production → 19.2.4/production}/react.js +0 -0
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// Unique per browser tab (page load). Writes send it as `origin`; the server echoes it on the
|
|
2
|
+
// resulting broadcast so the writing tab can skip its own echo (it already shows
|
|
3
|
+
// the change optimistically, and a refetch could land an older value over it).
|
|
4
|
+
export const BROWSER_TAB_ID = Math.random().toString(36).slice(2)
|
package/client/main.tsx
CHANGED
|
@@ -6,6 +6,7 @@ import { Toaster } from '@/client/components/ui/toast'
|
|
|
6
6
|
import { TooltipProvider } from '@/client/components/ui/tooltip'
|
|
7
7
|
import { installAppletErrorHook } from '@/client/features/applets/applet-log'
|
|
8
8
|
import { initConnection } from '@/client/features/chat/connection/chat-connection'
|
|
9
|
+
import { AppProvider } from './app/app-provider'
|
|
9
10
|
import { AppRouter } from './app/app-router'
|
|
10
11
|
|
|
11
12
|
const queryClient = new QueryClient()
|
|
@@ -23,12 +24,14 @@ export function mount(el: HTMLElement) {
|
|
|
23
24
|
function Root() {
|
|
24
25
|
return (
|
|
25
26
|
<QueryClientProvider client={queryClient}>
|
|
26
|
-
<
|
|
27
|
-
<
|
|
28
|
-
<
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
<AppProvider>
|
|
28
|
+
<TooltipProvider>
|
|
29
|
+
<Router>
|
|
30
|
+
<AppRouter />
|
|
31
|
+
</Router>
|
|
32
|
+
<Toaster />
|
|
33
|
+
</TooltipProvider>
|
|
34
|
+
</AppProvider>
|
|
32
35
|
</QueryClientProvider>
|
|
33
36
|
)
|
|
34
37
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react'
|
|
2
|
+
|
|
3
|
+
import type { WorkspaceEventsClient } from './workspace-events'
|
|
4
|
+
|
|
5
|
+
// App-wide state that lives exactly as long as the app: created by
|
|
6
|
+
// `AppProvider` (app/app-provider.tsx) when the app mounts and shut down when
|
|
7
|
+
// it unmounts. Put things here that must survive route switches but not the
|
|
8
|
+
// app itself.
|
|
9
|
+
export type AppContextValue = {
|
|
10
|
+
// The page's workspace events socket.
|
|
11
|
+
events: WorkspaceEventsClient
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const AppContext = createContext<AppContextValue | undefined>(undefined)
|
|
15
|
+
|
|
16
|
+
// Undefined outside `AppProvider` (isolated sandboxes, component tests);
|
|
17
|
+
// callers treat that as "no app-wide services" and do nothing.
|
|
18
|
+
export function useAppContext(): AppContextValue | undefined {
|
|
19
|
+
return useContext(AppContext)
|
|
20
|
+
}
|
|
@@ -1,142 +1,19 @@
|
|
|
1
1
|
import { useEffect } from 'react'
|
|
2
|
-
import type { NavigationRequest } from '@/lib/navigation'
|
|
3
2
|
|
|
4
3
|
import { useLatestRef } from '@/client/lib/use-latest-ref'
|
|
5
|
-
import { wsUrl } from '@/client/lib/ws-url'
|
|
6
|
-
import type { AppSettings, HarnessAvailability, ViewInfo, WidgetInfo } from '@/lib/types'
|
|
7
4
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
| { type: 'widget-layout:updated'; widgets: WidgetInfo[] }
|
|
11
|
-
// `moi refresh` — cache-bust and re-fetch applets without a rebuild. `only`
|
|
12
|
-
// narrows to one kind; absent means both widgets and views.
|
|
13
|
-
| { type: 'applets:refresh'; only?: 'widgets' | 'views' }
|
|
14
|
-
| { type: 'view:updated'; name: string }
|
|
15
|
-
| { type: 'view-layout:updated'; views: ViewInfo[] }
|
|
16
|
-
| { type: 'view:deleted'; workspaceId: string; name: string }
|
|
17
|
-
| { type: 'views:changed'; workspaceId: string }
|
|
18
|
-
| { type: 'selected-session:updated'; workspaceId: string; sessionId: string | null }
|
|
19
|
-
| { type: 'theme:updated' }
|
|
20
|
-
| { type: 'workspace:updated' }
|
|
21
|
-
// The registry list changed (reorder, create) without touching any open
|
|
22
|
-
// workspace's layout — clients refetch just the sidebar list.
|
|
23
|
-
| { type: 'workspaces-list:updated' }
|
|
24
|
-
// A workspace's env changed outside the UI — refetch the env view.
|
|
25
|
-
| { type: 'env:updated'; workspaceId: string }
|
|
26
|
-
// Workspace readiness changed. Patch its cache without a refetch.
|
|
27
|
-
| {
|
|
28
|
-
type: 'harness:updated'
|
|
29
|
-
workspaceId: string
|
|
30
|
-
availability: HarnessAvailability
|
|
31
|
-
}
|
|
32
|
-
// The Scratchpad canvas for `workspaceId` was saved — open tabs reload from
|
|
33
|
-
// disk. `origin` is the tab that wrote it, so that tab can skip its own echo.
|
|
34
|
-
| { type: 'scratchpad:updated'; workspaceId: string; origin?: string }
|
|
35
|
-
// App settings changed (PATCH /api/settings from any client) — carries the
|
|
36
|
-
// new value so caches update without a refetch.
|
|
37
|
-
| { type: 'settings:updated'; settings: AppSettings }
|
|
38
|
-
| NavigationRequest
|
|
39
|
-
|
|
40
|
-
type WorkspaceEventHandler = (event: WorkspaceEvent) => void
|
|
41
|
-
|
|
42
|
-
const listeners = new Set<WorkspaceEventHandler>()
|
|
43
|
-
// Fired when the socket comes back after a drop — for the SPA that means the
|
|
44
|
-
// server restarted (it holds the connection for its whole lifetime), so
|
|
45
|
-
// restart-scoped state such as the startup config can refetch.
|
|
46
|
-
const reconnectListeners = new Set<() => void>()
|
|
47
|
-
let everConnected = false
|
|
48
|
-
let ws: WebSocket | null = null
|
|
49
|
-
let connecting = false
|
|
50
|
-
|
|
51
|
-
// Reconnect delay grows exponentially on repeated drops so a downed server
|
|
52
|
-
// doesn't get hammered with a request every 2s; resets once a connection
|
|
53
|
-
// actually opens.
|
|
54
|
-
const RECONNECT_BASE_DELAY_MS = 1000
|
|
55
|
-
const RECONNECT_MAX_DELAY_MS = 30_000
|
|
56
|
-
let reconnectAttempt = 0
|
|
57
|
-
|
|
58
|
-
function scheduleReconnect() {
|
|
59
|
-
const delay = Math.min(RECONNECT_BASE_DELAY_MS * 2 ** reconnectAttempt, RECONNECT_MAX_DELAY_MS)
|
|
60
|
-
reconnectAttempt++
|
|
61
|
-
setTimeout(ensureConnection, delay)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function ensureConnection() {
|
|
65
|
-
if (ws || connecting) return
|
|
66
|
-
connecting = true
|
|
5
|
+
import { useAppContext } from './app-context'
|
|
6
|
+
import type { WorkspaceEventHandler } from './workspace-events'
|
|
67
7
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
socket.onopen = () => {
|
|
71
|
-
ws = socket
|
|
72
|
-
connecting = false
|
|
73
|
-
reconnectAttempt = 0
|
|
74
|
-
sendNavigationPresence()
|
|
75
|
-
if (everConnected) for (const handler of reconnectListeners) handler()
|
|
76
|
-
everConnected = true
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
socket.onmessage = event => {
|
|
80
|
-
try {
|
|
81
|
-
const data = JSON.parse(event.data) as WorkspaceEvent
|
|
82
|
-
for (const handler of listeners) handler(data)
|
|
83
|
-
} catch {}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
socket.onclose = () => {
|
|
87
|
-
ws = null
|
|
88
|
-
connecting = false
|
|
89
|
-
// Reconnect with backoff if there are still listeners
|
|
90
|
-
if (listeners.size > 0) {
|
|
91
|
-
scheduleReconnect()
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
socket.onerror = () => {
|
|
96
|
-
socket.close()
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Subscribe to reconnects; returns the unsubscribe. Only fires while some
|
|
101
|
-
// component holds the events socket open via useWorkspaceEvent (the app shell
|
|
102
|
-
// always does).
|
|
103
|
-
export function onWorkspaceEventsReconnect(handler: () => void): () => void {
|
|
104
|
-
reconnectListeners.add(handler)
|
|
105
|
-
return () => {
|
|
106
|
-
reconnectListeners.delete(handler)
|
|
107
|
-
}
|
|
108
|
-
}
|
|
8
|
+
export type { WorkspaceEvent, WorkspaceEventHandler } from './workspace-events'
|
|
109
9
|
|
|
10
|
+
// Listen to the app's workspace events. Subscribing never opens or closes the
|
|
11
|
+
// socket — `AppProvider` owns that — so a route switch that briefly leaves
|
|
12
|
+
// nothing subscribed costs nothing. A no-op outside `AppProvider`.
|
|
110
13
|
export function useWorkspaceEvent(handler: WorkspaceEventHandler) {
|
|
14
|
+
const events = useAppContext()?.events
|
|
111
15
|
const handlerRef = useLatestRef(handler)
|
|
112
|
-
|
|
113
|
-
useEffect(() => {
|
|
114
|
-
const wrapped: WorkspaceEventHandler = e => handlerRef.current(e)
|
|
115
|
-
listeners.add(wrapped)
|
|
116
|
-
ensureConnection()
|
|
117
|
-
|
|
118
|
-
return () => {
|
|
119
|
-
listeners.delete(wrapped)
|
|
120
|
-
// Close WS if no more listeners
|
|
121
|
-
if (listeners.size === 0 && ws) {
|
|
122
|
-
ws.close()
|
|
123
|
-
ws = null
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}, [handlerRef])
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
let navigationWorkspace: string | null = null
|
|
130
|
-
|
|
131
|
-
function sendNavigationPresence() {
|
|
132
|
-
if (ws?.readyState !== WebSocket.OPEN) return
|
|
133
|
-
ws.send(
|
|
134
|
-
JSON.stringify({
|
|
135
|
-
type: 'navigation:presence',
|
|
136
|
-
workspaceId: navigationWorkspace,
|
|
137
|
-
focused: document.visibilityState === 'visible' && document.hasFocus()
|
|
138
|
-
})
|
|
139
|
-
)
|
|
16
|
+
useEffect(() => events?.subscribe(event => handlerRef.current(event)), [events, handlerRef])
|
|
140
17
|
}
|
|
141
18
|
|
|
142
19
|
// This hook owns presence for the displayed workspace, including reconnects.
|
|
@@ -145,36 +22,35 @@ export function useNavigationClient(
|
|
|
145
22
|
workspaceId: string,
|
|
146
23
|
navigate: (href: string) => void | Promise<void>
|
|
147
24
|
) {
|
|
25
|
+
const events = useAppContext()?.events
|
|
26
|
+
|
|
148
27
|
useEffect(() => {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
28
|
+
if (!events) return
|
|
29
|
+
events.setNavigationWorkspace(workspaceId)
|
|
30
|
+
const sendPresence = () => events.sendNavigationPresence()
|
|
31
|
+
window.addEventListener('focus', sendPresence)
|
|
32
|
+
document.addEventListener('visibilitychange', sendPresence)
|
|
153
33
|
return () => {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
document.removeEventListener('visibilitychange', sendNavigationPresence)
|
|
34
|
+
events.setNavigationWorkspace(undefined)
|
|
35
|
+
window.removeEventListener('focus', sendPresence)
|
|
36
|
+
document.removeEventListener('visibilitychange', sendPresence)
|
|
158
37
|
}
|
|
159
|
-
}, [workspaceId])
|
|
38
|
+
}, [events, workspaceId])
|
|
160
39
|
|
|
161
40
|
useWorkspaceEvent(async event => {
|
|
162
|
-
if (event.type !== 'navigation:request' || event.workspaceId !== workspaceId) return
|
|
163
|
-
const socket = ws
|
|
41
|
+
if (!events || event.type !== 'navigation:request' || event.workspaceId !== workspaceId) return
|
|
164
42
|
try {
|
|
165
|
-
if (navigationWorkspace !== workspaceId)
|
|
43
|
+
if (events.navigationWorkspace !== workspaceId) {
|
|
44
|
+
throw new Error('The browser switched workspaces.')
|
|
45
|
+
}
|
|
166
46
|
await navigate(event.href)
|
|
167
|
-
|
|
168
|
-
JSON.stringify({ type: 'navigation:result', requestId: event.requestId, ok: true })
|
|
169
|
-
)
|
|
47
|
+
events.send({ type: 'navigation:result', requestId: event.requestId, ok: true })
|
|
170
48
|
} catch (error) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
})
|
|
177
|
-
)
|
|
49
|
+
events.send({
|
|
50
|
+
type: 'navigation:result',
|
|
51
|
+
requestId: event.requestId,
|
|
52
|
+
error: error instanceof Error ? error.message : 'Navigation failed'
|
|
53
|
+
})
|
|
178
54
|
}
|
|
179
55
|
})
|
|
180
56
|
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { NavigationRequest } from '@/lib/navigation'
|
|
2
|
+
import type { AppSettings, HarnessAvailability, ViewInfo, WidgetInfo } from '@/lib/types'
|
|
3
|
+
|
|
4
|
+
export type WorkspaceEvent =
|
|
5
|
+
| { type: 'widget:updated'; name: string }
|
|
6
|
+
| { type: 'widget-layout:updated'; widgets: WidgetInfo[] }
|
|
7
|
+
// `moi refresh` — cache-bust and re-fetch applets without a rebuild. `only`
|
|
8
|
+
// narrows to one kind; absent means both widgets and views.
|
|
9
|
+
| { type: 'applets:refresh'; only?: 'widgets' | 'views' }
|
|
10
|
+
| { type: 'view:updated'; name: string }
|
|
11
|
+
| { type: 'view-layout:updated'; views: ViewInfo[] }
|
|
12
|
+
| { type: 'view:deleted'; workspaceId: string; name: string }
|
|
13
|
+
| { type: 'views:changed'; workspaceId: string }
|
|
14
|
+
| { type: 'selected-session:updated'; workspaceId: string; sessionId: string | null }
|
|
15
|
+
// `origin` on an event is the browser tab whose write caused it (absent for
|
|
16
|
+
// CLI and agent changes), so that tab can skip its own echo. See
|
|
17
|
+
// client/lib/browser-tab-id.ts.
|
|
18
|
+
| { type: 'theme:updated'; origin?: string }
|
|
19
|
+
| { type: 'workspace:updated'; origin?: string }
|
|
20
|
+
// The registry list changed (reorder, create) without touching any open
|
|
21
|
+
// workspace's layout — clients refetch just the sidebar list.
|
|
22
|
+
| { type: 'workspaces-list:updated'; origin?: string }
|
|
23
|
+
// A workspace's env changed outside the UI — refetch the env view.
|
|
24
|
+
| { type: 'env:updated'; workspaceId: string }
|
|
25
|
+
// Workspace readiness changed. Patch its cache without a refetch.
|
|
26
|
+
| {
|
|
27
|
+
type: 'harness:updated'
|
|
28
|
+
workspaceId: string
|
|
29
|
+
availability: HarnessAvailability
|
|
30
|
+
}
|
|
31
|
+
// The Scratchpad canvas for `workspaceId` was saved — open tabs reload from
|
|
32
|
+
// disk. `origin` is the tab that wrote it, so that tab can skip its own echo.
|
|
33
|
+
| { type: 'scratchpad:updated'; workspaceId: string; origin?: string }
|
|
34
|
+
// App settings changed (PATCH /api/settings from any client) — carries the
|
|
35
|
+
// new value so caches update without a refetch.
|
|
36
|
+
| { type: 'settings:updated'; settings: AppSettings }
|
|
37
|
+
| NavigationRequest
|
|
38
|
+
|
|
39
|
+
export type WorkspaceEventHandler = (event: WorkspaceEvent) => void
|
|
40
|
+
|
|
41
|
+
export type WorkspaceEventsClientOptions = {
|
|
42
|
+
// Delay before reopening after a drop. It doubles on repeated drops, up to
|
|
43
|
+
// the max, so a downed server isn't hammered; it resets once a socket opens.
|
|
44
|
+
reconnectBaseDelayMs?: number
|
|
45
|
+
reconnectMaxDelayMs?: number
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// The page's workspace events socket. `AppProvider` opens it when the app
|
|
49
|
+
// mounts and closes it when the app unmounts; subscribers come and go freely
|
|
50
|
+
// in between without touching the connection. A dropped socket reopens on its
|
|
51
|
+
// own. Framework-free, so the connection logic is testable without React.
|
|
52
|
+
export class WorkspaceEventsClient {
|
|
53
|
+
private readonly listeners = new Set<WorkspaceEventHandler>()
|
|
54
|
+
private readonly baseDelayMs: number
|
|
55
|
+
private readonly maxDelayMs: number
|
|
56
|
+
// The socket being opened or open; undefined while disconnected.
|
|
57
|
+
private socket: WebSocket | undefined
|
|
58
|
+
private closed = true
|
|
59
|
+
private reconnectAttempt = 0
|
|
60
|
+
private reconnectTimer: ReturnType<typeof setTimeout> | undefined
|
|
61
|
+
private shownWorkspace: string | undefined
|
|
62
|
+
|
|
63
|
+
constructor(
|
|
64
|
+
private readonly url: string,
|
|
65
|
+
options: WorkspaceEventsClientOptions = {}
|
|
66
|
+
) {
|
|
67
|
+
this.baseDelayMs = options.reconnectBaseDelayMs ?? 1000
|
|
68
|
+
this.maxDelayMs = options.reconnectMaxDelayMs ?? 30_000
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
connect() {
|
|
72
|
+
this.closed = false
|
|
73
|
+
this.open()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Shut the connection down for good; no reconnect follows.
|
|
77
|
+
close() {
|
|
78
|
+
this.closed = true
|
|
79
|
+
clearTimeout(this.reconnectTimer)
|
|
80
|
+
const socket = this.socket
|
|
81
|
+
this.socket = undefined
|
|
82
|
+
socket?.close()
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
subscribe(handler: WorkspaceEventHandler): () => void {
|
|
86
|
+
this.listeners.add(handler)
|
|
87
|
+
return () => {
|
|
88
|
+
this.listeners.delete(handler)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
send(message: object) {
|
|
93
|
+
if (this.socket?.readyState !== WebSocket.OPEN) return
|
|
94
|
+
this.socket.send(JSON.stringify(message))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// The workspace this tab shows, for the server's navigation relay. Sent now
|
|
98
|
+
// and again on every reconnect.
|
|
99
|
+
get navigationWorkspace(): string | undefined {
|
|
100
|
+
return this.shownWorkspace
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
setNavigationWorkspace(workspaceId: string | undefined) {
|
|
104
|
+
this.shownWorkspace = workspaceId
|
|
105
|
+
this.sendNavigationPresence()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
sendNavigationPresence() {
|
|
109
|
+
this.send({
|
|
110
|
+
type: 'navigation:presence',
|
|
111
|
+
// The wire format uses `null` for "no workspace".
|
|
112
|
+
workspaceId: this.shownWorkspace ?? null,
|
|
113
|
+
focused: document.visibilityState === 'visible' && document.hasFocus()
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private open() {
|
|
118
|
+
if (this.closed || this.socket) return
|
|
119
|
+
const socket = new WebSocket(this.url)
|
|
120
|
+
this.socket = socket
|
|
121
|
+
|
|
122
|
+
socket.onopen = () => {
|
|
123
|
+
this.reconnectAttempt = 0
|
|
124
|
+
this.sendNavigationPresence()
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
socket.onmessage = event => {
|
|
128
|
+
try {
|
|
129
|
+
const data = JSON.parse(event.data) as WorkspaceEvent
|
|
130
|
+
for (const handler of this.listeners) handler(data)
|
|
131
|
+
} catch {}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
socket.onclose = () => {
|
|
135
|
+
// A socket this client already let go of (closed or replaced).
|
|
136
|
+
if (this.socket !== socket) return
|
|
137
|
+
this.socket = undefined
|
|
138
|
+
if (!this.closed) this.scheduleReconnect()
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
socket.onerror = () => {
|
|
142
|
+
socket.close()
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private scheduleReconnect() {
|
|
147
|
+
const delay = Math.min(this.baseDelayMs * 2 ** this.reconnectAttempt, this.maxDelayMs)
|
|
148
|
+
this.reconnectAttempt++
|
|
149
|
+
this.reconnectTimer = setTimeout(() => this.open(), delay)
|
|
150
|
+
}
|
|
151
|
+
}
|