闭社主体 forked from https://github.com/tootsuite/mastodon
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.

42 lines
827 B

  1. const createAudio = sources => {
  2. const audio = new Audio();
  3. sources.forEach(({ type, src }) => {
  4. const source = document.createElement('source');
  5. source.type = type;
  6. source.src = src;
  7. audio.appendChild(source);
  8. });
  9. return audio;
  10. };
  11. const play = audio => {
  12. if (!audio.paused) {
  13. audio.pause();
  14. audio.fastSeek(0);
  15. }
  16. audio.play();
  17. };
  18. export default function soundsMiddleware() {
  19. const soundCache = {
  20. boop: createAudio([
  21. {
  22. src: '/sounds/boop.ogg',
  23. type: 'audio/ogg',
  24. },
  25. {
  26. src: '/sounds/boop.mp3',
  27. type: 'audio/mpeg',
  28. },
  29. ]),
  30. };
  31. return () => next => action => {
  32. if (action.meta && action.meta.sound && soundCache[action.meta.sound]) {
  33. play(soundCache[action.meta.sound]);
  34. }
  35. return next(action);
  36. };
  37. };