-
Notifications
You must be signed in to change notification settings - Fork 32
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
Introduce self-deregistering closures for on / onany #48
Introduce self-deregistering closures for on / onany #48
Conversation
@SimonDanisch @piever I fixed the problem with observable pairs, it was just an example of code that actually did do something with the return value of Do you see any other potential problems with this change? Otherwise it would be great to add this soon, so I can use it to clean up some code in MakieLayout. One difficulty is with |
Ok, I think there should be no problem merging this! @shashi, do you have any comments? |
Looks good to me! |
otherwise it gets annoying to ensure that no error happens when a weak observable has `off` called before being finalized. much easier if off doesn't error
so because of an edge case where the user could call |
That change to |
This addresses a comment made in #48 (comment) by implementing storage for the ObserverFunctions added by `map`. This also adds optional precompilation support for observables, precompiling the methods based on the `T` in `Observable{T}`.
This addresses a comment made in #48 (comment) by implementing storage for the ObserverFunctions added by `map`. This also adds optional precompilation support for observables, precompiling the methods based on the `T` in `Observable{T}`.
This addresses a comment made in #48 (comment) by implementing storage for the ObserverFunctions added by `map`. This also adds optional precompilation support for observables, precompiling the methods based on the `T` in `Observable{T}`.
This addresses a comment made in #48 (comment) by implementing storage for the ObserverFunctions added by `map`. This also adds optional precompilation support for observables, precompiling the methods based on the `T` in `Observable{T}`.
We still have the problem that it's hard to keep track of the connections we make with Observables and to clean them up after use. This PR seeks to help while staying close enough to the previous API, that it hopefully doesn't break too much code using Observables.
The main change is that
on
returns a new typeObserverFunction
. This wraps the closureFunction
which is normally returned byon
. It stores the function, the observable that is being listened to, and aweak
flag. If theweak
flag is set viaon(..., weak = true)
, theObserverFunction
will calloff(observable, closure)
when finalized.This allows for some easy housekeeping. Imagine some large plotting object that creates multiple connections via
on
. We just have topush!
all weakObserverFunction
s into one big vector, and if our plot object goes out of scope, so will that vector and all the connections we made will be released.The
weak
flag is set tofalse
by default, which means that the behavior doesn't change from what we have now.Even with
weak = false
, this PR can make it a bit easier to clean up Observables, as theObserverFunctions
carry both the closure and related Observable, and it's therefore easier to calloff(observerfunc)
without having to find both of these parts first.I see almost no code storing the result of
on
in the wild right now, which would be necessary to do housekeeping (which is not being done). It would therefore be easy with this PR to improve code by storing the result and callingoff
on it at an appropriate time, or making the whole thingweak
and let scoping handle it. If preexisting code is storing the result ofon
, then the receiving structure is probably typedFunction
, and as I've madeObserverFunction <: Function
this should also not be breaking.CI is currently failing because of some problem with ObservablePairs. I have never used this and cannot understand from the code what they are supposed to do. Neither are they documented. So I assume they are not very important.