diff --git a/app/lib/appStateMiddleware.js b/app/lib/appStateMiddleware.js new file mode 100644 index 000000000..c0a30abc5 --- /dev/null +++ b/app/lib/appStateMiddleware.js @@ -0,0 +1,37 @@ +// https://github.com/bamlab/redux-enhancer-react-native-appstate +import { AppState } from 'react-native'; + +export const FOREGROUND = 'APP_STATE.FOREGROUND'; +export const BACKGROUND = 'APP_STATE.BACKGROUND'; +export const INACTIVE = 'APP_STATE.INACTIVE'; + +export default () => createStore => (...args) => { + const store = createStore(...args); + + let currentState = ''; + + const handleAppStateChange = (nextAppState) => { + if (nextAppState !== 'inactive') { + if (currentState !== nextAppState) { + let type; + if (nextAppState === 'active') { + type = FOREGROUND; + } else if (nextAppState === 'background') { + type = BACKGROUND; + } + if (type) { + store.dispatch({ + type + }); + } + } + currentState = nextAppState; + } + }; + + AppState.addEventListener('change', handleAppStateChange); + + // setTimeout to allow redux-saga to catch the initial state fired by redux-enhancer-react-native-appstate library + setTimeout(() => handleAppStateChange(AppState.currentState)); + return store; +}; diff --git a/app/lib/createStore.js b/app/lib/createStore.js index b4e1113a0..da52de600 100644 --- a/app/lib/createStore.js +++ b/app/lib/createStore.js @@ -1,9 +1,9 @@ import { createStore, applyMiddleware, compose } from 'redux'; import createSagaMiddleware from 'redux-saga'; -import applyAppStateListener from 'redux-enhancer-react-native-appstate'; import reducers from '../reducers'; import sagas from '../sagas'; +import applyAppStateMiddleware from './appStateMiddleware'; let sagaMiddleware; let enhancers; @@ -16,7 +16,7 @@ if (__DEV__) { }); enhancers = compose( - applyAppStateListener(), + applyAppStateMiddleware(), applyMiddleware(reduxImmutableStateInvariant), applyMiddleware(sagaMiddleware), Reactotron.createEnhancer() @@ -24,7 +24,7 @@ if (__DEV__) { } else { sagaMiddleware = createSagaMiddleware(); enhancers = compose( - applyAppStateListener(), + applyAppStateMiddleware(), applyMiddleware(sagaMiddleware) ); } diff --git a/app/reducers/app.js b/app/reducers/app.js index 7955bc7c5..eaf1afdf1 100644 --- a/app/reducers/app.js +++ b/app/reducers/app.js @@ -1,4 +1,4 @@ -import { FOREGROUND, BACKGROUND, INACTIVE } from 'redux-enhancer-react-native-appstate'; +import { FOREGROUND, BACKGROUND, INACTIVE } from '../lib/appStateMiddleware'; import { APP } from '../actions/actionsTypes'; const initialState = { diff --git a/app/sagas/rooms.js b/app/sagas/rooms.js index 1b5940d1a..8af8a290e 100644 --- a/app/sagas/rooms.js +++ b/app/sagas/rooms.js @@ -1,10 +1,9 @@ import { put, select, race, take, fork, cancel, delay } from 'redux-saga/effects'; -import { BACKGROUND, INACTIVE } from 'redux-enhancer-react-native-appstate'; import { Q } from '@nozbe/watermelondb'; - import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; + import * as types from '../actions/actionsTypes'; import { roomsSuccess, roomsFailure, roomsRefresh } from '../actions/rooms'; import database from '../lib/database'; @@ -13,6 +12,7 @@ import mergeSubscriptionsRooms from '../lib/methods/helpers/mergeSubscriptionsRo import RocketChat from '../lib/rocketchat'; import buildMessage from '../lib/methods/helpers/buildMessage'; import protectedFunction from '../lib/methods/helpers/protectedFunction'; +import { BACKGROUND, INACTIVE } from '../lib/appStateMiddleware'; const updateRooms = function* updateRooms({ server, newRoomsUpdatedAt }) { const serversDB = database.servers; diff --git a/app/sagas/state.js b/app/sagas/state.js index 3c3d94c9e..8c9687184 100644 --- a/app/sagas/state.js +++ b/app/sagas/state.js @@ -1,6 +1,6 @@ import { takeLatest, select, put } from 'redux-saga/effects'; -import { FOREGROUND, BACKGROUND } from 'redux-enhancer-react-native-appstate'; +import { FOREGROUND, BACKGROUND } from '../lib/appStateMiddleware'; import RocketChat from '../lib/rocketchat'; import { setBadgeCount } from '../notifications/push'; import log from '../utils/log'; diff --git a/package.json b/package.json index 3e8d4eb86..dc86441ba 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,6 @@ "react-redux": "7.1.3", "reactotron-react-native": "3.6.4", "redux": "4.0.4", - "redux-enhancer-react-native-appstate": "^0.3.1", "redux-immutable-state-invariant": "^2.1.0", "redux-saga": "1.1.3", "remove-markdown": "^0.3.0", diff --git a/yarn.lock b/yarn.lock index 03e8db7af..43a11e93b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10513,11 +10513,6 @@ redent@^1.0.0: indent-string "^2.1.0" strip-indent "^1.0.1" -redux-enhancer-react-native-appstate@^0.3.1: - version "0.3.1" - resolved "https://registry.yarnpkg.com/redux-enhancer-react-native-appstate/-/redux-enhancer-react-native-appstate-0.3.1.tgz#2fb7b99963cc63b4d7765e485c4f452cccbb34c4" - integrity sha1-L7e5mWPMY7TXdl5IXE9FLMy7NMQ= - redux-immutable-state-invariant@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/redux-immutable-state-invariant/-/redux-immutable-state-invariant-2.1.0.tgz#308fd3cc7415a0e7f11f51ec997b6379c7055ce1"