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.
This commit is contained in:
paweldomas 2018-05-25 16:10:48 -05:00 committed by Saúl Ibarra Corretgé
parent d26d1ff925
commit 701552ec8f
1 changed files with 12 additions and 33 deletions

View File

@ -1,42 +1,21 @@
import KeepAwake from 'react-native-keep-awake'; import KeepAwake from 'react-native-keep-awake';
import { import { getCurrentConference } from '../../base/conference';
CONFERENCE_FAILED, import { StateListenerRegistry } from '../../base/redux';
CONFERENCE_JOINED,
CONFERENCE_LEFT,
SET_AUDIO_ONLY
} from '../../base/conference';
import { MiddlewareRegistry } from '../../base/redux';
/** /**
* Middleware that captures conference actions and activates or deactivates the * State listener that activates or deactivates the wake lock accordingly. If
* wake lock accordingly. If the wake lock is active, it will prevent the screen * the wake lock is active, it will prevent the screen from dimming.
* from dimming.
*
* @param {Store} store - Redux store.
* @returns {Function}
*/ */
MiddlewareRegistry.register(store => next => action => { StateListenerRegistry.register(
switch (action.type) { /* selector */ state => {
case CONFERENCE_JOINED: { const { audioOnly } = state['features/base/conference'];
const { audioOnly } = store.getState()['features/base/conference']; const conference = getCurrentConference(state);
_setWakeLock(!audioOnly); return Boolean(conference && !audioOnly);
break; },
} /* listener */ wakeLock => _setWakeLock(wakeLock)
);
case CONFERENCE_FAILED:
case CONFERENCE_LEFT:
_setWakeLock(false);
break;
case SET_AUDIO_ONLY:
_setWakeLock(!action.audioOnly);
break;
}
return next(action);
});
/** /**
* Activates/deactivates the wake lock. If the wake lock is active, it will * Activates/deactivates the wake lock. If the wake lock is active, it will