2024-01-08 21:57:40 +11:00
|
|
|
import { isAction } from '@reduxjs/toolkit';
|
|
|
|
import type { Middleware, UnknownAction } from '@reduxjs/toolkit';
|
2023-05-10 20:59:29 +10:00
|
|
|
|
2023-07-12 11:02:32 +10:00
|
|
|
import ready from 'mastodon/ready';
|
|
|
|
import { assetHost } from 'mastodon/utils/config';
|
|
|
|
|
2023-05-10 20:59:29 +10:00
|
|
|
import type { RootState } from '..';
|
2023-05-10 00:56:26 +10:00
|
|
|
|
|
|
|
interface AudioSource {
|
2023-05-10 03:02:12 +10:00
|
|
|
src: string;
|
|
|
|
type: string;
|
2023-05-10 00:56:26 +10:00
|
|
|
}
|
|
|
|
|
2024-01-08 21:57:40 +11:00
|
|
|
interface ActionWithMetaSound extends UnknownAction {
|
|
|
|
meta: { sound: string };
|
|
|
|
}
|
|
|
|
|
|
|
|
function isActionWithMetaSound(action: unknown): action is ActionWithMetaSound {
|
|
|
|
return (
|
|
|
|
isAction(action) &&
|
|
|
|
'meta' in action &&
|
|
|
|
typeof action.meta === 'object' &&
|
|
|
|
!!action.meta &&
|
|
|
|
'sound' in action.meta &&
|
|
|
|
typeof action.meta.sound === 'string'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-10 00:56:26 +10:00
|
|
|
const createAudio = (sources: AudioSource[]) => {
|
2017-05-11 00:58:54 +10:00
|
|
|
const audio = new Audio();
|
|
|
|
sources.forEach(({ type, src }) => {
|
|
|
|
const source = document.createElement('source');
|
|
|
|
source.type = type;
|
|
|
|
source.src = src;
|
|
|
|
audio.appendChild(source);
|
|
|
|
});
|
|
|
|
return audio;
|
2017-05-21 01:31:47 +10:00
|
|
|
};
|
2017-05-11 00:58:54 +10:00
|
|
|
|
2023-05-10 00:56:26 +10:00
|
|
|
const play = (audio: HTMLAudioElement) => {
|
2017-03-14 03:12:30 +11:00
|
|
|
if (!audio.paused) {
|
|
|
|
audio.pause();
|
2017-10-19 02:13:51 +11:00
|
|
|
if (typeof audio.fastSeek === 'function') {
|
|
|
|
audio.fastSeek(0);
|
|
|
|
} else {
|
2017-12-09 11:25:00 +11:00
|
|
|
audio.currentTime = 0;
|
2017-10-19 02:13:51 +11:00
|
|
|
}
|
2017-03-14 03:12:30 +11:00
|
|
|
}
|
|
|
|
|
2023-05-10 20:59:29 +10:00
|
|
|
void audio.play();
|
2017-03-14 03:12:30 +11:00
|
|
|
};
|
|
|
|
|
2024-01-08 21:57:40 +11:00
|
|
|
export const soundsMiddleware = (): Middleware<
|
|
|
|
Record<string, never>,
|
|
|
|
RootState
|
|
|
|
> => {
|
2023-07-13 19:49:16 +10:00
|
|
|
const soundCache: Record<string, HTMLAudioElement> = {};
|
2023-07-12 11:02:32 +10:00
|
|
|
|
|
|
|
void ready(() => {
|
|
|
|
soundCache.boop = createAudio([
|
2017-05-11 00:58:54 +10:00
|
|
|
{
|
2023-07-12 11:02:32 +10:00
|
|
|
src: `${assetHost}/sounds/boop.ogg`,
|
2017-05-11 00:58:54 +10:00
|
|
|
type: 'audio/ogg',
|
|
|
|
},
|
|
|
|
{
|
2023-07-12 11:02:32 +10:00
|
|
|
src: `${assetHost}/sounds/boop.mp3`,
|
2017-05-21 01:31:47 +10:00
|
|
|
type: 'audio/mpeg',
|
2017-05-11 00:58:54 +10:00
|
|
|
},
|
2023-07-12 11:02:32 +10:00
|
|
|
]);
|
|
|
|
});
|
2017-03-14 03:12:30 +11:00
|
|
|
|
2024-01-08 21:57:40 +11:00
|
|
|
return () => (next) => (action) => {
|
|
|
|
if (isActionWithMetaSound(action)) {
|
|
|
|
const sound = action.meta.sound;
|
2023-05-10 00:56:26 +10:00
|
|
|
|
2023-07-13 19:49:16 +10:00
|
|
|
if (sound && Object.hasOwn(soundCache, sound)) {
|
2023-05-10 20:59:29 +10:00
|
|
|
play(soundCache[sound]);
|
|
|
|
}
|
2024-01-08 21:57:40 +11:00
|
|
|
}
|
2017-03-14 03:12:30 +11:00
|
|
|
|
2024-01-08 21:57:40 +11:00
|
|
|
return next(action);
|
|
|
|
};
|
2023-05-10 00:56:26 +10:00
|
|
|
};
|