Skip to content

Commit 112cd6e

Browse files
authored
Feat: new queueReleaseThrottle optional param for createNetworkMiddleware
1 parent 9a2ca4b commit 112cd6e

File tree

16 files changed

+202
-118
lines changed

16 files changed

+202
-118
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ createNetworkMiddleware(config: MiddlewareConfig): ReduxMiddleware
269269

270270
type MiddlewareConfig = {
271271
regexActionType?: RegExp = /FETCH.*REQUEST/,
272-
actionTypes?: Array<string> = []
272+
actionTypes?: Array<string> = [],
273+
queueReleaseThrottle?: number = 50,
273274
}
274275
```
275276

@@ -281,6 +282,8 @@ By default it's configured to intercept actions for fetching data following the
281282

282283
`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`.
283284

285+
`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.
286+
284287
##### Thunks Config
285288
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:
286289

@@ -316,7 +319,9 @@ import { createNetworkMiddleware } from 'react-native-offline';
316319
import createSagaMiddleware from 'redux-saga';
317320

318321
const sagaMiddleware = createSagaMiddleware();
319-
const networkMiddleware = createNetworkMiddleware();
322+
const networkMiddleware = createNetworkMiddleware({
323+
queueReleaseThrottle: 200,
324+
});
320325

321326
const store = createStore(
322327
rootReducer,

example/components/ConnectionToggler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function ConnectionToggler() {
66
return (
77
<DummyNetworkContext.Consumer>
88
{({ toggleConnection }) => (
9-
<View style={{ marginBottom: 30 }}>
9+
<View style={{ marginBottom: 20 }}>
1010
<Button
1111
onPress={toggleConnection}
1212
title="Toggle Internet connection"

example/components/OfflineQueue.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import React from 'react';
22
import { View, StyleSheet, Text, ScrollView } from 'react-native';
33
import { connect } from 'react-redux';
44

5-
function OfflineQueue({ queue }) {
5+
function OfflineQueue({ queue, title }) {
66
return (
77
<View style={{ height: 90, marginVertical: 8 }}>
8-
<Text style={styles.title}>Offline Queue (FIFO)</Text>
8+
<Text style={styles.title}>{title}</Text>
99
<ScrollView
1010
style={{ flex: 1 }}
1111
contentContainerStyle={styles.queue}

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"expo": "^32.0.0",
1616
"react": "16.5.0",
1717
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
18-
"react-native-offline": "^4.2.0",
18+
"react-native-offline": "4.3.0",
1919
"react-navigation": "^3.0.9",
2020
"redux": "^4.0.1",
2121
"redux-saga": "0.16.2"

example/redux/createStore.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ import createSagaMiddleware from 'redux-saga';
77
import counter from './reducer';
88
import rootSaga from './sagas';
99

10-
export default function createReduxStore({ withSaga = false } = {}) {
10+
export default function createReduxStore({
11+
withSaga = false,
12+
queueReleaseThrottle = 1000,
13+
} = {}) {
1114
const networkMiddleware = createNetworkMiddleware({
1215
regexActionType: /^OTHER/,
1316
actionTypes: ['ADD_ONE', 'SUB_ONE'],
17+
queueReleaseThrottle,
1418
});
1519

1620
const sagaMiddleware = createSagaMiddleware();

example/screens/ReduxScreen.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Platform, View, StyleSheet, Image } from 'react-native';
2+
import { Platform, View, StyleSheet, Image, Text } from "react-native";
33
import { ReduxNetworkProvider } from 'react-native-offline';
44
import { Provider } from 'react-redux';
55

@@ -11,7 +11,7 @@ import Counter from '../components/Counter';
1111
import OfflineQueue from '../components/OfflineQueue';
1212
import ActionButtons from '../components/ActionButtons';
1313

14-
const store = createStore();
14+
const store = createStore({ queueReleaseThrottle: 1000 });
1515

1616
export default class ReduxScreen extends React.Component {
1717
static navigationOptions = {
@@ -42,7 +42,7 @@ export default class ReduxScreen extends React.Component {
4242
<View style={styles.secondSection}>
4343
<ActionButtons />
4444
<View style={styles.offlineQueue}>
45-
<OfflineQueue />
45+
<OfflineQueue title="Offline Queue (FIFO), throttle = 1s" />
4646
</View>
4747
</View>
4848
</View>

example/screens/SagasScreen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ActionButtons from '../components/ActionButtons';
77
import OfflineQueue from '../components/OfflineQueue';
88
import createStore from '../redux/createStore';
99

10-
const store = createStore({ withSaga: true });
10+
const store = createStore({ withSaga: true, queueReleaseThrottle: 250 });
1111

1212
export default class SettingsScreen extends React.Component {
1313
static navigationOptions = {
@@ -42,7 +42,7 @@ export default class SettingsScreen extends React.Component {
4242
<View style={styles.secondSection}>
4343
<ActionButtons />
4444
<View style={styles.offlineQueue}>
45-
<OfflineQueue />
45+
<OfflineQueue title="Offline Queue (FIFO), throttle = 250ms" />
4646
</View>
4747
</View>
4848
</View>

example/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5592,10 +5592,10 @@ react-native-maps@expo/react-native-maps#v0.22.1-exp.0:
55925592
version "0.22.1"
55935593
resolved "https://codeload.github.com/expo/react-native-maps/tar.gz/e6f98ff7272e5d0a7fe974a41f28593af2d77bb2"
55945594

5595-
react-native-offline@^4.2.0:
5596-
version "4.2.0"
5597-
resolved "https://registry.yarnpkg.com/react-native-offline/-/react-native-offline-4.2.0.tgz#baf2337a8126b93e1b9241c6b328bbe1c26a19c0"
5598-
integrity sha512-gFEs5oDEcSeUybnGQ/MlOI3Q9K8rH+cXcMWbyAoLDYnY8K6MCRXwOwfhFuTfKaRrcfMsywjTLcYetfNMMVN1ng==
5595+
react-native-offline@4.3.0:
5596+
version "4.3.0"
5597+
resolved "https://registry.yarnpkg.com/react-native-offline/-/react-native-offline-4.3.0.tgz#6877e4a4b961e230e0a198bce2d0611ca8af9d89"
5598+
integrity sha512-hM+rNGHKpmagseFuhnok/c7uCrMqs70fHRzaqGBCKnTsio6ff3/wb7jomsuRjsR1Iy+DwBws+VUovcfpQAtuBQ==
55995599
dependencies:
56005600
lodash "^4.17.11"
56015601
react-redux "^6.0.0"

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-offline",
3-
"version": "4.2.0",
3+
"version": "4.3.0",
44
"description": "Handy toolbelt to deal with offline mode in React Native applications. Cross-platform, provides a smooth redux integration.",
55
"main": "./src/index.js",
66
"author": "Raul Gomez Acuña <[email protected]> (https://github.com/rgommezz)",
@@ -71,7 +71,7 @@
7171
"dependencies": {
7272
"lodash": "^4.17.11",
7373
"react-redux": "^6.0.0",
74-
"redux":"4.x",
74+
"redux": "4.x",
7575
"redux-saga": "^0.16.2"
7676
},
7777
"jest": {

src/components/ReduxNetworkProvider.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
type Props = {
1414
dispatch: FluxAction => FluxAction,
1515
isConnected: boolean,
16-
actionQueue: Array<FluxAction>,
1716
pingTimeout?: number,
1817
pingServerUrl?: string,
1918
shouldPing?: boolean,
@@ -36,17 +35,10 @@ class ReduxNetworkProvider extends React.Component<Props> {
3635
};
3736

3837
handleConnectivityChange = (isConnected: boolean) => {
39-
const { isConnected: wasConnected, actionQueue, dispatch } = this.props;
40-
38+
const { isConnected: wasConnected, dispatch } = this.props;
4139
if (isConnected !== wasConnected) {
4240
dispatch(connectionChange(isConnected));
4341
}
44-
// dispatching queued actions in order of arrival (if we have any)
45-
if (!wasConnected && isConnected && actionQueue.length > 0) {
46-
actionQueue.forEach((action: *) => {
47-
dispatch(action);
48-
});
49-
}
5042
};
5143

5244
render() {
@@ -65,7 +57,6 @@ class ReduxNetworkProvider extends React.Component<Props> {
6557
function mapStateToProps(state: { network: NetworkState }) {
6658
return {
6759
isConnected: state.network.isConnected,
68-
actionQueue: state.network.actionQueue,
6960
};
7061
}
7162

0 commit comments

Comments
 (0)