"Fossies" - the Fresh Open Source Software Archive 
Member "jitsi-meet-7315/react/features/base/sounds/reducer.ts" (2 Jun 2023, 3988 Bytes) of package /linux/misc/jitsi-meet-7315.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the last
Fossies "Diffs" side-by-side code changes report for "reducer.ts":
jitsi-meet_8319_vs_jitsi-meet_8615.
1 import { AnyAction } from 'redux';
2
3 import { AudioElement } from '../media/components/AbstractAudio';
4 import ReducerRegistry from '../redux/ReducerRegistry';
5 import { assign } from '../redux/functions';
6
7 import {
8 REGISTER_SOUND,
9 UNREGISTER_SOUND,
10 _ADD_AUDIO_ELEMENT,
11 _REMOVE_AUDIO_ELEMENT
12 } from './actionTypes';
13
14 /**
15 * The structure use by this reducer to describe a sound.
16 */
17 export type Sound = {
18
19 /**
20 * The HTMLAudioElement which implements the audio playback functionality.
21 * Becomes available once the sound resource gets loaded and the sound can
22 * not be played until that happens.
23 */
24 audioElement?: AudioElement;
25
26 /**
27 * This field is container for all optional parameters related to the sound.
28 */
29 options?: {
30 loop: boolean;
31 };
32
33 /**
34 * This field describes the source of the audio resource to be played. It
35 * can be either a path to the file or an object depending on the platform
36 * (native vs web).
37 */
38 src?: Object | string;
39 };
40
41 /**
42 * Initial/default state of the feature {@code base/sounds}. It is a {@code Map}
43 * of globally stored sounds.
44 *
45 * @type {Map<string, Sound>}
46 */
47 const DEFAULT_STATE = new Map();
48
49 export type ISoundsState = Map<string, Sound>;
50
51 /**
52 * The base/sounds feature's reducer.
53 */
54 ReducerRegistry.register<ISoundsState>(
55 'features/base/sounds',
56 (state = DEFAULT_STATE, action): ISoundsState => {
57 switch (action.type) {
58 case _ADD_AUDIO_ELEMENT:
59 case _REMOVE_AUDIO_ELEMENT:
60 return _addOrRemoveAudioElement(state, action);
61
62 case REGISTER_SOUND:
63 return _registerSound(state, action);
64
65 case UNREGISTER_SOUND:
66 return _unregisterSound(state, action);
67
68 default:
69 return state;
70 }
71 });
72
73 /**
74 * Adds or removes {@link AudioElement} associated with a {@link Sound}.
75 *
76 * @param {Map<string, Sound>} state - The current Redux state of this feature.
77 * @param {_ADD_AUDIO_ELEMENT | _REMOVE_AUDIO_ELEMENT} action - The action to be
78 * handled.
79 * @private
80 * @returns {Map<string, Sound>}
81 */
82 function _addOrRemoveAudioElement(state: ISoundsState, action: AnyAction) {
83 const isAddAction = action.type === _ADD_AUDIO_ELEMENT;
84 const nextState = new Map(state);
85 const { soundId } = action;
86
87 const sound = nextState.get(soundId);
88
89 if (sound) {
90 if (isAddAction) {
91 nextState.set(soundId,
92 assign(sound, {
93 audioElement: action.audioElement
94 }));
95 } else {
96 nextState.set(soundId,
97 assign(sound, {
98 audioElement: undefined
99 }));
100 }
101 }
102
103 return nextState;
104 }
105
106 /**
107 * Registers a new {@link Sound} for given id and source. It will make
108 * the {@link SoundCollection} component render HTMLAudioElement for given
109 * source making it available for playback through the redux actions.
110 *
111 * @param {Map<string, Sound>} state - The current Redux state of the sounds
112 * features.
113 * @param {REGISTER_SOUND} action - The register sound action.
114 * @private
115 * @returns {Map<string, Sound>}
116 */
117 function _registerSound(state: ISoundsState, action: AnyAction) {
118 const nextState = new Map(state);
119
120 nextState.set(action.soundId, {
121 src: action.src,
122 options: action.options
123 });
124
125 return nextState;
126 }
127
128 /**
129 * Unregisters a {@link Sound} which will make the {@link SoundCollection}
130 * component stop rendering the corresponding HTMLAudioElement. This will
131 * result further in the audio resource disposal.
132 *
133 * @param {Map<string, Sound>} state - The current Redux state of this feature.
134 * @param {UNREGISTER_SOUND} action - The unregister sound action.
135 * @private
136 * @returns {Map<string, Sound>}
137 */
138 function _unregisterSound(state: ISoundsState, action: AnyAction) {
139 const nextState = new Map(state);
140
141 nextState.delete(action.soundId);
142
143 return nextState;
144 }