@@ -46,9 +46,11 @@ export class ServerSelfUpdate extends Context.Service<
4646 reportProgress ?: (
4747 stage : ServerSelfUpdateProgressStage ,
4848 ) => Effect . Effect < void , ServerSelfUpdateError > ,
49+ onHandoffAccepted ?: ( ) => Effect . Effect < void > ,
4950 ) => Effect . Effect < ServerSelfUpdateResult , ServerSelfUpdateError > ;
5051 readonly commitDesktopUpdate : (
5152 requestId : string ,
53+ onHandoffAccepted ?: ( ) => Effect . Effect < void > ,
5254 ) => Effect . Effect < never , ServerSelfUpdateError > ;
5355 }
5456> ( ) ( "t3/cloud/selfUpdate/ServerSelfUpdate" ) { }
@@ -67,10 +69,13 @@ export const withRunningThreadContinuation = Effect.fn(
6769 const clearOnError = < A > (
6870 effect : Effect . Effect < A , ServerSelfUpdateError > ,
6971 threadIds : ( ) => ReadonlyArray < ThreadId > ,
72+ shouldClear : ( ) => boolean ,
7073 ) : Effect . Effect < A , ServerSelfUpdateError > =>
7174 effect . pipe (
7275 Effect . catchCause ( ( cause ) =>
73- input . clear ( threadIds ( ) ) . pipe ( Effect . andThen ( Effect . failCause ( cause ) ) ) ,
76+ ( shouldClear ( ) ? input . clear ( threadIds ( ) ) : Effect . void ) . pipe (
77+ Effect . andThen ( Effect . failCause ( cause ) ) ,
78+ ) ,
7479 ) ,
7580 ) ;
7681
@@ -79,25 +84,32 @@ export const withRunningThreadContinuation = Effect.fn(
7984 reportProgress = ( ) => Effect . void ,
8085 ) => {
8186 let prepared = false ;
87+ let handoffAccepted = false ;
8288 let continuationThreadIds : ReadonlyArray < ThreadId > = [ ] ;
8389 return clearOnError (
8490 input . selfUpdate
85- . update ( request , ( stage ) =>
86- ( request . continueRunningThreads === true &&
87- input . mode !== "desktop" &&
88- stage === "installing" &&
89- ! prepared
90- ? input . prepare . pipe (
91- Effect . tap ( ( threadIds ) =>
92- Effect . sync ( ( ) => {
93- prepared = true ;
94- continuationThreadIds = threadIds ;
95- } ) ,
96- ) ,
97- Effect . asVoid ,
98- )
99- : Effect . void
100- ) . pipe ( Effect . andThen ( reportProgress ( stage ) ) ) ,
91+ . update (
92+ request ,
93+ ( stage ) =>
94+ ( request . continueRunningThreads === true &&
95+ input . mode !== "desktop" &&
96+ stage === "installing" &&
97+ ! prepared
98+ ? input . prepare . pipe (
99+ Effect . tap ( ( threadIds ) =>
100+ Effect . sync ( ( ) => {
101+ prepared = true ;
102+ continuationThreadIds = threadIds ;
103+ } ) ,
104+ ) ,
105+ Effect . asVoid ,
106+ )
107+ : Effect . void
108+ ) . pipe ( Effect . andThen ( reportProgress ( stage ) ) ) ,
109+ ( ) =>
110+ Effect . sync ( ( ) => {
111+ handoffAccepted = true ;
112+ } ) ,
101113 )
102114 . pipe (
103115 Effect . tap ( ( result ) => {
@@ -112,6 +124,7 @@ export const withRunningThreadContinuation = Effect.fn(
112124 } ) ,
113125 ) ,
114126 ( ) => continuationThreadIds ,
127+ ( ) => ! handoffAccepted ,
115128 ) ;
116129 } ;
117130
@@ -123,16 +136,22 @@ export const withRunningThreadContinuation = Effect.fn(
123136 HashSet . has ( tokens , requestId ) ,
124137 HashSet . remove ( tokens , requestId ) ,
125138 ] ) ;
139+ let handoffAccepted = false ;
126140 let continuationThreadIds : ReadonlyArray < ThreadId > = [ ] ;
127141 return yield * clearOnError (
128142 Effect . gen ( function * ( ) {
129143 continuationThreadIds = shouldContinue ? yield * input . prepare : [ ] ;
130- return yield * input . selfUpdate . commitDesktopUpdate ( requestId ) ;
144+ return yield * input . selfUpdate . commitDesktopUpdate ( requestId , ( ) =>
145+ Effect . sync ( ( ) => {
146+ handoffAccepted = true ;
147+ } ) ,
148+ ) ;
131149 } ) ,
132150 ( ) => continuationThreadIds ,
151+ ( ) => ! handoffAccepted ,
133152 ) . pipe (
134153 Effect . catchCause ( ( cause ) =>
135- ( shouldContinue
154+ ( shouldContinue && ! handoffAccepted
136155 ? Ref . update ( desktopContinuationTokens , HashSet . add ( requestId ) )
137156 : Effect . void
138157 ) . pipe ( Effect . andThen ( Effect . failCause ( cause ) ) ) ,
@@ -161,7 +180,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
161180
162181 const update : ServerSelfUpdate [ "Service" ] [ "update" ] = Effect . fn (
163182 "cloud.server_self_update.update" ,
164- ) ( function * ( input , reportProgress = ( ) => Effect . void ) {
183+ ) ( function * ( input , reportProgress = ( ) => Effect . void , onHandoffAccepted = ( ) => Effect . void ) {
165184 if ( capability === "desktop-managed" ) {
166185 // input.targetVersion is meaningless here: the desktop app's own
167186 // update feed decides what it downloads, and the result carries what
@@ -273,9 +292,8 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
273292 ) ;
274293
275294 yield * reportProgress ( "installing" ) ;
276- const updateId = yield * launcher
277- . requestUpdate ( { targetVersion, dbPath : serverConfig . dbPath } )
278- . pipe (
295+ const updateId = yield * Effect . uninterruptible (
296+ launcher . requestUpdate ( { targetVersion, dbPath : serverConfig . dbPath } ) . pipe (
279297 Effect . mapError ( ( error ) =>
280298 failWith (
281299 error . _tag === "ServiceLauncherRejectedError"
@@ -284,7 +302,9 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
284302 error ,
285303 ) ,
286304 ) ,
287- ) ;
305+ Effect . tap ( ( ) => onHandoffAccepted ( ) ) ,
306+ ) ,
307+ ) ;
288308
289309 yield * Effect . logInfo ( "Server update prepared; handing off to the service launcher." , {
290310 updateId,
@@ -297,7 +317,8 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
297317
298318 return ServerSelfUpdate . of ( {
299319 update,
300- commitDesktopUpdate : ( requestId ) => desktopAppUpdate . commit ( requestId ) ,
320+ commitDesktopUpdate : ( requestId , onHandoffAccepted ) =>
321+ desktopAppUpdate . commit ( requestId , onHandoffAccepted ) ,
301322 } ) ;
302323} ) ;
303324
0 commit comments