@@ -45,7 +45,7 @@ import {
4545const AUTO_UPDATE_STARTUP_DELAY = "15 seconds" ;
4646const AUTO_UPDATE_POLL_INTERVAL = "4 minutes" ;
4747
48- type UpdateAction = "check" | "download" | "install" ;
48+ type UpdateAction = "check" | "download" | "install" | "channel" ;
4949
5050const AppUpdateYmlConfig = Schema . Record ( Schema . String , Schema . String ) ;
5151type AppUpdateYmlConfig = typeof AppUpdateYmlConfig . Type ;
@@ -70,7 +70,7 @@ const currentIsoTimestamp = DateTime.now.pipe(Effect.map(DateTime.formatIso));
7070export class DesktopUpdateActionInProgressError extends Schema . TaggedErrorClass < DesktopUpdateActionInProgressError > ( ) (
7171 "DesktopUpdateActionInProgressError" ,
7272 {
73- action : Schema . Literals ( [ "check" , "download" , "install" ] ) ,
73+ action : Schema . Literals ( [ "check" , "download" , "install" , "channel" ] ) ,
7474 requestedChannel : DesktopUpdateChannelSchema ,
7575 } ,
7676) {
@@ -118,7 +118,7 @@ export class DesktopUpdateEventHandlingError extends Schema.TaggedErrorClass<Des
118118export class DesktopUpdaterReportedError extends Schema . TaggedErrorClass < DesktopUpdaterReportedError > ( ) (
119119 "DesktopUpdaterReportedError" ,
120120 {
121- operation : Schema . Literals ( [ "check" , "download" , "install" , "background" ] ) ,
121+ operation : Schema . Literals ( [ "check" , "download" , "install" , "channel" , " background"] ) ,
122122 cause : Schema . Defect ( ) ,
123123 } ,
124124) {
@@ -320,6 +320,12 @@ export const make = Effect.gen(function* () {
320320 Option . isSome ( activeAction ) ? [ false , activeAction ] : [ true , Option . some ( action ) ] ,
321321 ) ;
322322
323+ const tryStartChannelChange = Ref . modify ( activeUpdateActionRef , ( activeAction ) =>
324+ Option . isSome ( activeAction )
325+ ? [ activeAction , activeAction ]
326+ : [ Option . none < UpdateAction > ( ) , Option . some < UpdateAction > ( "channel" ) ] ,
327+ ) ;
328+
323329 const finishUpdateAction = ( action : UpdateAction ) : Effect . Effect < void > =>
324330 Ref . update ( activeUpdateActionRef , ( activeAction ) =>
325331 Option . isSome ( activeAction ) && activeAction . value === action ? Option . none ( ) : activeAction ,
@@ -344,7 +350,10 @@ export const make = Effect.gen(function* () {
344350
345351 const shouldEnableAutoUpdates = resolveDisabledReason . pipe ( Effect . map ( Option . isNone ) ) ;
346352
347- const checkForUpdates = Effect . fn ( "desktop.updates.checkForUpdates" ) ( function * ( reason : string ) {
353+ const checkForUpdates = Effect . fn ( "desktop.updates.checkForUpdates" ) ( function * (
354+ reason : string ,
355+ actionReservation : "acquire" | "held" = "acquire" ,
356+ ) {
348357 yield * Effect . annotateCurrentSpan ( { reason } ) ;
349358 if ( yield * Ref . get ( desktopState . quitting ) ) return false ;
350359 if ( ! ( yield * Ref . get ( updaterConfiguredRef ) ) ) return false ;
@@ -358,9 +367,9 @@ export const make = Effect.gen(function* () {
358367 return false ;
359368 }
360369
361- if ( ! ( yield * tryStartUpdateAction ( "check" ) ) ) return false ;
370+ if ( actionReservation === "acquire" && ! ( yield * tryStartUpdateAction ( "check" ) ) ) return false ;
362371
363- return yield * Effect . gen ( function * ( ) {
372+ const check = Effect . gen ( function * ( ) {
364373 const checkedAt = yield * currentIsoTimestamp ;
365374 yield * setState ( reduceDesktopUpdateStateOnCheckStart ( state , checkedAt ) ) ;
366375 yield * logUpdaterInfo ( "checking for updates" , { reason } ) ;
@@ -383,7 +392,11 @@ export const make = Effect.gen(function* () {
383392 } ) ,
384393 } ) ,
385394 ) ;
386- } ) . pipe ( Effect . ensuring ( finishUpdateAction ( "check" ) ) ) ;
395+ } ) ;
396+
397+ return yield * actionReservation === "held"
398+ ? check
399+ : check . pipe ( Effect . ensuring ( finishUpdateAction ( "check" ) ) ) ;
387400 } ) ;
388401
389402 const downloadAvailableUpdate = Effect . gen ( function * ( ) {
@@ -778,41 +791,43 @@ export const make = Effect.gen(function* () {
778791 nextChannel : DesktopUpdateChannel ,
779792 ) {
780793 yield * Effect . annotateCurrentSpan ( { channel : nextChannel } ) ;
781- const activeAction = yield * activeUpdateAction ;
794+ const activeAction = yield * tryStartChannelChange ;
782795 if ( Option . isSome ( activeAction ) ) {
783796 return yield * new DesktopUpdateActionInProgressError ( {
784797 action : activeAction . value ,
785798 requestedChannel : nextChannel ,
786799 } ) ;
787800 }
788801
789- const state = yield * Ref . get ( updateStateRef ) ;
790- if ( nextChannel === state . channel ) {
791- return state ;
792- }
802+ return yield * Effect . gen ( function * ( ) {
803+ const state = yield * Ref . get ( updateStateRef ) ;
804+ if ( nextChannel === state . channel ) {
805+ return state ;
806+ }
793807
794- yield * desktopSettings
795- . setUpdateChannel ( nextChannel )
796- . pipe (
797- Effect . mapError (
798- ( cause ) => new DesktopUpdateChannelPersistenceError ( { channel : nextChannel , cause } ) ,
799- ) ,
800- ) ;
808+ yield * desktopSettings
809+ . setUpdateChannel ( nextChannel )
810+ . pipe (
811+ Effect . mapError (
812+ ( cause ) => new DesktopUpdateChannelPersistenceError ( { channel : nextChannel , cause } ) ,
813+ ) ,
814+ ) ;
801815
802- const enabled = yield * shouldEnableAutoUpdates ;
803- yield * setState ( createBaseUpdateState ( nextChannel , enabled , environment ) ) ;
816+ const enabled = yield * shouldEnableAutoUpdates ;
817+ yield * setState ( createBaseUpdateState ( nextChannel , enabled , environment ) ) ;
804818
805- if ( ! enabled || ! ( yield * Ref . get ( updaterConfiguredRef ) ) ) {
806- return yield * Ref . get ( updateStateRef ) ;
807- }
819+ if ( ! enabled || ! ( yield * Ref . get ( updaterConfiguredRef ) ) ) {
820+ return yield * Ref . get ( updateStateRef ) ;
821+ }
808822
809- yield * applyAutoUpdaterChannel ( nextChannel ) ;
810- const allowDowngrade = yield * electronUpdater . allowDowngrade ;
811- yield * electronUpdater . setAllowDowngrade ( true ) ;
812- yield * checkForUpdates ( "channel-change" ) . pipe (
813- Effect . ensuring ( electronUpdater . setAllowDowngrade ( allowDowngrade ) . pipe ( Effect . ignore ) ) ,
814- ) ;
815- return yield * Ref . get ( updateStateRef ) ;
823+ yield * applyAutoUpdaterChannel ( nextChannel ) ;
824+ const allowDowngrade = yield * electronUpdater . allowDowngrade ;
825+ yield * electronUpdater . setAllowDowngrade ( true ) ;
826+ yield * checkForUpdates ( "channel-change" , "held" ) . pipe (
827+ Effect . ensuring ( electronUpdater . setAllowDowngrade ( allowDowngrade ) . pipe ( Effect . ignore ) ) ,
828+ ) ;
829+ return yield * Ref . get ( updateStateRef ) ;
830+ } ) . pipe ( Effect . ensuring ( finishUpdateAction ( "channel" ) ) ) ;
816831 } ) ,
817832 check : Effect . fn ( "desktop.updates.check" ) ( function * ( reason : string ) {
818833 yield * Effect . annotateCurrentSpan ( { reason } ) ;
0 commit comments