@@ -359,14 +359,25 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
359359 ) ;
360360
361361 // Uninterruptible so a committed retirement always prunes, even if its caller is cancelled.
362- const publish = ( update : ( state : DeviceServiceState ) => DeviceServiceState ) =>
363- SynchronizedRef . modifyEffect ( stateRef , ( { state } ) => {
364- const next = { ...update ( state ) , revision : state . revision + 1 } ;
365- const retired = state . sessions . some ( ( session ) => ! next . sessions . includes ( session ) ) ;
366- return PubSub . publish ( statePubSub , next ) . pipe (
367- Effect . as ( [ { next, retired } , { state : next } ] as const ) ,
368- ) ;
369- } ) . pipe (
362+ const publish = (
363+ update : ( state : DeviceServiceState ) => DeviceServiceState ,
364+ onlyWhileEnabled = false ,
365+ ) =>
366+ SynchronizedRef . modifyEffect ( stateRef , ( { state } ) =>
367+ Effect . gen ( function * ( ) {
368+ const enabled =
369+ ! onlyWhileEnabled ||
370+ ( yield * readDeviceSettings . pipe (
371+ Effect . map ( ( current ) => current . enabled ) ,
372+ Effect . orElseSucceed ( ( ) => false ) ,
373+ ) ) ;
374+ const next = { ...( enabled ? update ( state ) : state ) , revision : state . revision + 1 } ;
375+ const retired = state . sessions . some ( ( session ) => ! next . sessions . includes ( session ) ) ;
376+ return yield * PubSub . publish ( statePubSub , next ) . pipe (
377+ Effect . as ( [ { next, retired } , { state : next } ] as const ) ,
378+ ) ;
379+ } ) ,
380+ ) . pipe (
370381 Effect . tap ( ( { retired } ) => ( retired ? pruneLaunchers : Effect . void ) ) ,
371382 Effect . map ( ( { next } ) => next ) ,
372383 Effect . uninterruptible ,
@@ -391,24 +402,26 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
391402 Effect . suspend ( ( ) =>
392403 ! hosts . has ( hostId )
393404 ? SynchronizedRef . get ( stateRef ) . pipe ( Effect . map ( ( { state } ) => state ) )
394- : publish ( ( state ) =>
395- expectedHost && hosts . get ( hostId ) !== expectedHost
396- ? state
397- : {
398- ...state ,
399- ...( hostId === LOCAL_DEVICE_HOST_ID
400- ? { hostStatus : status . status , hostStatusDetail : status . detail }
401- : { } ) ,
402- hostStatuses : {
403- ...state . hostStatuses ,
404- [ hostId ] : {
405- ...( state . hostStatuses [ hostId ] ?. androidDiscoveryIncomplete
406- ? { androidDiscoveryIncomplete : true }
407- : { } ) ,
408- ...status ,
405+ : publish (
406+ ( state ) =>
407+ expectedHost && hosts . get ( hostId ) !== expectedHost
408+ ? state
409+ : {
410+ ...state ,
411+ ...( hostId === LOCAL_DEVICE_HOST_ID
412+ ? { hostStatus : status . status , hostStatusDetail : status . detail }
413+ : { } ) ,
414+ hostStatuses : {
415+ ...state . hostStatuses ,
416+ [ hostId ] : {
417+ ...( state . hostStatuses [ hostId ] ?. androidDiscoveryIncomplete
418+ ? { androidDiscoveryIncomplete : true }
419+ : { } ) ,
420+ ...status ,
421+ } ,
409422 } ,
410423 } ,
411- } ,
424+ true ,
412425 ) ,
413426 ) ;
414427
0 commit comments