Upgrade Redux packages (#28585)

This commit is contained in:
Renaud Chaput 2024-01-08 11:57:40 +01:00 committed by GitHub
commit a0e237a96f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 167 additions and 104 deletions

View file

@ -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 'mastodon/ready';
import { assetHost } from 'mastodon/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);
};
};