@@ -27,6 +27,7 @@ import * as MobileStorage from "../persistence/mobile-storage";
2727import { appAtomRegistry } from "../state/atom-registry" ;
2828import { clearThreadOutboxEnvironment } from "../state/thread-outbox-removal" ;
2929import { clearComposerDraftsEnvironment } from "../state/use-composer-drafts" ;
30+ import { clearThreadComposerErrorsForEnvironment } from "../state/thread-composer-error" ;
3031import { mobileApplicationActiveWakeup } from "./app-state-wakeups" ;
3132import { connectionStorageLayer } from "./storage" ;
3233
@@ -82,29 +83,75 @@ const connectivityLayer = Connectivity.layer({
8283 ) ,
8384} ) ;
8485
86+ /**
87+ * Wakes connections when the device moves between networks while staying
88+ * online, such as Wi-Fi to cellular. Connectivity only reports online or
89+ * offline, so leaving home on cellular would otherwise go unnoticed until the
90+ * LAN socket times out.
91+ */
92+ const networkPathChanges = Stream . callback < "network-changed" > ( ( queue ) =>
93+ Effect . acquireRelease (
94+ Effect . sync ( ( ) => {
95+ let active = true ;
96+ let previous : Network . NetworkStateType | undefined ;
97+ const record = ( state : Network . NetworkState ) => {
98+ const type = state . isConnected === true ? state . type : undefined ;
99+ if ( previous !== undefined && type !== undefined && type !== previous ) {
100+ Queue . offerUnsafe ( queue , "network-changed" ) ;
101+ }
102+ previous = type ?? previous ;
103+ } ;
104+ // The listener reports changes only, so seed the current type; without
105+ // it the first Wi-Fi to cellular move would go unnoticed.
106+ void Network . getNetworkStateAsync ( )
107+ . then ( ( state ) => {
108+ if ( active && previous === undefined && state . isConnected === true ) {
109+ previous = state . type ;
110+ }
111+ } )
112+ . catch ( ( ) => undefined ) ;
113+ const subscription = Network . addNetworkStateListener ( record ) ;
114+ return {
115+ remove : ( ) => {
116+ active = false ;
117+ subscription . remove ( ) ;
118+ } ,
119+ } ;
120+ } ) ,
121+ ( subscription ) => Effect . sync ( ( ) => subscription . remove ( ) ) ,
122+ ) . pipe ( Effect . asVoid ) ,
123+ ) ;
124+
85125const wakeupsLayer = Wakeups . layer ( {
86- changes : Stream . merge (
87- Stream . callback < "application-active-probe" | "application-active-reconnect" > ( ( queue ) =>
88- Effect . acquireRelease (
89- Effect . sync ( ( ) => {
90- let backgroundedAtMs = AppState . currentState === "background" ? Date . now ( ) : null ;
91- return AppState . addEventListener ( "change" , ( state ) => {
92- if ( state === "background" ) {
93- backgroundedAtMs = Date . now ( ) ;
94- return ;
95- }
96- if ( state === "active" ) {
97- Queue . offerUnsafe ( queue , mobileApplicationActiveWakeup ( backgroundedAtMs , Date . now ( ) ) ) ;
98- backgroundedAtMs = null ;
99- }
100- } ) ;
101- } ) ,
102- ( subscription ) => Effect . sync ( ( ) => subscription . remove ( ) ) ,
103- ) . pipe ( Effect . asVoid ) ,
104- ) ,
105- managedRelayAccountChanges ( appAtomRegistry ) . pipe (
106- Stream . map ( ( ) => "credentials-changed" as const ) ,
107- ) ,
126+ changes : Stream . mergeAll (
127+ [
128+ Stream . callback < "application-active-probe" | "application-active-reconnect" > ( ( queue ) =>
129+ Effect . acquireRelease (
130+ Effect . sync ( ( ) => {
131+ let backgroundedAtMs = AppState . currentState === "background" ? Date . now ( ) : null ;
132+ return AppState . addEventListener ( "change" , ( state ) => {
133+ if ( state === "background" ) {
134+ backgroundedAtMs = Date . now ( ) ;
135+ return ;
136+ }
137+ if ( state === "active" ) {
138+ Queue . offerUnsafe (
139+ queue ,
140+ mobileApplicationActiveWakeup ( backgroundedAtMs , Date . now ( ) ) ,
141+ ) ;
142+ backgroundedAtMs = null ;
143+ }
144+ } ) ;
145+ } ) ,
146+ ( subscription ) => Effect . sync ( ( ) => subscription . remove ( ) ) ,
147+ ) . pipe ( Effect . asVoid ) ,
148+ ) ,
149+ managedRelayAccountChanges ( appAtomRegistry ) . pipe (
150+ Stream . map ( ( ) => "credentials-changed" as const ) ,
151+ ) ,
152+ networkPathChanges ,
153+ ] ,
154+ { concurrency : "unbounded" } ,
108155 ) ,
109156} ) ;
110157
@@ -218,6 +265,7 @@ const environmentOwnedDataCleanupLayer = Layer.succeed(
218265 [
219266 Effect . promise ( ( ) => clearThreadOutboxEnvironment ( environmentId ) ) ,
220267 Effect . promise ( ( ) => clearComposerDraftsEnvironment ( environmentId ) ) ,
268+ Effect . sync ( ( ) => clearThreadComposerErrorsForEnvironment ( environmentId ) ) ,
221269 ] ,
222270 { concurrency : "unbounded" , discard : true } ,
223271 ) . pipe (
0 commit comments