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've often come across the need to selectively trigger handlers of observables, e.g. only trigger the first one.
In the past I've worked around that with some weired constructions, but with the introduction of priorities this could be implemented quite easily and in a very clean way.
function Base.notify(@nospecialize(observable::AbstractObservable); priority::Union{Int, Function})
val = observable[]
for (p, f) in Observables.listeners(observable)::Vector{Pair{Int, Any}}
(priority isa Int ? p == priority :priority(p)) ||continue
result = Base.invokelatest(f, val)
if result isa Consume && result.x
# stop calling callbacks if event got consumedreturntrueendendreturnfalseend
Example
using Observables
o =Observable(1)
on(o) do o
println("Called with standard priority: $o")
endon(o, priority =-1) do r
println("Called with priority -1: $r")
endnotify(o, priority =-1);
# Called with priority -1: 1notify(o, priority =0);
# Called with standard priority: 1notify(o, priority =<(1));
# Called with standard priority: 1# Called with priority -1: 1
What do you think?
The text was updated successfully, but these errors were encountered:
I've often come across the need to selectively trigger handlers of observables, e.g. only trigger the first one.
In the past I've worked around that with some weired constructions, but with the introduction of priorities this could be implemented quite easily and in a very clean way.
Example
What do you think?
The text was updated successfully, but these errors were encountered: