perf(desktop): create windows before the background service connects (#49763)

This commit is contained in:
Luke Parker
2026-09-19 10:33:39 +10:00
committed by GitHub
Unverified
parent ec8d95f1f2
commit 5b565e8923
2 changed files with 65 additions and 9 deletions
@@ -1,15 +1,57 @@
import { expect, test } from "bun:test"
import { Effect } from "effect"
import { Deferred, Effect, Fiber } from "effect"
import { BackgroundServiceState } from "./background-service-state"
test("new consumers receive the latest reconnected service", async () => {
const initial = { url: "http://127.0.0.1:4100", password: "first" }
const replacement = { url: "http://127.0.0.1:4200", password: "second" }
const service = await Effect.runPromise(
BackgroundServiceState.make({ initial: Effect.succeed(initial), reconnect: Effect.succeed(replacement) }),
Effect.scoped(
Effect.gen(function* () {
const state = yield* BackgroundServiceState.make({
initial: Effect.succeed(initial),
reconnect: Effect.succeed(replacement),
})
expect(yield* state.connection).toEqual(initial)
expect(yield* state.reconnect).toEqual(replacement)
return yield* state.connection
}),
),
)
expect(await Effect.runPromise(service.connection)).toEqual(initial)
expect(await Effect.runPromise(service.reconnect)).toEqual(replacement)
expect(await Effect.runPromise(service.connection)).toEqual(replacement)
expect(service).toEqual(replacement)
})
test("the state is ready before the initial connect finishes and consumers wait for it", async () => {
const initial = { url: "http://127.0.0.1:4100", password: "first" }
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const gate = yield* Deferred.make<void>()
const state = yield* BackgroundServiceState.make({
initial: Deferred.await(gate).pipe(Effect.as(initial)),
reconnect: Effect.succeed(initial),
})
const consumer = yield* Effect.forkChild(state.connection)
yield* Effect.sleep("10 millis")
expect(consumer.pollUnsafe()).toBeUndefined()
yield* Deferred.succeed(gate, undefined)
expect(yield* Fiber.join(consumer)).toEqual(initial)
}),
),
)
})
test("a failed initial connect fails consumers instead of hanging them", async () => {
const result = await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const state = yield* BackgroundServiceState.make({
initial: Effect.fail(new Error("service unavailable")),
reconnect: Effect.succeed({ url: "http://127.0.0.1:4100", password: "later" }),
})
return yield* state.connection.pipe(Effect.exit)
}),
),
)
expect(result._tag).toBe("Failure")
})
@@ -1,16 +1,30 @@
export * as BackgroundServiceState from "./background-service-state"
import { Effect, Exit, Ref } from "effect"
import { Deferred, Effect, Exit, Ref } from "effect"
import type { SidecarCredentials } from "./sidecar-credentials"
// The initial connect runs in the background: windows are created as soon as the main process
// is up, and the renderer loads its bundle while the CLI service is being resolved. Consumers
// await the first outcome through `connection`, so nothing observes the service before it exists.
export const make = Effect.fn("BackgroundServiceState.make")(function* (options: {
readonly initial: Effect.Effect<SidecarCredentials.Data, unknown>
readonly reconnect: Effect.Effect<SidecarCredentials.Data>
}) {
// Every Exit is an Effect, so the latest resolution replays directly for each consumer.
const current = yield* Ref.make<Exit.Exit<SidecarCredentials.Data, unknown>>(yield* options.initial.pipe(Effect.exit))
const current = yield* Ref.make<Exit.Exit<SidecarCredentials.Data, unknown> | undefined>(undefined)
const first = yield* Deferred.make<void>()
yield* options.initial.pipe(
Effect.exit,
Effect.flatMap((exit) => Ref.set(current, exit)),
Effect.ensuring(Deferred.succeed(first, undefined)),
Effect.forkScoped,
)
return {
connection: Ref.get(current).pipe(Effect.flatten, Effect.orDie),
connection: Deferred.await(first).pipe(
Effect.flatMap(() => Ref.get(current)),
Effect.flatMap((exit) => exit ?? Exit.die(new Error("background service connect did not resolve"))),
Effect.orDie,
),
reconnect: options.reconnect.pipe(Effect.tap((next) => Ref.set(current, Exit.succeed(next)))),
}
})