patching for smoother experience

This commit is contained in:
Dylan Pizzo
2026-06-11 16:52:15 -04:00
parent 064528b178
commit 6313364ba4
+29 -1
View File
@@ -15,6 +15,22 @@ type Game = {
carts: Parameters<typeof Pico8Player>["0"]["carts"]; carts: Parameters<typeof Pico8Player>["0"]["carts"];
}; };
const getPatch = (before: number[], after: number[]) => {
const diff: Record<number, number> = {};
for (const i in after) {
if (after[i] !== before[i]) {
diff[i] = after[i];
}
}
return diff;
};
const applyPatch = (before: number[], patch: Record<number, number>) => {
for (const i in patch) {
before[i] = patch[i];
}
};
export const GamePage = () => { export const GamePage = () => {
const { author, slug } = useParams(); const { author, slug } = useParams();
// const [text, setText] = useState(""); // const [text, setText] = useState("");
@@ -46,6 +62,16 @@ export const GamePage = () => {
} }
} }
} }
if (msg.gpioPatch) {
if (picoRef.current) {
const handle = picoRef.current;
if (handle) {
// console.log("updating pico gpio");
applyPatch(handle.gpio, msg.gpioPatch);
setPrevGpio([...handle.gpio]);
}
}
}
}, },
}); });
const [game, setGame] = useState<Game | null>(null); const [game, setGame] = useState<Game | null>(null);
@@ -70,7 +96,9 @@ export const GamePage = () => {
if (JSON.stringify(handle.gpio) !== JSON.stringify(prevGpio)) { if (JSON.stringify(handle.gpio) !== JSON.stringify(prevGpio)) {
if (prevGpio) { if (prevGpio) {
setPrevGpio([...handle.gpio]); setPrevGpio([...handle.gpio]);
socket.sendMessage({ gpio: handle.gpio }); socket.sendMessage({
gpioPatch: getPatch(prevGpio, handle.gpio),
});
} else { } else {
socket.sendMessage({ getGpio: true }); socket.sendMessage({ getGpio: true });
setPrevGpio([...handle.gpio]); setPrevGpio([...handle.gpio]);