Skip to content

Commit

Permalink
Pin upstream refreshables when mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Equinox- committed Mar 31, 2024
1 parent cee2843 commit 5b63785
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Meds.Shared/Refreshable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;
Expand All @@ -16,6 +15,7 @@ public class Refreshable<T>
{
private readonly IEqualityComparer<T> _equality;
private readonly List<IObserver> _observers = new List<IObserver>();
private readonly List<object> _pinnedUpstream = new List<object>();
private readonly Action<T> _disposer;

public T Current { get; private set; }
Expand Down Expand Up @@ -63,6 +63,7 @@ public Refreshable<TOut> Map<TOut>(Func<T, TOut> map, IEqualityComparer<TOut> eq
lock (_observers)
{
var target = new Refreshable<TOut>(map(Current), disposer, equality);
target._pinnedUpstream.Add(this);
_observers.Add(new Mapped<TOut>(target, map));
return target;
}
Expand All @@ -80,6 +81,8 @@ public Refreshable<TOut> Combine<TOther, TOut>(
var initialThis = Current;
var initialOther = other.Current;
var target = new Refreshable<TOut>(map(initialThis, initialOther), disposer, equality);
target._pinnedUpstream.Add(this);
target._pinnedUpstream.Add(other);
var combine = new MappedCombine<TOther, TOut>(target, initialThis, initialOther, map);
_observers.Add(new MappedCombine<TOther, TOut>.LeftObserver(combine));
other._observers.Add(new MappedCombine<TOther, TOut>.RightObserver(combine));
Expand Down

0 comments on commit 5b63785

Please sign in to comment.