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.js 3.7KB

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