Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

actions.ts 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { IStore } from '../../app/types';
  2. import { Sounds } from '../config/configType';
  3. import { AudioElement } from '../media/components/AbstractAudio';
  4. import {
  5. PLAY_SOUND,
  6. REGISTER_SOUND,
  7. STOP_SOUND,
  8. UNREGISTER_SOUND,
  9. _ADD_AUDIO_ELEMENT,
  10. _REMOVE_AUDIO_ELEMENT
  11. } from './actionTypes';
  12. import { getSoundsPath } from './functions';
  13. import { getDisabledSounds } from './functions.any';
  14. /**
  15. * Adds {@link AudioElement} instance to the base/sounds feature state for the
  16. * {@link Sound} instance identified by the given id. After this action the
  17. * sound can be played by dispatching the {@link PLAY_SOUND} action.
  18. *
  19. * @param {string} soundId - The sound identifier for which the audio element
  20. * will be stored.
  21. * @param {AudioElement} audioElement - The audio element which implements the
  22. * audio playback functionality and which is backed by the sound resource
  23. * corresponding to the {@link Sound} with the given id.
  24. * @protected
  25. * @returns {{
  26. * type: PLAY_SOUND,
  27. * audioElement: AudioElement,
  28. * soundId: string
  29. * }}
  30. */
  31. export function _addAudioElement(soundId: string, audioElement: AudioElement) {
  32. return {
  33. type: _ADD_AUDIO_ELEMENT,
  34. audioElement,
  35. soundId
  36. };
  37. }
  38. /**
  39. * The opposite of {@link _addAudioElement} which removes {@link AudioElement}
  40. * for given sound from base/sounds state. It means that the audio resource has
  41. * been disposed and the sound can no longer be played.
  42. *
  43. * @param {string} soundId - The {@link Sound} instance identifier for which the
  44. * audio element is being removed.
  45. * @protected
  46. * @returns {{
  47. * type: _REMOVE_AUDIO_ELEMENT,
  48. * soundId: string
  49. * }}
  50. */
  51. export function _removeAudioElement(soundId: string) {
  52. return {
  53. type: _REMOVE_AUDIO_ELEMENT,
  54. soundId
  55. };
  56. }
  57. /**
  58. * Starts playback of the sound identified by the given sound id. The action
  59. * will have effect only if the audio resource has been loaded already.
  60. *
  61. * @param {string} soundId - The id of the sound to be played (the same one
  62. * which was used in {@link registerSound} to register the sound).
  63. * @returns {Function}
  64. */
  65. export function playSound(soundId: string) {
  66. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  67. const disabledSounds = getDisabledSounds(getState());
  68. if (!disabledSounds.includes(soundId as Sounds) && !disabledSounds.find(id => soundId.startsWith(id))) {
  69. dispatch({
  70. type: PLAY_SOUND,
  71. soundId
  72. });
  73. }
  74. };
  75. }
  76. /**
  77. * Registers a new sound for given id and a source object which can be either a
  78. * path or a raw object depending on the platform (native vs web). It will make
  79. * the {@link SoundCollection} render extra HTMLAudioElement which will make it
  80. * available for playback through the {@link playSound} action.
  81. *
  82. * @param {string} soundId - The global identifier which identify the sound
  83. * created for given source object.
  84. * @param {string} soundName - The name of bundled audio file that will be
  85. * associated with the given {@code soundId}.
  86. * @param {Object} options - Optional parameters.
  87. * @param {boolean} options.loop - True in order to loop the sound.
  88. * @returns {{
  89. * type: REGISTER_SOUND,
  90. * soundId: string,
  91. * src: string,
  92. * options: {
  93. * loop: boolean
  94. * }
  95. * }}
  96. */
  97. export function registerSound(
  98. soundId: string, soundName: string, options: Object = {}) {
  99. return {
  100. type: REGISTER_SOUND,
  101. soundId,
  102. src: `${getSoundsPath()}/${soundName}`,
  103. options
  104. };
  105. }
  106. /**
  107. * Stops playback of the sound identified by the given sound id.
  108. *
  109. * @param {string} soundId - The id of the sound to be stopped (the same one
  110. * which was used in {@link registerSound} to register the sound).
  111. * @returns {{
  112. * type: STOP_SOUND,
  113. * soundId: string
  114. * }}
  115. */
  116. export function stopSound(soundId: string) {
  117. return {
  118. type: STOP_SOUND,
  119. soundId
  120. };
  121. }
  122. /**
  123. * Unregister the sound identified by the given id. It will make the
  124. * {@link SoundCollection} component stop rendering the corresponding
  125. * {@code HTMLAudioElement} which then should result in the audio resource
  126. * disposal.
  127. *
  128. * @param {string} soundId - The identifier of the {@link Sound} to be removed.
  129. * @returns {{
  130. * type: UNREGISTER_SOUND,
  131. * soundId: string
  132. * }}
  133. */
  134. export function unregisterSound(soundId: string) {
  135. return {
  136. type: UNREGISTER_SOUND,
  137. soundId
  138. };
  139. }