Skip to content

Commit 9bd5282

Browse files
committed
web gpu puzzles - save per-puzzle state, avoid deadlock on fast puzzle changes
1 parent 994b40f commit 9bd5282

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

experimental/fasthtml/gpu_puzzles/client.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const State = {
1111
wgslStatus: "",
1212
};
1313

14+
codeState = [];
15+
1416
const PuzzleSpec = [
1517
{
1618
name: "Map",
@@ -234,8 +236,15 @@ async function updateEditor() {
234236
}
235237
}
236238

237-
function update(event) {
239+
async function update(event) {
240+
241+
if (!AppState.isDispatchReady) {
242+
await waitForDispatchReady();
243+
}
238244
console.log("Updating");
245+
if (event.type == "selectPuzzle") {
246+
codeState[AppState.puzzleIndex] = AppState.editor.getValue();
247+
}
239248
if ((event.type === "selectPuzzle") & (event.value === "prev")) {
240249
AppState.puzzleIndex = AppState.puzzleIndex - 1;
241250
if (AppState.puzzleIndex < 0) {
@@ -257,7 +266,11 @@ function update(event) {
257266
if (event.type === "init" || event.type === "selectPuzzle") {
258267
// Reset editor template code if we are either starting the app for the
259268
// first time or picking a new puzzle
260-
AppState.editor.setValue(AppState.module.getTemplate(AppState.puzzleIndex));
269+
if (codeState[AppState.puzzleIndex]) {
270+
AppState.editor.setValue(codeState[AppState.puzzleIndex]);
271+
} else {
272+
AppState.editor.setValue(AppState.module.getTemplate(AppState.puzzleIndex));
273+
}
261274
}
262275

263276
updateEditor();

experimental/fasthtml/gpu_puzzles/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def controls():
3333
Div(
3434
"Puzzle 1: Map",
3535
id="puzzle_name",
36-
style="font-size: 1.5rem; width: 15vw; font-weight: bold;"
36+
style="font-size: 1.5rem; width: 25vw; font-weight: bold;"
3737
),
3838
Button(
3939
">>",

0 commit comments

Comments
 (0)