@@ -4,8 +4,10 @@ import { Provider } from 'react-redux'
4
4
import { applyMiddleware , compose , createStore , StoreEnhancer } from 'redux'
5
5
import { offline } from '@redux-offline/redux-offline'
6
6
import offlineConfig from '@redux-offline/redux-offline/lib/defaults'
7
+ import { NetworkCallback } from '@redux-offline/redux-offline/lib/types'
7
8
import logger from 'redux-logger'
8
9
import thunk from 'redux-thunk'
10
+ import isReachable from 'is-reachable'
9
11
10
12
import { AppContainer } from 'blapp/commerce/containers/App'
11
13
import reducer from 'blapp/commerce/reducers'
@@ -15,9 +17,34 @@ const persistCallback = () => {
15
17
store . subscribe ( ( ) => doRender ( ) )
16
18
}
17
19
20
+ const detectNetwork = ( callback : NetworkCallback ) => {
21
+ const handle = ( status : boolean ) => {
22
+ // Copy-ished from original redux-offline implementation
23
+ if ( window . requestAnimationFrame ) {
24
+ window . requestAnimationFrame ( ( ) => callback ( status ) )
25
+ } else {
26
+ setTimeout ( ( ) => callback ( status ) , 0 )
27
+ }
28
+ }
29
+
30
+ const checkNetwork = ( ) => {
31
+ if ( navigator . onLine === false ) {
32
+ handle ( false )
33
+ }
34
+
35
+ isReachable ( location . origin , { timeout : 5000 } ) . then ( handle )
36
+ }
37
+
38
+ window . addEventListener ( "online" , checkNetwork )
39
+ window . addEventListener ( "offline" , checkNetwork )
40
+ setInterval ( checkNetwork , 60000 )
41
+ checkNetwork ( )
42
+ }
43
+
18
44
const offlineConf = {
19
45
...offlineConfig ,
20
46
persistCallback,
47
+ detectNetwork,
21
48
}
22
49
23
50
const store = createStore (
0 commit comments