Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/app/src/runtime/platform/platform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type PlatformBase = {
/** Observe native drag cancellation that does not reach the renderer event loop. */
onDragCancel?(callback: () => void): () => void

/** Observe the machine waking from sleep, when server event streams may be dead without knowing it (desktop only) */
onSystemResume?(callback: () => void): () => void

/** Open a native save file dialog and write content to the selected path (desktop only) */
saveFile?(opts: SaveFilePickerOptions, content: string): Promise<boolean>

Expand Down
6 changes: 6 additions & 0 deletions packages/app/src/runtime/server/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ function createServerSdkContextBase(server: ServerConnection.Any, scope: ServerS
transport.dispose()
})

// After sleep a stream can still read as connected while its socket is gone; replace it now rather than at its idle
// watchdog.
const stopResume = platform.onSystemResume?.(() => connection.internal.resync("System resumed from sleep"))

if (stopResume) onCleanup(stopResume)

return {
server,
scope,
Expand Down
18 changes: 16 additions & 2 deletions packages/desktop/src/main/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export * as Ipc from "./ipc"

import { app, BrowserWindow, MessageChannelMain } from "electron"
import { app, BrowserWindow, MessageChannelMain, powerMonitor } from "electron"
import { Effect, Layer } from "effect"
import { RpcServer } from "effect/rpc"
import { DesktopRpcs } from "../shared/ipc-rpc"
import { SystemResumed } from "../shared/ipc-rpc/events"
import { DragCancelEvent, IpcTransportPort } from "../shared/ipc-transport"
import { Plugins } from "./plugin"
import { DesktopFiles, openExternalURL } from "./files"
Expand All @@ -19,9 +20,10 @@ import { ApplicationLifecycle } from "./lifecycle"
import { createCliInstaller } from "./native/install-cli"
import { createMenu, disposeMenu, refreshMenu } from "./native/menu"
import { onNativeTranslations } from "./native/translations"
import { emitIpcEvent } from "./ipc-events"
import { DesktopCli } from "./service/desktop-cli"
import { setStorageSnapshotObserver } from "./storage/snapshot"
import { getLastFocusedWindow } from "./windows"
import { getLastFocusedWindow, getMainWindows } from "./windows"
import { isRendererUrl } from "./windows/scheme"
import { receivesPluginPort } from "./windows/security"

Expand Down Expand Up @@ -107,6 +109,18 @@ export const registerIpcHandlers = Effect.gen(function* () {
})
yield* Effect.addFinalizer(() => Effect.sync(() => app.off("browser-window-created", onWindowCreated)))

// A laptop that slept keeps reporting its event streams as connected until they time out, and Chromium sends no
// `online` event when the network never dropped from its point of view.
const resumed = () => {
const event = new SystemResumed()
getMainWindows().forEach((win) => emitIpcEvent(win.webContents, event))
}

if (process.platform === "win32") {
powerMonitor.on("resume", resumed)
yield* Effect.addFinalizer(() => Effect.sync(() => powerMonitor.off("resume", resumed)))
}

return {
installMenu: () => createMenu(menu),
}
Expand Down
1 change: 1 addition & 0 deletions packages/desktop/src/renderer/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type ElectronAPI = {
awaitInitialization(): Promise<ServerReadyData>
reconnectService(signal?: AbortSignal): Promise<ServerReadyData>
onSidecarChanged(cb: (data: ServerReadyData) => void): () => void
onSystemResumed(cb: () => void): () => void
readyDeepLinks(): void
acknowledgeDeepLinks(attempt: number): void
isFirstLaunchOnboardingPending(): Promise<boolean>
Expand Down
1 change: 1 addition & 0 deletions packages/desktop/src/renderer/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const api: ElectronAPI = {
awaitInitialization: () => invoke("AppAwaitInitialization"),
reconnectService: (signal) => cancellable("AppReconnectService", undefined, signal),
onSidecarChanged: (cb) => listen("SidecarChanged", (event) => cb({ url: event.url })),
onSystemResumed: (cb) => listen("SystemResumed", () => cb()),
readyDeepLinks: () => send("AppReadyDeepLinks"),
acknowledgeDeepLinks: (attempt) => send("AppAcknowledgeDeepLinks", { attempt }),
isFirstLaunchOnboardingPending: () => invoke("AppIsFirstLaunchOnboardingPending"),
Expand Down
1 change: 1 addition & 0 deletions packages/desktop/src/renderer/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function createDesktopPlatform(api: ElectronAPI, windowState: DesktopWind
recordFatalRendererError: (error) => api.recordFatalRendererError(error),
restart: async () => api.relaunch(),
notify: createDesktopNotify(api),
onSystemResume: (callback) => api.onSystemResumed(callback),
requestAttention: () => api.requestAttention(),
fetch: (input, init) => {
if (input instanceof Request) return fetch(input)
Expand Down
4 changes: 4 additions & 0 deletions packages/desktop/src/shared/ipc-rpc/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ export class SidecarChanged extends Schema.TaggedClass<SidecarChanged>()("Sideca
url: Schema.String,
}) {}

// The machine woke from sleep; every window checks its server event streams instead of waiting for them to time out.
export class SystemResumed extends Schema.TaggedClass<SystemResumed>()("SystemResumed", {}) {}

export const DesktopEvent = Schema.Union([
DeepLinksOpened,
SidecarChanged,
SystemResumed,
MenuCommandTriggered,
WindowFullscreenChanged,
WindowFocusChanged,
Expand Down
Loading