Recipe: persist and restore state

luastra/state · luastra/host · app.resolve · about 15 minutes

Save a versioned counter snapshot, restore it asynchronously, and reject malformed stored data without losing current state.

You will increment a counter, save it, change it again, then load the saved value. Reload the preview and load again to verify host persistence.

State.encode/decode is synchronous and deterministic; Host storage is asynchronous, so every request is correlated in app.resolve.

A successful storage read only proves that bytes were returned. Decode the versioned snapshot and validate domain values before replacing current application state.

Detail pages

1. Create the project2. Replace luastra.json3. Replace src/main.luau4. Replace tests/smoke.luau5. Check and run6. Understand and extend