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

add onall #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Observables"
uuid = "510215fc-4207-5dde-b226-833fc4488ee2"
version = "0.5.1"
version = "0.5.2"

[compat]
julia = "1.6"
Expand Down
34 changes: 33 additions & 1 deletion src/Observables.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Observables

export Observable, on, off, onany, connect!, obsid, async_latest, throttle
export Observable, on, off, onany, onall, connect!, obsid, async_latest, throttle
export Consume, ObserverFunction, AbstractObservable

import Base.Iterators.filter
Expand Down Expand Up @@ -348,6 +348,38 @@ function onany(f, args...; weak::Bool = false, priority::Int=0)
return obsfuncs
end


onall(f) = error("onall needs at least two observables")
onall(f, obs1) = error("onall needs at least two observables")

"""
onall(f, args...)

Calls `f` on updates to all observable refs in `args`.
`f` is called only if all (i.e. not any) observable refs in `args` are updated at least once.
yakir12 marked this conversation as resolved.
Show resolved Hide resolved
`args` may contain any number of `Observable` objects.
`f` will be passed the values contained in the refs as the respective argument.
All other objects in `args` are passed as-is.

See also: [`on`](@ref).
"""
function onall(f, observables...; condition=(old, new)-> true)
updated = fill(false, length(observables))
for (i, observable) in enumerate(observables)
old = observable[]
on(observable) do new_value
if condition(old, new_value)
updated[i] = true
if all(updated)
f(to_value.(observables)...)
fill!(updated, false)
end
end
old = new_value
end
end
end

"""
map!(f, observable::AbstractObservable, args...; update::Bool=true)

Expand Down
19 changes: 16 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ end
r[] = 3 # shouldn't call test
end

@testset "onany and map" begin
@testset "onany, onall, and map" begin
r = Observable(0)
tested = Ref(false)
onany(1, r) do x, y
Expand All @@ -178,6 +178,19 @@ end
r[] = 2
@test tested[]

r1 = Observable(0)
r2 = Observable(0)
tested = Ref(false)
onall(r1, r2) do x, y
@test x == 1
@test y == 2
tested[] = true
end
r1[] = 1
@test !tested[]
r2[] = 2
@test tested[]

r1 = Observable(0)
r2 = Observable(0)
map!(x->x+1, r2, r1)
Expand Down Expand Up @@ -339,14 +352,14 @@ end

o = Observable(0)
cnt[] = 0
function compute_something(x)
function compute_something_else(x)
for i=1:10^8; rand() end
cnt[] = cnt[] + 1
end
sleep(1)
o_latest = async_latest(o, 3)

on(compute_something, o_latest) # compute something on the latest update
on(compute_something_else, o_latest) # compute something on the latest update
for i=1:5
o[] = i
end
Expand Down