Skip to content

Commit 5e3f960

Browse files
committed
fix(desktop): interrupt startup when shutdown is requested
1 parent 4f229b1 commit 5e3f960

4 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎apps/desktop/src/app/DesktopApp.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ const scopedProgram = Effect.scoped(
373373
),
374374
);
375375

376-
yield* startup;
376+
yield* shutdown.runUntilRequested(startup);
377377
yield* shutdown.awaitRequest;
378378
}),
379379
);

‎apps/desktop/src/app/DesktopLifecycle.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ describe("DesktopLifecycle", () => {
183183
markComplete: Deferred.succeed(allowShutdown, undefined).pipe(Effect.asVoid),
184184
awaitComplete: Deferred.await(allowShutdown),
185185
isComplete: Deferred.isDone(allowShutdown),
186+
runUntilRequested: (effect) => Effect.raceFirst(effect, Deferred.await(shutdownRequested)),
186187
});
187188

188189
const layerEnvironment = Layer.succeed(DesktopEnvironment.DesktopEnvironment, {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { expect, it } from "@effect/vitest";
2+
import * as Deferred from "effect/Deferred";
3+
import * as Effect from "effect/Effect";
4+
import * as Exit from "effect/Exit";
5+
import * as Fiber from "effect/Fiber";
6+
import * as DesktopShutdown from "./DesktopShutdown.ts";
7+
8+
it.effect("quitting interrupts pending hydration and skips subsequent startup work", () =>
9+
Effect.gen(function* () {
10+
const shutdown = yield* DesktopShutdown.DesktopShutdown;
11+
const started = yield* Deferred.make<void>();
12+
const hydrationResult = yield* Deferred.make<void>();
13+
let released = false;
14+
let configured = false;
15+
const startup = Effect.scoped(
16+
Effect.gen(function* () {
17+
const hydration = yield* Effect.forkScoped(
18+
Effect.acquireUseRelease(
19+
Deferred.succeed(started, undefined),
20+
() => Deferred.await(hydrationResult),
21+
() =>
22+
Effect.sync(() => {
23+
released = true;
24+
}),
25+
),
26+
);
27+
yield* Fiber.join(hydration);
28+
configured = true;
29+
}),
30+
);
31+
const running = yield* shutdown.runUntilRequested(startup).pipe(Effect.forkChild);
32+
yield* Deferred.await(started);
33+
yield* shutdown.request;
34+
yield* Fiber.join(running);
35+
expect(released).toBe(true);
36+
expect(configured).toBe(false);
37+
expect(yield* shutdown.isComplete).toBe(false);
38+
yield* shutdown.markComplete;
39+
yield* shutdown.awaitComplete;
40+
expect(yield* shutdown.isComplete).toBe(true);
41+
}).pipe(Effect.scoped, Effect.provide(DesktopShutdown.layer)),
42+
);
43+
44+
it.effect("returns the completed startup result", () =>
45+
Effect.gen(function* () {
46+
const shutdown = yield* DesktopShutdown.DesktopShutdown;
47+
expect(yield* shutdown.runUntilRequested(Effect.succeed("ready"))).toBe("ready");
48+
expect(yield* shutdown.isComplete).toBe(false);
49+
}).pipe(Effect.scoped, Effect.provide(DesktopShutdown.layer)),
50+
);
51+
52+
it.effect("preserves startup failure before a quit request", () =>
53+
Effect.gen(function* () {
54+
const shutdown = yield* DesktopShutdown.DesktopShutdown;
55+
const result = yield* shutdown
56+
.runUntilRequested(Effect.fail("startup failed"))
57+
.pipe(Effect.exit);
58+
expect(result).toEqual(Exit.fail("startup failed"));
59+
}).pipe(Effect.provide(DesktopShutdown.layer)),
60+
);

‎apps/desktop/src/app/DesktopShutdown.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export class DesktopShutdown extends Context.Service<
1212
readonly markComplete: Effect.Effect<void>;
1313
readonly awaitComplete: Effect.Effect<void>;
1414
readonly isComplete: Effect.Effect<boolean>;
15+
readonly runUntilRequested: <A, E, R>(
16+
effect: Effect.Effect<A, E, R>,
17+
) => Effect.Effect<A | void, E, R>;
1518
}
1619
>()("@t3tools/desktop/app/DesktopShutdown") {}
1720

@@ -29,6 +32,7 @@ const make = Effect.gen(function* () {
2932
),
3033
awaitComplete: Deferred.await(completed),
3134
isComplete: Ref.get(completedRef),
35+
runUntilRequested: (effect) => Effect.raceFirst(effect, Deferred.await(requested)),
3236
});
3337
});
3438

0 commit comments

Comments
 (0)