Skip to content

Instantly share code, notes, and snippets.

@ericdes
Last active February 27, 2016 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericdes/6793d5dad79334323d1d to your computer and use it in GitHub Desktop.
Save ericdes/6793d5dad79334323d1d to your computer and use it in GitHub Desktop.
// OK
private void AddTrade_Click(object sender, RoutedEventArgs e)
{
var tradeCount = ViewModel.MyTrades.Count;
ViewModel.TradesSource.AddOrUpdate(Trade.Generate(tradeCount));
}
// UPDATE NOT PROPAGATED TO VIEW
private void UpdateMarketPrice_Click(object sender, RoutedEventArgs e)
{
ViewModel.MyTrades[0].SetMarketPrice(13);
}
public class Vm1 : ReactiveObject, IDisposable
{
private readonly IDisposable _cleanup;
private readonly IObservableCache<Trade, long> _trades;
private ReadOnlyObservableCollection<Trade> _myTrades;
public ReadOnlyObservableCollection<Trade> MyTrades
{
get { return _myTrades; }
}
public readonly SourceCache<Trade, long> TradesSource;
public Vm1()
{
TradesSource = new SourceCache<Trade, long>(trade => trade.Id);
var tradesObservable = TradesSource.AsObservableCache();
TradesSource.AddOrUpdate(Trade.Generate(0));
_trades = TradesSource.AsObservableCache();
var job = _trades.Connect()
.Bind(out _myTrades)
.Subscribe();
_cleanup = new CompositeDisposable(_trades, TradesSource, job);
}
public void Dispose()
{
_cleanup.Dispose();
}
}
@RolandPheasant
Copy link

This reduced code will to the same job

    public class Vm1 : ReactiveObject, IDisposable
    {
        private readonly IDisposable _cleanup;

       private ReadOnlyObservableCollection<Trade> _myTrades;
        public ReadOnlyObservableCollection<Trade> MyTrades
        {
            get { return _myTrades; }
        }

        public readonly SourceCache<Trade, long> TradesSource;

      public Vm1()
        {
            TradesSource = new SourceCache<Trade, long>(trade => trade.Id);
            TradesSource.AddOrUpdate(Trade.Generate(0));

            var job = TradesSource.Connect()
                .Bind(out _myTrades)
                .Subscribe();

            _cleanup = new CompositeDisposable(_trades, TradesSource, job);
        }

        public void Dispose()
        {
            _cleanup.Dispose();
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment