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 changes() #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 18 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, async_latest, throttle, changes
export Consume, ObserverFunction, AbstractObservable

import Base.Iterators.filter
Expand Down Expand Up @@ -572,6 +572,23 @@ Observable(3)
return obs
end

"""
changes(obs)

Returns an `Observable` that only forwards `obs` updates when its value changes.
"""
function changes(obs::AbstractObservable{T}) where {T}
result = Observable{T}(obs[])
oldobs = Observable{T}(obs[])
on(obs) do val
if val != oldobs[]
result[] = val
oldobs[] = val
end
end
return result
end


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