From 01bf335a9ae20bb134f263ca507233ec4e9d1739 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Sun, 10 Jul 2022 14:04:50 -0400 Subject: [PATCH] Use real variables to backup 'state' variables which keep losing state --- main.py | 2 +- src/index.tsx | 133 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 106 insertions(+), 29 deletions(-) diff --git a/main.py b/main.py index 054714f..df431b0 100644 --- a/main.py +++ b/main.py @@ -5,7 +5,7 @@ import asyncio import pathlib import subprocess -VERSION = "0.7.0-rc1" +VERSION = "0.7.0" HOME_DIR = "/home/deck" DEFAULT_SETTINGS_LOCATION = HOME_DIR + "/.config/powertools/default_settings.json" LOG_LOCATION = "/tmp/powertools.log" diff --git a/src/index.tsx b/src/index.tsx index 2bfad61..7c1f33f 100755 --- a/src/index.tsx +++ b/src/index.tsx @@ -29,12 +29,24 @@ import * as python from "./python"; // } var firstTime: boolean = true; -var versionGlobal: string = "0.0.0-jank"; +var versionGlobalHolder: string = "0.0.0-jank"; var periodicHook: NodeJS.Timer | null = null; var lastGame: string = ""; var lifetimeHook: any = null; var startHook: any = null; +var smt_backup: boolean = true; +var cpus_backup: number = 8; +var boost_backup: boolean = true; +var freq_backup: number = 8; +var slowPPT_backup: number = 1; +var fastPPT_backup: number = 1; +var chargeNow_backup: number = 5200000; +var chargeFull_backup: number = 5200000; +var chargeDesign_backup: number = 5200000; +var persistent_backup: boolean = false; +var perGameProfile_backup: boolean = false; + var reload = function(){}; const Content: VFC<{ serverAPI: ServerAPI }> = ({serverAPI}) => { @@ -55,22 +67,83 @@ const Content: VFC<{ serverAPI: ServerAPI }> = ({serverAPI}) => { python.setServer(serverAPI); - const [smtGlobal, setSMT] = useState(true); - const [cpusGlobal, setCPUs] = useState(8); - const [boostGlobal, setBoost] = useState(true); + const [smtGlobal, setSMT_internal] = useState(smt_backup); + const setSMT = (value: boolean) => { + smt_backup = value; + setSMT_internal(value); + }; - const [freqGlobal, setFreq] = useState(8); + const [cpusGlobal, setCPUs_internal] = useState(cpus_backup); + const setCPUs = (value: number) => { + cpus_backup = value; + setCPUs_internal(value); + }; - const [slowPPTGlobal, setSlowPPT] = useState(1); - const [fastPPTGlobal, setFastPPT] = useState(1); + const [boostGlobal, setBoost_internal] = useState(boost_backup); + const setBoost = (value: boolean) => { + boost_backup = value; + setBoost_internal(value); + }; - const [chargeNowGlobal, setChargeNow] = useState(40); - const [chargeFullGlobal, setChargeFull] = useState(40); - const [chargeDesignGlobal, setChargeDesign] = useState(40); + const [freqGlobal, setFreq_internal] = useState(freq_backup); + const setFreq = (value: number) => { + freq_backup = value; + setFreq_internal(value); + }; - const [persistGlobal, setPersist] = useState(false); - const [perGameProfileGlobal, setPerGameProfile] = useState(false); - const [gameGlobal, setGame] = useState(lastGame); + const [slowPPTGlobal, setSlowPPT_internal] = useState(slowPPT_backup); + const setSlowPPT = (value: number) => { + slowPPT_backup = value; + setSlowPPT_internal(value); + }; + + const [fastPPTGlobal, setFastPPT_internal] = useState(fastPPT_backup); + const setFastPPT = (value: number) => { + fastPPT_backup = value; + setFastPPT_internal(value); + }; + + const [chargeNowGlobal, setChargeNow_internal] = useState(chargeNow_backup); + const setChargeNow = (value: number) => { + chargeNow_backup = value; + setChargeNow_internal(value); + }; + + const [chargeFullGlobal, setChargeFull_internal] = useState(chargeFull_backup); + const setChargeFull = (value: number) => { + chargeFull_backup = value; + setChargeFull_internal(value); + }; + + const [chargeDesignGlobal, setChargeDesign_internal] = useState(chargeDesign_backup); + const setChargeDesign = (value: number) => { + chargeDesign_backup = value; + setChargeDesign_internal(value); + }; + + const [persistGlobal, setPersist_internal] = useState(persistent_backup); + const setPersist = (value: boolean) => { + persistent_backup = value; + setPersist_internal(value); + }; + + const [perGameProfileGlobal, setPerGameProfile_internal] = useState(perGameProfile_backup); + const setPerGameProfile = (value: boolean) => { + perGameProfile_backup = value; + setPerGameProfile_internal(value); + }; + + const [gameGlobal, setGame_internal] = useState(lastGame); + const setGame = (value: string) => { + lastGame = value; + setGame_internal(value); + }; + + const [versionGlobal, setVersion_internal] = useState(versionGlobalHolder); + const setVersion = (value: string) => { + versionGlobalHolder = value; + setVersion_internal(value); + }; reload = function () { python.execute(python.onViewReady()); @@ -87,20 +160,6 @@ const Content: VFC<{ serverAPI: ServerAPI }> = ({serverAPI}) => { python.resolve(python.getPerGameProfile(), setPerGameProfile); }; - if (periodicHook == null) { - periodicHook = setInterval(function() { - python.resolve(python.getCurrentGame(), (game: string) => { - python.resolve(python.getChargeNow(), setChargeNow); - if (lastGame != game) { - setGame(game); - lastGame = game; - reload(); - } - python.resolve(python.getChargeFull(), setChargeFull); - }); - }, 1000); - } - if (firstTime) { firstTime = false; @@ -113,9 +172,26 @@ const Content: VFC<{ serverAPI: ServerAPI }> = ({serverAPI}) => { python.resolve(python.getCurrentGame(), setGame); - python.resolve(python.getVersion(), (v: string) => {versionGlobal = v;}); + python.resolve(python.getVersion(), setVersion); } + if (periodicHook != null) { + clearInterval(periodicHook); + periodicHook = null; + } + + periodicHook = setInterval(function() { + python.resolve(python.getChargeFull(), setChargeFull); + python.resolve(python.getChargeNow(), setChargeNow); + python.resolve(python.getCurrentGame(), (game: string) => { + if (lastGame != game) { + setGame(game); + lastGame = game; + reload(); + } + }); + }, 1000); + const FieldWithSeparator = joinClassNames(gamepadDialogClasses.Field, gamepadDialogClasses.WithBottomSeparatorStandard); return ( @@ -364,6 +440,7 @@ export default definePlugin((serverApi: ServerAPI) => { onDismount() { console.log("PowerTools shutting down"); clearInterval(periodicHook!); + periodicHook = null; lifetimeHook!.unregister(); startHook!.unregister(); serverApi.routerHook.removeRoute("/decky-plugin-test");