You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reducer.ts 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // @ts-ignore
  2. import type { AudioElement } from '../media';
  3. import ReducerRegistry from '../redux/ReducerRegistry';
  4. import { assign } from '../redux/functions';
  5. import {
  6. REGISTER_SOUND,
  7. UNREGISTER_SOUND,
  8. _ADD_AUDIO_ELEMENT,
  9. _REMOVE_AUDIO_ELEMENT
  10. } from './actionTypes';
  11. /**
  12. * The structure use by this reducer to describe a sound.
  13. */
  14. export type Sound = {
  15. /**
  16. * The HTMLAudioElement which implements the audio playback functionality.
  17. * Becomes available once the sound resource gets loaded and the sound can
  18. * not be played until that happens.
  19. */
  20. audioElement?: AudioElement;
  21. /**
  22. * This field is container for all optional parameters related to the sound.
  23. */
  24. options?: Object;
  25. /**
  26. * This field describes the source of the audio resource to be played. It
  27. * can be either a path to the file or an object depending on the platform
  28. * (native vs web).
  29. */
  30. src?: Object | string;
  31. };
  32. /**
  33. * Initial/default state of the feature {@code base/sounds}. It is a {@code Map}
  34. * of globally stored sounds.
  35. *
  36. * @type {Map<string, Sound>}
  37. */
  38. const DEFAULT_STATE = new Map();
  39. export type ISoundsState = Map<string, Sound>;
  40. /**
  41. * The base/sounds feature's reducer.
  42. */
  43. ReducerRegistry.register<ISoundsState>(
  44. 'features/base/sounds',
  45. (state = DEFAULT_STATE, action): ISoundsState => {
  46. switch (action.type) {
  47. case _ADD_AUDIO_ELEMENT:
  48. case _REMOVE_AUDIO_ELEMENT:
  49. return _addOrRemoveAudioElement(state, action);
  50. case REGISTER_SOUND:
  51. return _registerSound(state, action);
  52. case UNREGISTER_SOUND:
  53. return _unregisterSound(state, action);
  54. default:
  55. return state;
  56. }
  57. });
  58. /**
  59. * Adds or removes {@link AudioElement} associated with a {@link Sound}.
  60. *
  61. * @param {Map<string, Sound>} state - The current Redux state of this feature.
  62. * @param {_ADD_AUDIO_ELEMENT | _REMOVE_AUDIO_ELEMENT} action - The action to be
  63. * handled.
  64. * @private
  65. * @returns {Map<string, Sound>}
  66. */
  67. function _addOrRemoveAudioElement(state: ISoundsState, action: any) {
  68. const isAddAction = action.type === _ADD_AUDIO_ELEMENT;
  69. const nextState = new Map(state);
  70. const { soundId } = action;
  71. const sound = nextState.get(soundId);
  72. if (sound) {
  73. if (isAddAction) {
  74. nextState.set(soundId,
  75. assign(sound, {
  76. audioElement: action.audioElement
  77. }));
  78. } else {
  79. nextState.set(soundId,
  80. assign(sound, {
  81. audioElement: undefined
  82. }));
  83. }
  84. }
  85. return nextState;
  86. }
  87. /**
  88. * Registers a new {@link Sound} for given id and source. It will make
  89. * the {@link SoundCollection} component render HTMLAudioElement for given
  90. * source making it available for playback through the redux actions.
  91. *
  92. * @param {Map<string, Sound>} state - The current Redux state of the sounds
  93. * features.
  94. * @param {REGISTER_SOUND} action - The register sound action.
  95. * @private
  96. * @returns {Map<string, Sound>}
  97. */
  98. function _registerSound(state: ISoundsState, action: any) {
  99. const nextState = new Map(state);
  100. nextState.set(action.soundId, {
  101. src: action.src,
  102. options: action.options
  103. });
  104. return nextState;
  105. }
  106. /**
  107. * Unregisters a {@link Sound} which will make the {@link SoundCollection}
  108. * component stop rendering the corresponding HTMLAudioElement. This will
  109. * result further in the audio resource disposal.
  110. *
  111. * @param {Map<string, Sound>} state - The current Redux state of this feature.
  112. * @param {UNREGISTER_SOUND} action - The unregister sound action.
  113. * @private
  114. * @returns {Map<string, Sound>}
  115. */
  116. function _unregisterSound(state: ISoundsState, action: any) {
  117. const nextState = new Map(state);
  118. nextState.delete(action.soundId);
  119. return nextState;
  120. }