Skip to content

Commit 4a70c92

Browse files
committed
fix(react-native detected network)
1 parent 88e70c5 commit 4a70c92

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-relay-offline",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"keywords": [
55
"graphql",
66
"relay",

src/QueryRendererOffline.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class QueryRendererOffline extends React.Component<OfflineProps, State> {
2121
if (!this.state.rehydratate) {
2222

2323
const unsubscribe = this.props.environment.storeOffline.subscribe(() => {
24-
if (this.props.environment.storeOffline.getState().rehydrated) {
24+
if (this.props.environment.isRehydrated()) {
2525
this.setState({
2626
rehydratate: this.props.environment.isRehydrated()
2727
})

src/runtime/RelayModernEnvironment.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from 'relay-runtime/lib/RelayStoreTypes';
77

88
import { v4 as uuid } from "uuid";
9+
import { NORMALIZED_OFFLINE, NORMALIZED_REHYDRATED, NORMALIZED_DETECTED } from './redux/OfflineStore';
910

1011
const actions = {
1112
ENQUEUE: 'ENQUEUE_OFFLINE_MUTATION',
@@ -24,11 +25,11 @@ class RelayModernEnvironment extends Environment {
2425
}
2526

2627
public isRehydrated() {
27-
return this.storeOffline.getState().rehydrated;
28+
return this.storeOffline.getState()[NORMALIZED_REHYDRATED] && this.storeOffline.getState()[NORMALIZED_DETECTED];
2829
}
2930

3031
public isOnline() {
31-
return this.storeOffline.getState().offline.online;
32+
return this.storeOffline.getState()[NORMALIZED_OFFLINE].online;
3233
}
3334

3435
public getStoreOffline() {

src/runtime/redux/OfflineStore.ts

+18
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ export type OfflineCallback = (err: any, success: any) => void;
1010
export const NORMALIZED_CACHE_KEY = 'relay';
1111
export const NORMALIZED_ROOTS_KEY = 'relay-roots';
1212
export const NORMALIZED_OFFLINE = 'offline';
13+
export const NORMALIZED_DETECTED = 'detected';
14+
export const NORMALIZED_REHYDRATED = 'rehydrated';
1315
export const WRITE_CACHE_ACTION = 'RELAY_WRITE_CACHE';
1416
export const WRITE_ROOT_ACTION = 'RELAY_WRITE_ROOT';
17+
export const WRITE_DETECTED_NETWORK = 'RELAY_DETECTED_NETWORK';
18+
1519
const KEY_PREFIX_PERSIT = 'relayPersist:';
1620

1721
type AppState = {
@@ -65,6 +69,14 @@ class StoreOffline {
6569
return state;
6670
}
6771
},
72+
detected: (state = false, action) => {
73+
switch (action.type) {
74+
case WRITE_DETECTED_NETWORK:
75+
return true;
76+
default:
77+
return state;
78+
}
79+
},
6880
...storeOffline.cacheReducer(),
6981
...storeOffline.rootsReducer(),
7082
}),
@@ -73,6 +85,12 @@ class StoreOffline {
7385
applyMiddleware(thunk),
7486
offline({
7587
...defaultOfflineConfig,
88+
detectNetwork: callback => {
89+
detectNetwork(online => {
90+
callback(online);
91+
store.dispatch(writeThunk(WRITE_DETECTED_NETWORK, true) as any as Action);
92+
});
93+
},
7694
retry: storeOffline.getEffectDelay,
7795
persistCallback: () => {
7896
persistCallback();

0 commit comments

Comments
 (0)