2022-09-23 09:03:25 +00:00
|
|
|
import { IStore } from '../../app/types';
|
|
|
|
import MiddlewareRegistry from '../redux/MiddlewareRegistry';
|
2018-02-26 19:37:12 +00:00
|
|
|
|
2018-05-16 15:03:10 +00:00
|
|
|
import { PLAY_SOUND, STOP_SOUND } from './actionTypes';
|
2019-08-21 14:50:00 +00:00
|
|
|
import logger from './logger';
|
2018-02-26 19:37:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements the entry point of the middleware of the feature base/media.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
switch (action.type) {
|
|
|
|
case PLAY_SOUND:
|
|
|
|
_playSound(store, action.soundId);
|
|
|
|
break;
|
2018-05-16 15:03:10 +00:00
|
|
|
case STOP_SOUND:
|
|
|
|
_stopSound(store, action.soundId);
|
|
|
|
break;
|
2018-02-26 19:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plays sound from audio element registered in the Redux store.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The Redux store instance.
|
|
|
|
* @param {string} soundId - Audio element identifier.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-23 09:03:25 +00:00
|
|
|
function _playSound({ getState }: IStore, soundId: string) {
|
2018-02-26 19:37:12 +00:00
|
|
|
const sounds = getState()['features/base/sounds'];
|
|
|
|
const sound = sounds.get(soundId);
|
|
|
|
|
|
|
|
if (sound) {
|
|
|
|
if (sound.audioElement) {
|
|
|
|
sound.audioElement.play();
|
|
|
|
} else {
|
|
|
|
logger.warn(`PLAY_SOUND: sound not loaded yet for id: ${soundId}`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.warn(`PLAY_SOUND: no sound found for id: ${soundId}`);
|
|
|
|
}
|
|
|
|
}
|
2018-05-16 15:03:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop sound from audio element registered in the Redux store.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The Redux store instance.
|
|
|
|
* @param {string} soundId - Audio element identifier.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-23 09:03:25 +00:00
|
|
|
function _stopSound({ getState }: IStore, soundId: string) {
|
2018-05-16 15:03:10 +00:00
|
|
|
const sounds = getState()['features/base/sounds'];
|
|
|
|
const sound = sounds.get(soundId);
|
|
|
|
|
|
|
|
if (sound) {
|
|
|
|
const { audioElement } = sound;
|
|
|
|
|
|
|
|
if (audioElement) {
|
|
|
|
audioElement.stop();
|
|
|
|
} else {
|
|
|
|
logger.warn(`STOP_SOUND: sound not loaded yet for id: ${soundId}`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.warn(`STOP_SOUND: no sound found for id: ${soundId}`);
|
|
|
|
}
|
|
|
|
}
|