mirror of
https://github.com/glitch-soc/mastodon.git
synced 2025-12-24 19:37:26 +00:00
[Glitch] Upgrade Redux packages
Port a0e237a96f to glitch-soc
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { Middleware, AnyAction } from 'redux';
|
||||
import { isAction } from '@reduxjs/toolkit';
|
||||
import type { Middleware, UnknownAction } from '@reduxjs/toolkit';
|
||||
|
||||
import ready from 'flavours/glitch/ready';
|
||||
import { assetHost } from 'flavours/glitch/utils/config';
|
||||
@@ -10,6 +11,21 @@ interface AudioSource {
|
||||
type: string;
|
||||
}
|
||||
|
||||
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'
|
||||
);
|
||||
}
|
||||
|
||||
const createAudio = (sources: AudioSource[]) => {
|
||||
const audio = new Audio();
|
||||
sources.forEach(({ type, src }) => {
|
||||
@@ -34,7 +50,10 @@ const play = (audio: HTMLAudioElement) => {
|
||||
void audio.play();
|
||||
};
|
||||
|
||||
export const soundsMiddleware = (): Middleware<unknown, RootState> => {
|
||||
export const soundsMiddleware = (): Middleware<
|
||||
Record<string, never>,
|
||||
RootState
|
||||
> => {
|
||||
const soundCache: Record<string, HTMLAudioElement> = {};
|
||||
|
||||
void ready(() => {
|
||||
@@ -50,15 +69,15 @@ export const soundsMiddleware = (): Middleware<unknown, RootState> => {
|
||||
]);
|
||||
});
|
||||
|
||||
return () =>
|
||||
(next) =>
|
||||
(action: AnyAction & { meta?: { sound?: string } }) => {
|
||||
const sound = action.meta?.sound;
|
||||
return () => (next) => (action) => {
|
||||
if (isActionWithMetaSound(action)) {
|
||||
const sound = action.meta.sound;
|
||||
|
||||
if (sound && Object.hasOwn(soundCache, sound)) {
|
||||
play(soundCache[sound]);
|
||||
}
|
||||
}
|
||||
|
||||
return next(action);
|
||||
};
|
||||
return next(action);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user