From 701552ec8f21fae5646f4e2d3fdc12ac630ca397 Mon Sep 17 00:00:00 2001 From: paweldomas Date: Fri, 25 May 2018 16:10:48 -0500 Subject: [PATCH] ref(mobile/wake-lock): convert middleware to a state listener If CONFERENCE_LEFT would arrive with a delay while we're in another conference already, then the wake lock could end up in an incorrect state. --- react/features/mobile/wake-lock/middleware.js | 45 +++++-------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/react/features/mobile/wake-lock/middleware.js b/react/features/mobile/wake-lock/middleware.js index 1071c1c51..08b4b7546 100644 --- a/react/features/mobile/wake-lock/middleware.js +++ b/react/features/mobile/wake-lock/middleware.js @@ -1,42 +1,21 @@ import KeepAwake from 'react-native-keep-awake'; -import { - CONFERENCE_FAILED, - CONFERENCE_JOINED, - CONFERENCE_LEFT, - SET_AUDIO_ONLY -} from '../../base/conference'; -import { MiddlewareRegistry } from '../../base/redux'; +import { getCurrentConference } from '../../base/conference'; +import { StateListenerRegistry } from '../../base/redux'; /** - * Middleware that captures conference actions and activates or deactivates the - * wake lock accordingly. If the wake lock is active, it will prevent the screen - * from dimming. - * - * @param {Store} store - Redux store. - * @returns {Function} + * State listener that activates or deactivates the wake lock accordingly. If + * the wake lock is active, it will prevent the screen from dimming. */ -MiddlewareRegistry.register(store => next => action => { - switch (action.type) { - case CONFERENCE_JOINED: { - const { audioOnly } = store.getState()['features/base/conference']; +StateListenerRegistry.register( + /* selector */ state => { + const { audioOnly } = state['features/base/conference']; + const conference = getCurrentConference(state); - _setWakeLock(!audioOnly); - break; - } - - case CONFERENCE_FAILED: - case CONFERENCE_LEFT: - _setWakeLock(false); - break; - - case SET_AUDIO_ONLY: - _setWakeLock(!action.audioOnly); - break; - } - - return next(action); -}); + return Boolean(conference && !audioOnly); + }, + /* listener */ wakeLock => _setWakeLock(wakeLock) +); /** * Activates/deactivates the wake lock. If the wake lock is active, it will