framesurge / perseus

A state-driven web development framework for Rust with full support for server-side rendering and static generation.
https://framesurge.sh/perseus/en-US
MIT License
2.15k stars 89 forks source link

Refactor global state #223

Closed arctic-hen7 closed 1 year ago

arctic-hen7 commented 1 year ago

This PR makes the breaking change of making global state accessible through a function .get_global_state() on the RenderCtx, removing it from function parameters. This makes the macros substantially simpler, reduces the number of internal functions that have to be public, and simplifies the process of managing global state. It also advances the reactive state platform to tie reference structs to their intermediaries, which sets the stage for better state import ergonomics (right now, you have to import some shadow types with a * import).

This will also make the macros that handle capsules (#4) much simpler, since they will need to support custom component-level properties, and this reduces the number of cases they'll need to handle. In other words, this makes my job a lot easier over the next month or so!

I understand that not everyone is going to be happy with this change, since accessing global state as a third page parameter is very convenient. However, this should actually make your app slightly smaller and faster, and it will improve long-term ergonomics substantially with the capsules system. It also makes it clearer that global state is not tied to any page, while eliminating the horrific pattern of having () as page state when you wanted to access the global state on a page without any state of its own.

arctic-hen7 commented 1 year ago

I'll note here as a reminder to myself that this makes it slightly easier to create a BorrowMut panic in an app if one were to directly access the global state, avoiding the wrappers Perseus provides. Why you would ever do that, I don't know. I'll probably make a few more methods private to make this impossible (since advanced thawing logic is already handled by ThawPrefs).

arctic-hen7 commented 1 year ago

Snap store seems to be broken in CI for downloading Firefox for the E2E tests, so I'm running it all locally before merging...success!