Create post timeout (#1242)

* Add timeout for api calls that take too long

This will allow any api call that takes too long to fail faster.

Specially for Client4.createPost.  If the api never returns the post is in a pending state and the user is not able to retry.

* Add canTimeout flag
This commit is contained in:
Chris Duarte 2017-12-08 13:19:17 -08:00 committed by enahum
parent d6cad314af
commit 7fdb0c4d27
2 changed files with 33 additions and 0 deletions

View file

@ -15,6 +15,7 @@ import {NavigationTypes, ViewTypes} from 'app/constants';
import appReducer from 'app/reducers';
import networkConnectionListener from 'app/utils/network';
import {createSentryMiddleware} from 'app/utils/sentry/middleware';
import {promiseTimeout} from 'app/utils/promise_timeout';
import {messageRetention} from './middleware';
import {transformSet} from './utils';
@ -102,6 +103,22 @@ export default function configureAppStore(initialState) {
);
const offlineOptions = {
effect: (effect, action) => {
if (typeof effect !== 'function') {
throw new Error('Offline Action: effect must be a function.');
} else if (!action.meta.offline.commit) {
throw new Error('Offline Action: commit action must be present.');
}
if (action.meta.offline.canTimeout) {
const defaultTimeout = 10000;
const timeout = action.meta.offline.timeout || defaultTimeout;
return promiseTimeout(effect(), timeout);
}
return effect();
},
detectNetwork: (callback) => networkConnectionListener(callback),
persist: (store, options) => {
const persistor = persistStore(store, {storage: AsyncStorage, ...options}, () => {

View file

@ -0,0 +1,16 @@
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
export function promiseTimeout(promise, ms) {
const timeout = new Promise((resolve, reject) => {
const id = setTimeout(() => {
clearTimeout(id);
reject('Timed out in ' + ms + 'ms.');
}, ms);
});
return Promise.race([
promise,
timeout
]);
}