You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a project where I need to remove listeners from many observables.
Sometimes it's about removing all, sometimes about removing all except the first one.
I don't want to keep track of all the observer functions so I wondered whether it was possible to remove the listeners by index.
Hence I came up with the follwing code.
export off!, nlistener
nlistener(@nospecialize(o::Observable)) =length(Observables.listeners(o))
functionoff!(@nospecialize(o::Observables.AbstractObservable), index::Union{AbstractRange{<:Integer}, Vector{<:Integer}})
allunique(index) || (@info("All indices must be distinct"); returnBitVector(zeros(len)))
len =length(index)
callbacks = Observables.listeners(o)
success =BitVector(zeros(len))
for (i, n) inenumerate(reverse(sort(index)))
if0< n <=length(callbacks)
for g in Observables.removehandler_callbacks
g(observable, callbacks[n])
enddeleteat!(callbacks, n)
success[len - i +1] =trueendend
success
endoff!(@nospecialize(o::Observables.AbstractObservable), index::Integer) =off!(o, [index])[1]
off!(@nospecialize(o::Observables.AbstractObservable)) =off!(o, 1:length(Observables.listeners(o)))
Does that look like something you'd like to integrate in Observables?
If so, please let me know. I'd be happy to submit a PR.
The text was updated successfully, but these errors were encountered:
hhaensel
changed the title
Feature request: off(o::AbstractObservable) off(o, AbstractObservable, index)
Feature request: off(o::AbstractObservable) off(o::AbstractObservable, index)
Jul 4, 2022
Hi everyone,
I have a project where I need to remove listeners from many observables.
Sometimes it's about removing all, sometimes about removing all except the first one.
I don't want to keep track of all the observer functions so I wondered whether it was possible to remove the listeners by index.
Hence I came up with the follwing code.
Does that look like something you'd like to integrate in Observables?
If so, please let me know. I'd be happy to submit a PR.
The text was updated successfully, but these errors were encountered: