GlosSITweaks: make linter happy

pull/239/head
Peter Repukat 1 year ago
parent 50a4ea814d
commit ffdb227d2b

@ -1,9 +1,15 @@
import type { SteamConfig } from './common/util/types'; import type { SteamConfig } from './common/util/types';
import { fetchWithTimeout } from './common/util/util'; import { fetchWithTimeout } from './common/util/util';
import type { GlosSISettings } from './@types/GlosSISettings';
class SteamTargetApi { class SteamTargetApi {
public getGlosSIActive() {
return fetchWithTimeout('http://localhost:8756/', { timeout: 500 })
.then(
() => true
).catch(() => false);
}
public getSteamSettings(): Promise<SteamConfig> { public getSteamSettings(): Promise<SteamConfig> {
return fetch('http://localhost:8756/steam_settings') return fetch('http://localhost:8756/steam_settings')
.then( .then(
@ -13,12 +19,15 @@ class SteamTargetApi {
); );
} }
public getGlosSIActive() { public getGlosSISettings() {
return fetchWithTimeout('http://localhost:8756/', { timeout: 10000 }) return fetch('http://localhost:8756/settings')
.then( .then(
() => true (res) => res.json().then(
).catch((e) => false); (json) => json as GlosSISettings
)
);
} }
} }
@ -27,18 +36,19 @@ class GlosSIApiCtor {
} }
interface GlosSITweaks { interface GlosSITweaks {
[tweakName: string]: { readonly install: () => unknown; readonly uninstall?: () => void } [tweakName: string]: { readonly install: () => unknown; readonly uninstall?: () => void };
} }
declare global { declare global {
interface Window { interface Window {
GlosSITweaks: GlosSITweaks GlosSITweaks: GlosSITweaks;
GlosSIApi: InstanceType<typeof GlosSIApiCtor>; GlosSIApi: InstanceType<typeof GlosSIApiCtor>;
} }
// eslint-disable-next-line // eslint-disable-next-line
const GlosSIApi: InstanceType<typeof GlosSIApiCtor>; const GlosSIApi: InstanceType<typeof GlosSIApiCtor>;
const GlosSITweaks: GlosSITweaks // eslint-disable-next-line
const GlosSITweaks: GlosSITweaks;
} }
@ -51,7 +61,7 @@ const installGlosSIApi = () => {
}, },
uninstall: () => { uninstall: () => {
Object.entries(window.GlosSITweaks) Object.entries(window.GlosSITweaks)
.filter(([tweakName, obj]) => (tweakName !== 'GlosSI')) .filter(([tweakName]) => (tweakName !== 'GlosSI'))
.forEach(([, obj]) => obj.uninstall?.()); .forEach(([, obj]) => obj.uninstall?.());
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
@ -66,15 +76,15 @@ const installGlosSIApi = () => {
const glossiCheckInterval = setInterval(() => { const glossiCheckInterval = setInterval(() => {
if (window.GlosSIApi) { if (window.GlosSIApi) {
window.GlosSIApi.SteamTarget.getGlosSIActive().then((active) => { void window.GlosSIApi.SteamTarget.getGlosSIActive().then((active) => {
if (!active) { if (!active) {
window?.GlosSITweaks?.GlosSI?.uninstall?.(); window?.GlosSITweaks?.GlosSI?.uninstall?.();
} }
}); });
return; return;
} }
clearTimeout(glossiCheckInterval) clearTimeout(glossiCheckInterval);
}, 5000) }, 5000);
}; };

@ -1,6 +1,5 @@
import type { SteamConfig } from '../../../common/util/types';
import type { SteamConfig } from "../../../common/util/types"; import { initTweak } from '../../../common/tweakApi';
import { initTweak } from "../../../common/tweakApi";
const backup: { originalFpsCorner?: number } = {}; const backup: { originalFpsCorner?: number } = {};
@ -20,4 +19,6 @@ initTweak('HideFPSCounter', {
SteamClient.Settings.SetInGameOverlayShowFPSCorner((backup.originalFpsCorner ?? 0) as 0 | 1 | 2 | 3 | 4); SteamClient.Settings.SetInGameOverlayShowFPSCorner((backup.originalFpsCorner ?? 0) as 0 | 1 | 2 | 3 | 4);
}, 10 * 1000); }, 10 * 1000);
} }
}); }).then(() => {
console.log('HideFPSCounter installed');
}).catch((e) => console.error('HideFPSCounter failed to install', e));

@ -2,9 +2,9 @@
export const initTweak = <T>(name: string, tweakMain: (() => T)|{ export const initTweak = <T>(name: string, tweakMain: (() => T)|{
install: () => T; install: () => T;
uninstall: () => void; uninstall: () => void;
}, force = false): T|Error => { }, force = false): T => {
if (!force && window.GlosSITweaks[name]) { if (!force && window.GlosSITweaks[name]) {
return new Error(`Tweak ${name} is already installed!`); throw new Error(`Tweak ${name} is already installed!`);
} }
if (typeof tweakMain === 'object') { if (typeof tweakMain === 'object') {

@ -1,12 +1,12 @@
export const fetchWithTimeout = async (input: RequestInfo | URL, init?: RequestInit & { timeout: number }) => { export const fetchWithTimeout = async (input: RequestInfo | URL, init?: RequestInit & { timeout: number }) => {
const { timeout = 8000 } = init || {}; const { timeout = 8000 } = init || {};
const controller = new AbortController(); const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout); const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(input, { const response = await fetch(input, {
...(init ||{}), ...(init ||{}),
signal: controller.signal signal: controller.signal
}); });
clearTimeout(id); clearTimeout(id);
return response; return response;
} };

Loading…
Cancel
Save