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.

actions.ts 4.4KB

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