DrSensor / nusa

incremental runtime that bring both simplicity and power into webdev (buildless, cross-language, data-driven)
MIT License
4 stars 0 forks source link

Resumability a.k.a data snapshot #15

Open DrSensor opened 2 years ago

DrSensor commented 2 years ago

Not to be confused with Qwik Resumability 😅

Well, the idea is that all changes in class property accessor will write the data into a snapshot. The snapshot is stored into persisted medium either at debounced eventful write or at regular interval when in @frameloop. To avoid the cost of serialization, the change should be incremental. Storing the snapshot should only cost concatenation + casting the value into string (or maybe TLV format) nothing! (thanks to structuredClone() algorithm in IndexedDB). The real cost is when you load then parse the snapshot at startup.

<script data-store="inline" src="https://esm.run/wiles/render-scope.js"></script>

<render-scope data-store="localStorage">
  <script type="module" data-resume src="countdown.js"></script>
</render-scope>

<render-scope data-resume>
  <script type="module" src="calculator.js"></script>
</render-scope>

This bring several benefits:

  1. No need for hot reloading the ES Module (bye-bye webpack and vite 👋). You just need to reload the page.
  2. Save game state as html. The experience might be similar with "Quick save states" in Gameboy emulator. not possible since it only stored in IndexedDB
  3. State synchronization across client<->client or clients<->server. This is similar on how ECS being utilized in multiplayer game. However, it only need the snapshot, no need to store that snapshot into persisted medium. Most likely has similar approach on how PouchDB replication works.

Relevant:

TODO - [ ] implement class IncrementalJsonWriter extends Map - [ ] implement generic snapshot (possible medium: localStorage, sessionStorage, inline inside html page)

DrSensor commented 1 year ago

It's going to be stored only on IndexedDB because it support any structured clone object. So no need for serialization like JSON.stringify(), TLV buffer, or whatever.