2022-07-26 10:20:39 +00:00
|
|
|
// @ts-ignore
|
2018-02-26 19:37:12 +00:00
|
|
|
import type { AudioElement } from '../media';
|
2022-07-26 10:20:39 +00:00
|
|
|
import ReducerRegistry from '../redux/ReducerRegistry';
|
|
|
|
import { assign } from '../redux/functions';
|
2018-02-26 19:37:12 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
REGISTER_SOUND,
|
2022-09-27 07:10:28 +00:00
|
|
|
UNREGISTER_SOUND,
|
|
|
|
_ADD_AUDIO_ELEMENT,
|
|
|
|
_REMOVE_AUDIO_ELEMENT
|
2018-02-26 19:37:12 +00:00
|
|
|
} from './actionTypes';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The structure use by this reducer to describe a sound.
|
|
|
|
*/
|
|
|
|
export type Sound = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The HTMLAudioElement which implements the audio playback functionality.
|
|
|
|
* Becomes available once the sound resource gets loaded and the sound can
|
|
|
|
* not be played until that happens.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
audioElement?: AudioElement;
|
2018-02-26 19:37:12 +00:00
|
|
|
|
|
|
|
/**
|
2022-07-26 10:20:39 +00:00
|
|
|
* This field is container for all optional parameters related to the sound.
|
2018-02-26 19:37:12 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
options?: Object;
|
2018-05-16 15:03:10 +00:00
|
|
|
|
|
|
|
/**
|
2022-07-26 10:20:39 +00:00
|
|
|
* This field describes the source of the audio resource to be played. It
|
|
|
|
* can be either a path to the file or an object depending on the platform
|
|
|
|
* (native vs web).
|
2018-05-16 15:03:10 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
src?: Object | string;
|
|
|
|
};
|
2018-02-26 19:37:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initial/default state of the feature {@code base/sounds}. It is a {@code Map}
|
|
|
|
* of globally stored sounds.
|
|
|
|
*
|
|
|
|
* @type {Map<string, Sound>}
|
|
|
|
*/
|
|
|
|
const DEFAULT_STATE = new Map();
|
|
|
|
|
2022-07-26 10:20:39 +00:00
|
|
|
export type ISoundsState = Map<string, Sound>;
|
|
|
|
|
2018-02-26 19:37:12 +00:00
|
|
|
/**
|
|
|
|
* The base/sounds feature's reducer.
|
|
|
|
*/
|
2022-09-05 09:05:07 +00:00
|
|
|
ReducerRegistry.register<ISoundsState>(
|
2018-02-26 19:37:12 +00:00
|
|
|
'features/base/sounds',
|
2022-09-05 09:05:07 +00:00
|
|
|
(state = DEFAULT_STATE, action): ISoundsState => {
|
2018-02-26 19:37:12 +00:00
|
|
|
switch (action.type) {
|
|
|
|
case _ADD_AUDIO_ELEMENT:
|
|
|
|
case _REMOVE_AUDIO_ELEMENT:
|
|
|
|
return _addOrRemoveAudioElement(state, action);
|
|
|
|
|
|
|
|
case REGISTER_SOUND:
|
|
|
|
return _registerSound(state, action);
|
|
|
|
|
|
|
|
case UNREGISTER_SOUND:
|
|
|
|
return _unregisterSound(state, action);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds or removes {@link AudioElement} associated with a {@link Sound}.
|
|
|
|
*
|
|
|
|
* @param {Map<string, Sound>} state - The current Redux state of this feature.
|
|
|
|
* @param {_ADD_AUDIO_ELEMENT | _REMOVE_AUDIO_ELEMENT} action - The action to be
|
|
|
|
* handled.
|
|
|
|
* @private
|
|
|
|
* @returns {Map<string, Sound>}
|
|
|
|
*/
|
2022-07-26 10:20:39 +00:00
|
|
|
function _addOrRemoveAudioElement(state: ISoundsState, action: any) {
|
2018-02-26 19:37:12 +00:00
|
|
|
const isAddAction = action.type === _ADD_AUDIO_ELEMENT;
|
|
|
|
const nextState = new Map(state);
|
|
|
|
const { soundId } = action;
|
|
|
|
|
|
|
|
const sound = nextState.get(soundId);
|
|
|
|
|
|
|
|
if (sound) {
|
|
|
|
if (isAddAction) {
|
|
|
|
nextState.set(soundId,
|
|
|
|
assign(sound, {
|
|
|
|
audioElement: action.audioElement
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
nextState.set(soundId,
|
|
|
|
assign(sound, {
|
|
|
|
audioElement: undefined
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextState;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a new {@link Sound} for given id and source. It will make
|
|
|
|
* the {@link SoundCollection} component render HTMLAudioElement for given
|
|
|
|
* source making it available for playback through the redux actions.
|
|
|
|
*
|
|
|
|
* @param {Map<string, Sound>} state - The current Redux state of the sounds
|
|
|
|
* features.
|
|
|
|
* @param {REGISTER_SOUND} action - The register sound action.
|
|
|
|
* @private
|
|
|
|
* @returns {Map<string, Sound>}
|
|
|
|
*/
|
2022-07-26 10:20:39 +00:00
|
|
|
function _registerSound(state: ISoundsState, action: any) {
|
2018-02-26 19:37:12 +00:00
|
|
|
const nextState = new Map(state);
|
|
|
|
|
|
|
|
nextState.set(action.soundId, {
|
2018-05-16 15:03:10 +00:00
|
|
|
src: action.src,
|
|
|
|
options: action.options
|
2018-02-26 19:37:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return nextState;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregisters a {@link Sound} which will make the {@link SoundCollection}
|
|
|
|
* component stop rendering the corresponding HTMLAudioElement. This will
|
|
|
|
* result further in the audio resource disposal.
|
|
|
|
*
|
|
|
|
* @param {Map<string, Sound>} state - The current Redux state of this feature.
|
|
|
|
* @param {UNREGISTER_SOUND} action - The unregister sound action.
|
|
|
|
* @private
|
|
|
|
* @returns {Map<string, Sound>}
|
|
|
|
*/
|
2022-07-26 10:20:39 +00:00
|
|
|
function _unregisterSound(state: ISoundsState, action: any) {
|
2018-02-26 19:37:12 +00:00
|
|
|
const nextState = new Map(state);
|
|
|
|
|
|
|
|
nextState.delete(action.soundId);
|
|
|
|
|
|
|
|
return nextState;
|
|
|
|
}
|