Skip to content

Commit

Permalink
Add Latest operator to Ref and Atom Rx support
Browse files Browse the repository at this point in the history
  • Loading branch information
louthy committed Nov 18, 2024
1 parent 69cea8f commit ab4f955
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions LanguageExt.Rx/Atom.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ public static IObservable<A> OnChange<A>(this Atom<A> atom) =>
add => atom.Change += new AtomChangedEvent<A>(add),
remove => atom.Change -= new AtomChangedEvent<A>(remove));

/// <summary>
/// Creates an observable that watches for changes to the `Atom`, but
/// also gets primed with the initial state on subscription, so there's
/// always at least one value that flows downstream.
/// </summary>
/// <param name="atom">Atom to observe</param>
/// <typeparam name="A">Value type</typeparam>
/// <returns>Observable atom</returns>
[Pure]
public static IObservable<A> Latest<A>(this Atom<A> atom) =>
Observable.Return(atom.Value)
.Merge(atom.OnChange());

/// <summary>
/// Observe changes to the `Atom`
/// </summary>
Expand All @@ -31,6 +44,19 @@ public static IObservable<A> OnChange<M, A>(this Atom<M, A> atom) =>
add => atom.Change += new AtomChangedEvent<A>(add),
remove => atom.Change -= new AtomChangedEvent<A>(remove));

/// <summary>
/// Creates an observable that watches for changes to the `Atom`, but
/// also gets primed with the initial state on subscription, so there's
/// always at least one value that flows downstream.
/// </summary>
/// <param name="atom">Atom to observe</param>
/// <typeparam name="A">Value type</typeparam>
/// <returns>Observable atom</returns>
[Pure]
public static IObservable<A> Latest<M, A>(this Atom<M, A> atom) =>
Observable.Return(atom.Value)
.Merge(atom.OnChange());

/// <summary>
/// Observe changes to the `Ref`
/// </summary>
Expand All @@ -43,6 +69,19 @@ public static IObservable<A> OnChange<A>(this Ref<A> atom) =>
add => atom.Change += new AtomChangedEvent<A>(add),
remove => atom.Change -= new AtomChangedEvent<A>(remove));

/// <summary>
/// Creates an observable that watches for changes to the `Atom`, but
/// also gets primed with the initial state on subscription, so there's
/// always at least one value that flows downstream.
/// </summary>
/// <param name="atom">Atom to observe</param>
/// <typeparam name="A">Value type</typeparam>
/// <returns>Observable atom</returns>
[Pure]
public static IObservable<A> Latest<A>(this Ref<A> atom) =>
Observable.Return(atom.Value)
.Merge(atom.OnChange());

/// <summary>
/// Observe changes to the `AtomHashMap`
/// </summary>
Expand Down

0 comments on commit ab4f955

Please sign in to comment.