From 6fd42b4cb018ed867158bd766e5dddfa1e8ceb0c Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Wed, 7 Aug 2024 09:15:06 +0200 Subject: [PATCH] add changes() --- src/Observables.jl | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Observables.jl b/src/Observables.jl index 47af289..514cbd8 100644 --- a/src/Observables.jl +++ b/src/Observables.jl @@ -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 @@ -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)