Skip to content

Commit e8be38a

Browse files
committed
fix(desktop): preserve create error and retain partial tray on failed cleanup
- Retain partialTray in currentTrayRef when creation cleanup fails so it can be retried later. - Avoid Effect.orDie during partial tray cleanup to preserve original ElectronTrayCreateError. - Add regression test for concurrent creation and destruction failures.
1 parent 2bebe5a commit e8be38a

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

‎apps/desktop/src/electron/ElectronTray.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,45 @@ describe("ElectronTray", () => {
202202
assert.isTrue(instance.destroyed);
203203
}).pipe(Effect.provide(TestLayer)),
204204
);
205+
206+
it.effect(
207+
"retains partial tray in currentTrayRef and preserves create error when cleanup fails",
208+
() =>
209+
Effect.gen(function* () {
210+
const electronTray = yield* ElectronTray.ElectronTray;
211+
212+
let shouldThrow = true;
213+
buildFromTemplateMock.mockImplementationOnce(() => {
214+
const instance = trayInstances[0];
215+
if (instance) {
216+
const originalDestroy = instance.destroy;
217+
instance.destroy = () => {
218+
if (shouldThrow) {
219+
throw new Error("Cleanup destruction failure");
220+
}
221+
originalDestroy.call(instance);
222+
};
223+
}
224+
throw new Error("Menu template build failure");
225+
});
226+
227+
const result = yield* electronTray
228+
.create({
229+
iconPath: "C:/path/to/icon.ico",
230+
menuItems: [{ label: "Invalid Item" }],
231+
})
232+
.pipe(Effect.flip);
233+
234+
assert.instanceOf(result, ElectronTray.ElectronTrayCreateError);
235+
assert.equal(trayInstances.length, 1);
236+
const instance = trayInstances[0];
237+
assert.isDefined(instance);
238+
assert.isFalse(instance.destroyed);
239+
240+
// Subsequent destroy retry successfully cleans up the partial tray
241+
shouldThrow = false;
242+
yield* electronTray.destroy;
243+
assert.isTrue(instance.destroyed);
244+
}).pipe(Effect.provide(TestLayer)),
245+
);
205246
});

‎apps/desktop/src/electron/ElectronTray.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,14 @@ export const make = Effect.gen(function* () {
153153
}),
154154
}).pipe(
155155
Effect.tap((createdTray) => Ref.set(currentTrayRef, Option.some(createdTray))),
156-
Effect.tapError(() =>
157-
partialTray ? destroyTray(partialTray).pipe(Effect.orDie) : Effect.void,
158-
),
156+
Effect.tapError(() => {
157+
const target = partialTray;
158+
return target
159+
? destroyTray(target).pipe(
160+
Effect.catch(() => Ref.set(currentTrayRef, Option.some(target))),
161+
)
162+
: Effect.void;
163+
}),
159164
);
160165

161166
return tray;

0 commit comments

Comments
 (0)