Open twiddlingbits opened 1 month ago
I'm not fully sure how it could be made generic. As you said, one approach would be to just store all of the relevant variables as URL parameters, but I'm not quite sure how to allow the programmer to set those and load them generically other than setting up some list of pointers.
Alternatively, there could be "menus" or "scenes" in the widget library. Then each scene just needs a unique name and some mechanism for skipping to specific scenes. Maybe using some sort of event-based model? The init phase registers the objects in a scene, then events are called when things happen. That way the "root" can have a function called after the initial setup to move into the desired scene.
I'm not fully sure how it could be made generic.
Or a "state snapshot". I wouldn't stress over this. If we keep it in the back of our head, maybe something will come to light as we work though the API. Many (most) things take iteration and evolution to get good.
I've been thinking of some possible implementations for this. I think the easiest might be something like a model, view, controller paradigm. Where the persistent state is stored in the model, the controller would be things like events, and the view would handle the rendering. The main problems would be serialization, deserialization, and dealing with mixed persistent/non-persistent state.
Serialization/Deserialization: The main problem here is telling whatever is actually writing the state how it should be formatted as URL params. To start off with, since they're basically acting as globals, they would all need to be unique. So having two menus open at the same time could cause issues. Alternatively, the program could just use a single URL param with the relevant C structures just directly encoded as base 64. Either way, I'm not quite sure how to describe it in a way it can be set and restored. I'm thinking of two different approaches. The first one would be to have sort of global registry either indexed by number or string that can store a set of primitives (like numbers, arrays, strings, etc.). The main problem would be dealing with conflicting names from different parts of the hierarchy or from other programs. I think it might be workable with something like a registry "hierarchy" where it can automatically rename things as needed. Alternatively, there could just be some state param where the entire state is just encoded into ASCII using something like base64. With this, you could do something similar to the above without needing to worry about names, or possibly something more complex. I think it would be nice if it could just directly encode arbitrary data like structs, but I feel like that could get really complicated if you allowed things like pointers.
Mixed persistent/non-persistent state: I'm not 100% sure how much of an issue this will be depending on how things are setup, but here's my thinking on the problem: If you have a game like pong or maybe some sort of sound editor you probably won't want a lot of it to be persistent. Or, if you do, you likely want it in something like [client-side storage)[https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Client-side_storage] that can handle a larger amount of data being stored than just the URL params. How would a "stateful" program/API drop into something like this? Would there just be some init function that can be called to initialize stateless parts so they can be called or would it be handled some other way?
I was thinking of posting a link to Pong to the discord wasm demos section. But i was thinking I would like to paste a link to the Pong 2 Player AI version (and skip the intro menu, when this demo link is used). For example, i sorted of wanted to select the 2 Player AI mode, than copy the link in the address bar (but of course the link didn't change when i selected the 2P AI menu). How would we do that in a general way? Is it just an app feature (you could easily add the code to your index.html to look at the #Pong2AI (like we do for #async sometimes). But is there a more generic feature?
It seems like it might be a feature of your button widget library.
Or, more generically, a feature of a UI library. For example, one might have "screens" that can be deep linked to. Or one might have various UI status adjust the URL in the title bar so that they contain all the state info in the link so that a copy/paste of the link can deep link to that state.