Files
CHOMPStation2/tgui/packages/tgui/assets.ts
CHOMPStation2 eebf92d66f [MIRROR] TGUI 5.0 Patch 1 (#7701)
Co-authored-by: Selis <sirlionfur@hotmail.de>
2024-02-08 15:31:06 +01:00

46 lines
1.2 KiB
TypeScript

/**
* @file
* @copyright 2020 Aleksej Komarov
* @license MIT
*/
import { Dispatch } from 'common/redux';
import { Action, AnyAction, Middleware } from '../common/redux';
const EXCLUDED_PATTERNS = [/v4shim/i];
const loadedMappings: Record<string, string> = {};
export const resolveAsset = (name: string): string =>
loadedMappings[name] || name;
export const assetMiddleware: Middleware =
(storeApi) =>
<ActionType extends Action = AnyAction>(next: Dispatch<ActionType>) =>
(action: ActionType) => {
const { type, payload } = action as AnyAction;
if (type === 'asset/stylesheet') {
Byond.loadCss(payload);
return;
}
if (type === 'asset/mappings') {
for (const name of Object.keys(payload)) {
// Skip anything that matches excluded patterns
if (EXCLUDED_PATTERNS.some((regex) => regex.test(name))) {
continue;
}
const url = payload[name];
const ext = name.split('.').pop();
loadedMappings[name] = url;
if (ext === 'css') {
Byond.loadCss(url);
}
if (ext === 'js') {
Byond.loadJs(url);
}
}
return;
}
next(action);
};