From b13ed0f409a35eb0d6c9e9da77a0559f8926c8f9 Mon Sep 17 00:00:00 2001 From: Mattermost Build Date: Thu, 5 May 2022 20:13:18 +0200 Subject: [PATCH] MM-43904 - Fix: Calls batch actions (#6229) (#6232) * fix batch actions * tests (cherry picked from commit 67c65156a7f761da35b35ccadb93bed6aee28bb9) Co-authored-by: Christopher Poile --- .../calls/store/actions/calls.test.js | 4 +-- app/products/calls/store/actions/calls.ts | 35 +++++++++++-------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/products/calls/store/actions/calls.test.js b/app/products/calls/store/actions/calls.test.js index 741aaba4623..884027706b2 100644 --- a/app/products/calls/store/actions/calls.test.js +++ b/app/products/calls/store/actions/calls.test.js @@ -141,14 +141,14 @@ describe('Actions.Calls', () => { }); it('loadCalls', async () => { - await store.dispatch(await store.dispatch(CallsActions.loadCalls())); + await store.dispatch(CallsActions.loadCalls()); expect(Client4.getCalls).toBeCalledWith(); assert.equal(store.getState().entities.calls.calls['channel-1'].channelId, 'channel-1'); assert.equal(store.getState().entities.calls.enabled['channel-1'], true); }); it('loadConfig', async () => { - await store.dispatch(await store.dispatch(CallsActions.loadConfig())); + await store.dispatch(CallsActions.loadConfig()); expect(Client4.getCallsConfig).toBeCalledWith(); assert.equal(store.getState().entities.calls.config.DefaultEnabled, true); assert.equal(store.getState().entities.calls.config.AllowEnableCalls, true); diff --git a/app/products/calls/store/actions/calls.ts b/app/products/calls/store/actions/calls.ts index 8b01cf86aaa..bdd3ab59720 100644 --- a/app/products/calls/store/actions/calls.ts +++ b/app/products/calls/store/actions/calls.ts @@ -3,12 +3,19 @@ import {intlShape} from 'react-intl'; import InCallManager from 'react-native-incall-manager'; +import {batch} from 'react-redux'; import {Client4} from '@client/rest'; import Calls from '@constants/calls'; import {logError} from '@mm-redux/actions/errors'; import {forceLogoutIfNecessary} from '@mm-redux/actions/helpers'; -import {GenericAction, ActionFunc, DispatchFunc, GetStateFunc, batchActions} from '@mm-redux/types/actions'; +import { + GenericAction, + ActionFunc, + DispatchFunc, + GetStateFunc, + ActionResult, +} from '@mm-redux/types/actions'; import {Dictionary} from '@mm-redux/types/utilities'; import {newClient} from '@mmproducts/calls/connection'; import CallsTypes from '@mmproducts/calls/store/action_types/calls'; @@ -19,7 +26,7 @@ import {hasMicrophonePermission} from '@utils/permission'; export let ws: any = null; export function loadConfig(force = false): ActionFunc { - return async (dispatch: DispatchFunc, getState: GetStateFunc): Promise => { + return async (dispatch: DispatchFunc, getState: GetStateFunc): Promise => { if (!force) { if ((Date.now() - getConfig(getState()).last_retrieved_at) < Calls.RefreshConfigMillis) { return {} as GenericAction; @@ -34,28 +41,27 @@ export function loadConfig(force = false): ActionFunc { dispatch(logError(error)); // Reset the config to the default (off) since it looks like Calls is not enabled. - return { + dispatch({ type: CallsTypes.RECEIVED_CONFIG, data: {...DefaultServerConfig, last_retrieved_at: Date.now()}, - }; + }); } - return { - type: CallsTypes.RECEIVED_CONFIG, - data: {...data, last_retrieved_at: Date.now()}, - }; + data = {...data, last_retrieved_at: Date.now()}; + dispatch({type: CallsTypes.RECEIVED_CONFIG, data}); + return {data}; }; } export function loadCalls(): ActionFunc { - return async (dispatch: DispatchFunc, getState: GetStateFunc): Promise => { + return async (dispatch: DispatchFunc, getState: GetStateFunc): Promise => { let resp = []; try { resp = await Client4.getCalls(); } catch (error) { forceLogoutIfNecessary(error, dispatch, getState); dispatch(logError(error)); - return {} as GenericAction; + return {}; } const callsResults: Dictionary = {}; @@ -86,7 +92,8 @@ export function loadCalls(): ActionFunc { enabled: enabledChannels, }; - return {type: CallsTypes.RECEIVED_CALLS, data}; + dispatch({type: CallsTypes.RECEIVED_CALLS, data}); + return {data}; }; } @@ -98,9 +105,9 @@ export function batchLoadCalls(forceConfig = false): ActionFunc { return {}; } - const promises = [dispatch(loadConfig(forceConfig)), dispatch(loadCalls())]; - Promise.all(promises).then((actions: Array>) => { - dispatch(batchActions(actions, 'BATCH_LOAD_CALLS')); + batch(() => { + dispatch(loadConfig(forceConfig)); + dispatch(loadCalls()); }); return {};