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

Update on value change #68

Merged
merged 4 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 42 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, connect!, obsid, observe_changes, async_latest, throttle

import Base.Iterators.filter

Expand Down Expand Up @@ -477,6 +477,47 @@ Observable{$Int} with 0 listeners. Value:
map!(f, Observable(f(arg1[], map(to_value, args)...)), arg1, args...; update=false)
end

"""
obs = observe_changes(arg::AbstractObservable, eq=(==))

Returns an `Observable` which updates with the value of `arg` whenever the new value
differs from the current value of `obs` according to the equality operator `eq`.

# Example:
```
julia> obs = Observable(0);

julia> obs_change = observe_changes(obs);

julia> on(obs) do o
println("obs[] == \$o")
end;

julia> on(obs_change) do o
println("obs_change[] == \$o")
end;

julia> obs[] = 0;
obs[] == 0

julia> obs[] = 1;
obs_change[] == 1
obs[] == 1

julia> obs[] = 1;
obs[] == 1
```
"""
function observe_changes(obs::AbstractObservable{T}, eq=(==)) where T
out = Observable{T}(obs[])
on(obs) do val
if !eq(val, out[])
out[] = val
end
end
out
end

"""
async_latest(observable::AbstractObservable, n=1)

Expand Down
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,29 @@ end
@test c[] == 103
end

@testset "observe_changes" begin
o = Observable(0)
changes = observe_changes(o)
changes_approx = observe_changes(o, (x,y) -> abs(x-y) < 2)
@test eltype(changes) == eltype(changes_approx) == Int
@test changes[] == changes_approx[] == 0

values = Int[]
values_approx = Int[]
on(changes) do v
push!(values, v)
end
on(changes_approx) do v
push!(values_approx, v)
end

for i in 1:100
o[] = floor(Int, i/10)
end
@test values == 1:10
@test values_approx == 2:2:10
end

@testset "async_latest" begin
o = Observable(0)
cnt = Ref(0)
Expand Down