From 112cd6e5410a94ebf11323698957b4a58a17012b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20G=C3=B3mez=20Acu=C3=B1a?= <4982414+rgommezz@users.noreply.github.com> Date: Sun, 3 Feb 2019 22:08:54 +0000 Subject: [PATCH] Feat: new queueReleaseThrottle optional param for createNetworkMiddleware --- README.md | 9 +- example/components/ConnectionToggler.js | 2 +- example/components/OfflineQueue.js | 4 +- example/package.json | 2 +- example/redux/createStore.js | 6 +- example/screens/ReduxScreen.js | 6 +- example/screens/SagasScreen.js | 4 +- example/yarn.lock | 8 +- package.json | 4 +- src/components/ReduxNetworkProvider.js | 11 +- src/redux/createNetworkMiddleware.js | 128 +++++++++++++++++------- src/redux/sagas.js | 8 +- src/utils/wait.js | 2 + test/ReduxNetworkProvider.test.js | 21 +--- test/createNetworkMiddleware.test.js | 86 +++++++++++++--- test/sagas.test.js | 19 +--- 16 files changed, 202 insertions(+), 118 deletions(-) create mode 100644 src/utils/wait.js diff --git a/README.md b/README.md index a98cd403..5b0924bc 100644 --- a/README.md +++ b/README.md @@ -269,7 +269,8 @@ createNetworkMiddleware(config: MiddlewareConfig): ReduxMiddleware type MiddlewareConfig = { regexActionType?: RegExp = /FETCH.*REQUEST/, - actionTypes?: Array = [] + actionTypes?: Array = [], + queueReleaseThrottle?: number = 50, } ``` @@ -281,6 +282,8 @@ By default it's configured to intercept actions for fetching data following the `actionTypes`: array with additional action types to intercept that don't fulfil the RegExp criteria. For instance, it's useful for actions that carry along refreshing data, such as `REFRESH_LIST`. +`queueReleaseThrottle`: waiting time in ms between dispatches when flushing the offline queue. Useful to reduce the server pressure when coming back online. Defaults to 50ms. + ##### Thunks Config For `redux-thunk` library, the async flow is wrapped inside functions that will be lazily evaluated when dispatched, so our store is able to dispatch functions as well. Therefore, the configuration differs: @@ -316,7 +319,9 @@ import { createNetworkMiddleware } from 'react-native-offline'; import createSagaMiddleware from 'redux-saga'; const sagaMiddleware = createSagaMiddleware(); -const networkMiddleware = createNetworkMiddleware(); +const networkMiddleware = createNetworkMiddleware({ + queueReleaseThrottle: 200, +}); const store = createStore( rootReducer, diff --git a/example/components/ConnectionToggler.js b/example/components/ConnectionToggler.js index c8c591b7..22c4c41c 100644 --- a/example/components/ConnectionToggler.js +++ b/example/components/ConnectionToggler.js @@ -6,7 +6,7 @@ function ConnectionToggler() { return ( {({ toggleConnection }) => ( - +