dphfox / Fusion

A modern reactive UI library, built specifically for Roblox and Luau.
https://elttob.uk/Fusion/
MIT License
530 stars 91 forks source link

Share observer objects for the same state #309

Closed boatbomber closed 5 months ago

boatbomber commented 5 months ago

Instead of creating multiple Observers for a single watched state, cache them. Because the cache is a hard ref, it replaces the hardRef set from before.

Note that this has one change to behavior: if you create an Observer but never call :onChange, it will never clean up. This is because we create the hardRef upon init, so that objects can be shared. Cleanup still occurs when all change listeners are disconnected, but if you never create any to disconnect that can't occur. However, I don't see this being an issue since Observers are never created without using their one and only purpose of connecting :onChange.

boatbomber commented 5 months ago

This is incompatible with the upcoming scopes. Further work will need to be done at a later time.