@@ -160,108 +160,113 @@ function quitFromSignal(
160160}
161161
162162/** @public Service construction is part of the canonical Effect module API. */
163- export const make = DesktopLifecycle . of ( {
164- relaunch : Effect . fn ( "desktop.lifecycle.relaunch" ) ( function * ( reason ) {
165- const electronApp = yield * ElectronApp . ElectronApp ;
166- const environment = yield * DesktopEnvironment . DesktopEnvironment ;
167- const state = yield * DesktopState . DesktopState ;
168- yield * logLifecycleInfo ( "desktop relaunch requested" , { reason } ) ;
169- yield * Effect . gen ( function * ( ) {
170- yield * Effect . yieldNow ;
171- yield * Ref . set ( state . quitting , true ) ;
172- yield * requestDesktopShutdownAndWait ( ) ;
173- if ( environment . isDevelopment ) {
174- yield * electronApp . exit ( 75 ) ;
175- return ;
176- }
177- yield * electronApp . relaunch ( {
178- execPath : process . execPath ,
179- args : process . argv . slice ( 1 ) ,
180- } ) ;
181- yield * electronApp . exit ( 0 ) ;
182- } ) . pipe (
183- Effect . catchCause ( ( cause ) => {
184- const error = new DesktopLifecycleRelaunchError ( { reason, cause } ) ;
185- return logLifecycleError ( error . message , { error } ) ;
186- } ) ,
187- Effect . forkDetach ,
188- Effect . asVoid ,
189- ) ;
190- } ) ,
191- register : Effect . gen ( function * ( ) {
192- const desktopWindow = yield * DesktopWindow . DesktopWindow ;
193- const electronWindow = yield * ElectronWindow . ElectronWindow ;
194- const electronApp = yield * ElectronApp . ElectronApp ;
195- const electronTheme = yield * ElectronTheme . ElectronTheme ;
196- const environment = yield * DesktopEnvironment . DesktopEnvironment ;
197- const context = yield * Effect . context < DesktopLifecycleRegistrationServices > ( ) ;
198- const runEffect = Effect . runPromiseWith ( context ) ;
199- let quitAllowed = false ;
200- let updaterQuitAllowed = false ;
201- yield * electronTheme . onUpdated ( ( ) => {
202- void runEffect (
203- desktopWindow . syncAppearance . pipe ( Effect . withSpan ( "desktop.lifecycle.themeUpdated" ) ) ,
163+ export const make = Effect . gen ( function * ( ) {
164+ const signalScope = yield * Scope . Scope ;
165+ return DesktopLifecycle . of ( {
166+ relaunch : Effect . fn ( "desktop.lifecycle.relaunch" ) ( function * ( reason ) {
167+ const electronApp = yield * ElectronApp . ElectronApp ;
168+ const environment = yield * DesktopEnvironment . DesktopEnvironment ;
169+ const state = yield * DesktopState . DesktopState ;
170+ yield * logLifecycleInfo ( "desktop relaunch requested" , { reason } ) ;
171+ yield * Effect . gen ( function * ( ) {
172+ yield * Effect . yieldNow ;
173+ yield * Ref . set ( state . quitting , true ) ;
174+ yield * requestDesktopShutdownAndWait ( ) ;
175+ if ( environment . isDevelopment ) {
176+ yield * electronApp . exit ( 75 ) ;
177+ return ;
178+ }
179+ yield * electronApp . relaunch ( {
180+ execPath : process . execPath ,
181+ args : process . argv . slice ( 1 ) ,
182+ } ) ;
183+ yield * electronApp . exit ( 0 ) ;
184+ } ) . pipe (
185+ Effect . catchCause ( ( cause ) => {
186+ const error = new DesktopLifecycleRelaunchError ( { reason, cause } ) ;
187+ return logLifecycleError ( error . message , { error } ) ;
188+ } ) ,
189+ Effect . forkDetach ,
190+ Effect . asVoid ,
204191 ) ;
205- } ) ;
206- yield * electronApp . onBeforeQuitForUpdate ( ( ) => {
207- // Electron's updater owns the remaining quit/install/relaunch sequence.
208- // Cancelling the following app "before-quit" event breaks that sequence,
209- // most visibly on macOS where the native updater performs the relaunch.
210- updaterQuitAllowed = true ;
211- // This event is synchronous and the updater's quit proceeds as soon as
212- // the listener returns, so a forked destroyAll would race the quit
213- // and windows could still be open when the process exits (visible on
214- // macOS). Destroy them inline.
215- Effect . runSyncWith ( context ) (
216- electronWindow . destroyAll . pipe (
217- Effect . andThen ( logLifecycleInfo ( "allowing updater-controlled quit" ) ) ,
218- Effect . catchCause ( ( cause ) =>
219- logLifecycleError ( "failed to destroy windows before updater quit" , { cause } ) ,
192+ } ) ,
193+ register : Effect . gen ( function * ( ) {
194+ const desktopWindow = yield * DesktopWindow . DesktopWindow ;
195+ const electronWindow = yield * ElectronWindow . ElectronWindow ;
196+ const electronApp = yield * ElectronApp . ElectronApp ;
197+ const electronTheme = yield * ElectronTheme . ElectronTheme ;
198+ const environment = yield * DesktopEnvironment . DesktopEnvironment ;
199+ const context = yield * Effect . context < DesktopLifecycleRegistrationServices > ( ) ;
200+ const runEffect = Effect . runPromiseWith ( context ) ;
201+ let quitAllowed = false ;
202+ let updaterQuitAllowed = false ;
203+ yield * electronTheme . onUpdated ( ( ) => {
204+ void runEffect (
205+ desktopWindow . syncAppearance . pipe ( Effect . withSpan ( "desktop.lifecycle.themeUpdated" ) ) ,
206+ ) ;
207+ } ) ;
208+ yield * electronApp . onBeforeQuitForUpdate ( ( ) => {
209+ // Electron's updater owns the remaining quit/install/relaunch sequence.
210+ // Cancelling the following app "before-quit" event breaks that sequence,
211+ // most visibly on macOS where the native updater performs the relaunch.
212+ updaterQuitAllowed = true ;
213+ // This event is synchronous and the updater's quit proceeds as soon as
214+ // the listener returns, so a forked destroyAll would race the quit
215+ // and windows could still be open when the process exits (visible on
216+ // macOS). Destroy them inline.
217+ Effect . runSyncWith ( context ) (
218+ electronWindow . destroyAll . pipe (
219+ Effect . andThen ( logLifecycleInfo ( "allowing updater-controlled quit" ) ) ,
220+ Effect . catchCause ( ( cause ) =>
221+ logLifecycleError ( "failed to destroy windows before updater quit" , { cause } ) ,
222+ ) ,
223+ Effect . withSpan ( "desktop.lifecycle.beforeQuitForUpdate" ) ,
220224 ) ,
221- Effect . withSpan ( "desktop.lifecycle.beforeQuitForUpdate" ) ,
222- ) ,
223- ) ;
224- } ) ;
225- yield * electronApp . on ( "before-quit" , ( event : Electron . Event ) => {
226- handleBeforeQuit (
227- event ,
228- runEffect ,
229- ( ) => quitAllowed || updaterQuitAllowed ,
230- ( ) => {
231- quitAllowed = true ;
232- } ,
233- ) ;
234- } ) ;
235- yield * electronApp . on ( "activate" , ( ) => {
236- void runEffect (
237- Effect . gen ( function * ( ) {
238- const state = yield * DesktopState . DesktopState ;
239- if ( yield * Ref . get ( state . quitting ) ) return ;
240- yield * desktopWindow . activate ;
241- } ) . pipe ( Effect . withSpan ( "desktop.lifecycle.activate" ) ) ,
242- ) ;
243- } ) ;
244- yield * electronApp . on ( "window-all-closed" , ( ) => {
245- void runEffect (
246- Effect . gen ( function * ( ) {
247- const app = yield * ElectronApp . ElectronApp ;
248- const state = yield * DesktopState . DesktopState ;
249- if ( environment . platform !== "darwin" && ! ( yield * Ref . get ( state . quitting ) ) ) {
250- yield * app . quit ;
251- }
252- } ) . pipe ( Effect . withSpan ( "desktop.lifecycle.windowAllClosed" ) ) ,
253- ) ;
254- } ) ;
255-
256- if ( environment . platform !== "win32" ) {
257- yield * addScopedListener ( process , "SIGINT" , ( ) => {
258- quitFromSignal ( "SIGINT" , runEffect ) ;
225+ ) ;
226+ } ) ;
227+ yield * electronApp . on ( "before-quit" , ( event : Electron . Event ) => {
228+ handleBeforeQuit (
229+ event ,
230+ runEffect ,
231+ ( ) => quitAllowed || updaterQuitAllowed ,
232+ ( ) => {
233+ quitAllowed = true ;
234+ } ,
235+ ) ;
236+ } ) ;
237+ yield * electronApp . on ( "activate" , ( ) => {
238+ void runEffect (
239+ Effect . gen ( function * ( ) {
240+ const state = yield * DesktopState . DesktopState ;
241+ if ( yield * Ref . get ( state . quitting ) ) return ;
242+ yield * desktopWindow . activate ;
243+ } ) . pipe ( Effect . withSpan ( "desktop.lifecycle.activate" ) ) ,
244+ ) ;
259245 } ) ;
260- yield * addScopedListener ( process , "SIGTERM" , ( ) => {
261- quitFromSignal ( "SIGTERM" , runEffect ) ;
246+ yield * electronApp . on ( "window-all-closed" , ( ) => {
247+ void runEffect (
248+ Effect . gen ( function * ( ) {
249+ const app = yield * ElectronApp . ElectronApp ;
250+ const state = yield * DesktopState . DesktopState ;
251+ if ( environment . platform !== "darwin" && ! ( yield * Ref . get ( state . quitting ) ) ) {
252+ yield * app . quit ;
253+ }
254+ } ) . pipe ( Effect . withSpan ( "desktop.lifecycle.windowAllClosed" ) ) ,
255+ ) ;
262256 } ) ;
263- }
264- } ) . pipe ( Effect . withSpan ( "desktop.lifecycle.register" ) ) ,
257+
258+ if ( environment . platform !== "win32" ) {
259+ // The layer outlives program finalizers, so repeated signals stay owned
260+ // while backends stop. Electron listeners still follow the program scope.
261+ yield * addScopedListener ( process , "SIGINT" , ( ) => {
262+ quitFromSignal ( "SIGINT" , runEffect ) ;
263+ } ) . pipe ( Scope . provide ( signalScope ) ) ;
264+ yield * addScopedListener ( process , "SIGTERM" , ( ) => {
265+ quitFromSignal ( "SIGTERM" , runEffect ) ;
266+ } ) . pipe ( Scope . provide ( signalScope ) ) ;
267+ }
268+ } ) . pipe ( Effect . withSpan ( "desktop.lifecycle.register" ) ) ,
269+ } ) ;
265270} ) ;
266271
267- export const layer = Layer . succeed ( DesktopLifecycle , make ) ;
272+ export const layer = Layer . effect ( DesktopLifecycle , make ) ;
0 commit comments