@@ -83,29 +83,75 @@ const connectivityLayer = Connectivity.layer({
8383 ) ,
8484} ) ;
8585
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+
86125const wakeupsLayer = Wakeups . layer ( {
87- changes : Stream . merge (
88- Stream . callback < "application-active-probe" | "application-active-reconnect" > ( ( queue ) =>
89- Effect . acquireRelease (
90- Effect . sync ( ( ) => {
91- let backgroundedAtMs = AppState . currentState === "background" ? Date . now ( ) : null ;
92- return AppState . addEventListener ( "change" , ( state ) => {
93- if ( state === "background" ) {
94- backgroundedAtMs = Date . now ( ) ;
95- return ;
96- }
97- if ( state === "active" ) {
98- Queue . offerUnsafe ( queue , mobileApplicationActiveWakeup ( backgroundedAtMs , Date . now ( ) ) ) ;
99- backgroundedAtMs = null ;
100- }
101- } ) ;
102- } ) ,
103- ( subscription ) => Effect . sync ( ( ) => subscription . remove ( ) ) ,
104- ) . pipe ( Effect . asVoid ) ,
105- ) ,
106- managedRelayAccountChanges ( appAtomRegistry ) . pipe (
107- Stream . map ( ( ) => "credentials-changed" as const ) ,
108- ) ,
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" } ,
109155 ) ,
110156} ) ;
111157
0 commit comments