Skip to content

Commit 6c7fbe0

Browse files
committed
fix(#218): monitor network in background when sharing with iOS
1 parent cb091b7 commit 6c7fbe0

4 files changed

Lines changed: 49 additions & 9 deletions

File tree

machines/request.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ export const requestMachine =
129129
actions: 'switchProtocol',
130130
},
131131
],
132+
133+
OFFLINE: 'offline',
132134
},
133135
states: {
134136
inactive: {
@@ -173,10 +175,6 @@ export const requestMachine =
173175
},
174176
},
175177
},
176-
177-
on: {
178-
APP_ACTIVE: 'checkingNetwork',
179-
},
180178
},
181179

182180
bluetoothDenied: {
@@ -479,9 +477,15 @@ export const requestMachine =
479477

480478
offline: {
481479
on: {
482-
ONLINE: 'checkingBluetoothService',
480+
OFFLINE: {
481+
internal: true,
482+
},
483+
SCREEN_FOCUS: 'checkingNetwork',
483484
APP_ACTIVE: 'checkingNetwork',
484485
},
486+
after: {
487+
500: 'checkingNetwork',
488+
},
485489
},
486490
},
487491
},
@@ -775,6 +779,15 @@ export const requestMachine =
775779
);
776780

777781
return () => subscription.remove();
782+
} else {
783+
return NetInfo.addEventListener((state) => {
784+
callback({
785+
type:
786+
state.isConnected && state.isInternetReachable
787+
? 'ONLINE'
788+
: 'OFFLINE',
789+
});
790+
});
778791
}
779792
},
780793

machines/request.typegen.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export interface Typegen0 {
99
data: unknown;
1010
__tip: 'See the XState TS docs to learn how to strongly type this.';
1111
};
12+
'xstate.after(500)#request.offline': {
13+
type: 'xstate.after(500)#request.offline';
14+
};
1215
'xstate.after(CANCEL_TIMEOUT)#request.cancelling': {
1316
type: 'xstate.after(CANCEL_TIMEOUT)#request.cancelling';
1417
};
@@ -50,6 +53,7 @@ export interface Typegen0 {
5053
| 'CANCEL'
5154
| 'FACE_INVALID'
5255
| 'FACE_VALID'
56+
| 'OFFLINE'
5357
| 'REJECT'
5458
| 'SCREEN_BLUR'
5559
| 'SCREEN_FOCUS'
@@ -59,6 +63,7 @@ export interface Typegen0 {
5963
| ''
6064
| 'DISCONNECT'
6165
| 'DISMISS'
66+
| 'OFFLINE'
6267
| 'SCREEN_BLUR'
6368
| 'SCREEN_FOCUS'
6469
| 'SWITCH_PROTOCOL'
@@ -76,6 +81,7 @@ export interface Typegen0 {
7681
| 'xstate.after(CLEAR_DELAY)#request.clearingConnection';
7782
removeLoggers:
7883
| 'DISMISS'
84+
| 'OFFLINE'
7985
| 'SCREEN_BLUR'
8086
| 'xstate.after(CLEAR_DELAY)#request.clearingConnection'
8187
| 'xstate.init';
@@ -117,9 +123,13 @@ export interface Typegen0 {
117123
| 'SCREEN_FOCUS'
118124
| 'SWITCH_PROTOCOL'
119125
| 'xstate.after(CANCEL_TIMEOUT)#request.cancelling';
120-
checkNetwork: 'APP_ACTIVE' | 'SCREEN_FOCUS' | 'SWITCH_PROTOCOL';
126+
checkNetwork:
127+
| 'APP_ACTIVE'
128+
| 'SCREEN_FOCUS'
129+
| 'SWITCH_PROTOCOL'
130+
| 'xstate.after(500)#request.offline';
121131
exchangeDeviceInfo: 'RECEIVE_DEVICE_INFO';
122-
monitorConnection: 'xstate.init';
132+
monitorConnection: 'OFFLINE' | 'xstate.init';
123133
receiveVc: 'EXCHANGE_DONE' | 'RECEIVE_DEVICE_INFO';
124134
requestBluetooth: 'BLUETOOTH_DISABLED';
125135
sendDisconnect: 'CANCEL';

machines/scan.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ export const scanMachine =
117117
SCREEN_FOCUS: {
118118
target: '.checkingLocationService',
119119
},
120+
OFFLINE: {
121+
cond: 'isModeOnline',
122+
target: '.offline',
123+
},
120124
},
121125
states: {
122126
inactive: {
@@ -457,8 +461,8 @@ export const scanMachine =
457461
},
458462

459463
on: {
460-
OFFLINE: 'offline',
461464
ONLINE: 'preparingToConnect',
465+
OFFLINE: 'offline',
462466
},
463467
},
464468

@@ -662,6 +666,15 @@ export const scanMachine =
662666
);
663667

664668
return () => subscription.remove();
669+
} else {
670+
return NetInfo.addEventListener((state) => {
671+
callback({
672+
type:
673+
state.isConnected && state.isInternetReachable
674+
? 'ONLINE'
675+
: 'OFFLINE',
676+
});
677+
});
665678
}
666679
},
667680

machines/scan.typegen.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ export interface Typegen0 {
5252
clearCreatedVp:
5353
| 'DISCONNECT'
5454
| 'DISMISS'
55+
| 'OFFLINE'
5556
| 'SCREEN_BLUR'
5657
| 'SCREEN_FOCUS'
5758
| 'xstate.after(CANCEL_TIMEOUT)#scan.reviewing.cancelling'
5859
| 'xstate.stop';
5960
clearReason:
6061
| 'DISCONNECT'
6162
| 'DISMISS'
63+
| 'OFFLINE'
6264
| 'SCREEN_BLUR'
6365
| 'SCREEN_FOCUS'
6466
| 'xstate.after(CANCEL_TIMEOUT)#scan.reviewing.cancelling'
@@ -72,6 +74,7 @@ export interface Typegen0 {
7274
| 'DISCONNECT'
7375
| 'DISMISS'
7476
| 'LOCATION_ENABLED'
77+
| 'OFFLINE'
7578
| 'SCREEN_BLUR'
7679
| 'SCREEN_FOCUS'
7780
| 'xstate.after(CANCEL_TIMEOUT)#scan.reviewing.cancelling'
@@ -82,6 +85,7 @@ export interface Typegen0 {
8285
| 'ACCEPT_REQUEST'
8386
| 'CANCEL'
8487
| 'DISCONNECT'
88+
| 'OFFLINE'
8589
| 'SCREEN_BLUR'
8690
| 'SCREEN_FOCUS'
8791
| 'VERIFY_AND_ACCEPT_REQUEST'
@@ -126,7 +130,7 @@ export interface Typegen0 {
126130
| 'done.invoke.scan.reviewing.creatingVp:invocation[0]';
127131
};
128132
'eventsCausingGuards': {
129-
isModeOnline: 'CONNECTED';
133+
isModeOnline: 'CONNECTED' | 'OFFLINE';
130134
isQrOffline: 'SCAN';
131135
isQrOnline: 'SCAN';
132136
};

0 commit comments

Comments
 (0)