Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change design of Observer to allow pooling listeners #310

Open
boatbomber opened this issue Jan 29, 2024 · 1 comment
Open

Change design of Observer to allow pooling listeners #310

boatbomber opened this issue Jan 29, 2024 · 1 comment
Labels
enhancement New feature or request not ready - blocked Waiting on other work to be done

Comments

@boatbomber
Copy link
Contributor

Instead of creating a new Observer every time, there should be some form of sharing whenever possible. If two Observers are watching the same state, it should really just be one Observer with two listener callbacks.

@boatbomber boatbomber added enhancement New feature or request not ready - evaluating Currently gauging feedback labels Jan 29, 2024
@dphfox
Copy link
Owner

dphfox commented Jan 29, 2024

I'm going to hijack this issue with a radical idea; what if we got rid of Observer entirely as a user-facing construct? Nobody actually uses them for anything except immediately piping it into an :onChange() or :onBind() call.

Things have changed a lot since 0.1 when this API surface was first introduced. Now that we have scopes, we've externalised all destruction tracking, so we can append code into the current scope to handle disconnecting the listener. Beyond that, what purpose does Observer as a public-facing construct serve?

So my proposition is this; drop the API entirely, and replace Observer with a function, observe() or onChange() or something like that. As far as the user cares, those functions connect the callback to be run in a new coroutine at some point soon after an update occurs. Maybe it also provides some nice options to track multiple state objects or run the callback immediately to match onBind - I don't know, but all that matters is there's no object involved.

Under the hood, Fusion can now internally manage whatever reactive graph mechanisms it needs in order to dispatch those listeners. Fusion can count the number of listeners so that it destroys whatever internal objects its using once all listeners are disconnected.

From there, observe() becomes the foundation atop which we can build other APIs, like perhaps a general connector for binding instances to properties, which can then be used in applyInstanceProps (#304)

@dphfox dphfox changed the title Pool Observers when watching the same state Change design of Observer to allow pooling listeners Jan 29, 2024
@dphfox dphfox added ready to work on Enhancements/changes ready to be made and removed not ready - evaluating Currently gauging feedback labels Feb 3, 2024
@dphfox dphfox added this to Fusion 0.3 Feb 3, 2024
@dphfox dphfox moved this to To Do in Fusion 0.3 Feb 3, 2024
@dphfox dphfox added not ready - blocked Waiting on other work to be done and removed ready to work on Enhancements/changes ready to be made labels Feb 3, 2024
@dphfox dphfox removed this from Fusion 0.3 Jul 3, 2024
@dphfox dphfox moved this to Todo in Fusion - Road to 1.0 Feb 1, 2025
HappySunChild added a commit to HappySunChild/ConFusion that referenced this issue Feb 16, 2025
This change was mostly inspired by this issue, dphfox/Fusion#310

I totally agree with Observers being *completely* redundant, and for the most part unneeded, because most the time when you're creating an Observer it's just to pipe it straight into a `:onBind()` or `:onChange()` call. I've replaced Observers with a function simply called `observe()` which takes the target and the callback as arguments and returns a disconnect function. It adds a set inside the target that holds all of the callbacks and when that target value is evaluate it calls all of those observe tasks, essentially removing the Observer "middleman".

(I know I'm not good at describing these things, you can just look at the changes yourself to see what I mean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request not ready - blocked Waiting on other work to be done
Projects
Development

No branches or pull requests

2 participants