GithubHelp home page GithubHelp logo

reactivemarbles / dynamicdata Goto Github PK

View Code? Open in Web Editor NEW
1.7K 64.0 178.0 18.35 MB

Reactive collections based on Rx.Net

Home Page: http://dynamic-data.org

License: MIT License

C# 100.00%
reactive-extensions reactive-collections rx dynamic-data rx-observable dotnet csharp mvvm reactive-programming dynamicdata

dynamicdata's People

Contributors

andersstorhaug avatar chrispulman avatar chuuddo avatar dependabot-preview[bot] avatar dependabot[bot] avatar dwcullop avatar elijahreva avatar glennawatson avatar hinidu avatar hutterm avatar jakenveina avatar jasonwurzel avatar kmgallahan avatar liklainy avatar longtomjr avatar ltrzesniewski avatar mendelmonteiro avatar modplug avatar noggog avatar onliner10 avatar owoudenberg avatar oysteinkrog avatar peter-b- avatar renovate[bot] avatar robsonj avatar rolandpheasant avatar ryanholden8 avatar stevosaurus avatar tomasfil avatar wouterdek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dynamicdata's Issues

Sum aggregates nullable to non-nullable

Shouldn't null + null be null and not 0?

public static IObservable<Decimal> Sum<T>(this IObservable<IChangeSet<T>> source, Func<T, Decimal> valueSelector) {...}

public static IObservable<Decimal> Sum<T>(this IObservable<IChangeSet<T>> source, Func<T, Decimal?> valueSelector) {...}

The overload 2 can be covered by valueSelector when using overload 1. The null sum scenario however is impossible at the moment.

issue sorting

with the following cache connect

        _pageController = new PageController();
        _filterController = new FilterController<MyVm>(myobject => myobject.Id != "");
        _sortController = new SortController<MyVm>(SortExpressionComparer<MyVm>.Descending(t => t.Id));

        var mySubscription = MyCache.Connect()
            .Filter(_filterController)
            .Sort(_sortController)
            .Page(_pageController)
            .ObserveOnDispatcher() 
            .Bind(Items)
            .Subscribe(); 

calling the following has no effect

_sortController.Change(SortExpressionComparer.Ascending(t => t.Id));

if i remove the page controller OR if i create a new page request like so
_pageController.Change(new PageRequest(1, 2));

then sorting works again. it looks like if you use a page controller without doing any paging the sorting just doesnt work.

Edit: It also appears that if you make the page the same size or greater than the number of elements, sorting doesnt work.

Edit: Add Some items to the cache
MyCache.AddOrUpdate(new MyVm("1"));
MyCache.AddOrUpdate(new MyVm("2"));
MyCache.AddOrUpdate(new MyVm("3"));
MyCache.AddOrUpdate(new MyVm("4"));

        _pageController.Change(new PageRequest(1, 3));
        _sortController.Change(SortExpressionComparer<MyVm>.Ascending(t => t.Id));

throws ArgumentOutOfRangeException. however a page size of 1 or 2 works fine.

Create OnItemAdded callback

There is OnItemRemoved but no OnItemAdded. I find that I sometime have to do some work when something is added to the collection. My implementation is.

    internal sealed class OnBeingAdded<T>
    {
        private readonly List<T> _items = new List<T>();
        private readonly IObservable<IChangeSet<T>> _source;
        private readonly Action<T> _callback;

        public OnBeingAdded(IObservable<IChangeSet<T>> source, Action<T> callback)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            if (callback == null)
                throw new ArgumentNullException("callback");
            _source = source;
            _callback = callback;
        }

        public IObservable<IChangeSet<T>> Run()
        {
            return Observable.Create((Func<IObserver<IChangeSet<T>>, IDisposable>)(observer =>
           {
               var subscriber = _source.Do(RegisterForRemoval, observer.OnError).SubscribeSafe(observer);
               return Disposable.Create(() =>
                                        {
                                            subscriber.Dispose();
                                            _items.ForEach<T>(t => _callback(t));
                                            _items.Clear();
                                        });
           }));
        }

        private void RegisterForRemoval(IChangeSet<T> changes)
        {
            changes.ForEach(change =>
          {
              switch (change.Reason)
              {
                  case ListChangeReason.Add:
                      _callback(change.Item.Current);
                      break;
                  case ListChangeReason.AddRange:
                      change.Range.ForEach(_callback);
                      break;
                  case ListChangeReason.Replace:
                      _callback(change.Item.Current);
                      break;
                  case ListChangeReason.Remove:
                      break;
                  case ListChangeReason.RemoveRange:
                      break;
                  case ListChangeReason.Moved:
                      break;
                  case ListChangeReason.Clear:
                      break;
                  default:
                      throw new ArgumentOutOfRangeException();
              }
          });
            _items.Clone(changes);
        }
    }

    public static IObservable<IChangeSet<T>> OnItemAdded<T>
        (this IObservable<IChangeSet<T>> source,
         Action<T> addAction)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        if (addAction == null)
            throw new ArgumentNullException("addAction");
        return new OnBeingAdded<T>(source, addAction).Run();
    }

Suggestion: Way to "Bind" ICollection<T> with SourceCache/SourceList

Hello Roland,

first of all, DynamicData is great!
I currently have the need for maintaining a list (its actually internal settings representation) but also using a SourceCache for propagating changes and data to the rest of the application. Think of it as a settings service in an application.

It would be great to have a builtin way to reflect add and remove changes to a list, like .Bind()

With best regards,
Stefan

Index out of range exception

so DateFavorited is a nullable DateTime.

In some combinations, such as when both TestVm's have a date set, and you remove one from the ItemCache, you get an index out of range error.

if you comment out the first item's date and remove the second one from the ItemCache, there is no error.

It also only happens if you are sorting on the DateFavorited field.

once this error occurs, changes to the itemcache stop refreshing the UI properly, so it's pretty nasty.

using System;
using System.Reactive.Linq;
using System.Windows;
using DynamicData.Binding;
using DynamicData;
using DynamicData.Controllers;
using DynamicData.Operators;

namespace WpfApplication3
{
    public partial class MainWindow : Window
    {
        public ObservableCollectionExtended<TestVm> Items { get; set; }
        public SourceCache<TestVm, int> ItemCache;
        public TestVm SelectedItem { get; set; }
        public PageController PageController;

        public MainWindow()
        {
            InitializeComponent();

            PageController = new PageController();
            var sortController = new SortController<TestVm>(SortExpressionComparer<TestVm>.Ascending(t => t.DateFavorited ?? DateTime.MinValue));
            var filterController = new FilterController<TestVm>(myVm => myVm.Id != 0);
            Items = new ObservableCollectionExtended<TestVm>();
            ItemCache = new SourceCache<TestVm, int>(myVm => myVm.Id);

            var item1 = new TestVm(1) { DateFavorited = DateTime.Now };
            var item2 = new TestVm(2) { DateFavorited = DateTime.Now };

            ItemCache.AddOrUpdate(item1);
            ItemCache.AddOrUpdate(item2);

            ItemCache.Connect()
                            .Filter(filterController)
                            .Sort(sortController).Do(OnNext)
                            .Page(PageController).Do(OnNext) //error doesnt occur with paging disabled
                            .ObserveOnDispatcher()
                            .Bind(Items)
                            .Subscribe();

            PageController.Change(new PageRequest(1, 100));

            DataContext = this;
        }

        private void OnNext(IChangeSet<TestVm, int> changeSet)
        {

        }

        private void BtnRemove_OnClick(object sender, RoutedEventArgs e)
        {
            if (SelectedItem != null)
            {
                SelectedItem.DateFavorited = null;
                ItemCache.Remove(SelectedItem); //ERROR!
            }
        }
    }

    public class TestVm
    {
        public int Id { get; set; }
        public DateTime? DateFavorited { get; set; }

        public TestVm(int id)
        {
            Id = id;
        }
    }
}

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <ListBox SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding Items}" />

        <StackPanel Orientation="Vertical" Grid.Column="1">
            <Button Name="BtnRemove" Content="Remove" Click="BtnRemove_OnClick" />
        </StackPanel>
    </Grid>
</Window>

Create overloads to allow sorting when properties change

The observable cache has .Sort() overloads to allow the consumer to change the comparer or to force a resort in the event that a property which is sorted on changes. These options are not available on the observable list and they need to be implemented

Group with IEqualityComparer?

Hey there, I'm looking for an overload of the Group function, that takes an IEqualityComparer, just like the GroupBy function in LINQ and Rx.

Is this something this library should/would/could implement, or is there any other method to achieve this functionality?

Can I wrap DB table as an observable

Hi, is it possible to get a reader observable wrapper on a Database table, for e.g. I have a table of tickets, how do I watch the table with this lib - this would be a tremendous help.

Enable capability to re-apply a transform

In the same way that an observable change set can dynamically change a filter, sort, group, page + more, @ButchersBoy has requested the ability to force dynamic data to re-apply a transform.

Experimenting with the API I have come up with something like this.

var personCache = new SourceCache<Person,string>(p=>p.Name);

//invoke OnNext to force a transform
var forceTransform = new Subject<Func<Person, bool>>();

var transformed = personCache.Connect()
.Transform(p => new PersonProxy(p), forceTransform);

//Use selector to choose which people should be transformed 
forceTransform.OnNext((person,key)=>true);

This is achievable and will be implemented soon.

Exception handling in subscription code

Hi Roland,

I've been playing with DynamicData for a while now and I've always been confused about the exception handling.

I read the code and its unit tests and I understand that :

  • any exception thrown while editing is caught and given back to an onError action if any, or rethrown. Seems fair to me.
  • any exception thrown while transforming or filtering will be caught and given back to all observers connected to the SourceList/SourceCache via the OnError action (given when subscribing).

But I think there is some kind of an unwanted weird behavior when a subscription code throws.

I tried with the following test :

[Test]
public void SingleSubscriberThatThrows()
{
    var s = new SourceList<int>();
    s.Connect().Subscribe(i => { throw new NotImplementedException(); });

    s.Edit(u => u.Add(1));
}

In this test an exception is correctly thrown when the new value is added in the SourceList, but it's handled and given back to the OnError action. There is here only one subscriber and he's disconnected from the inner subject in the SourceList when its subscription code throws so we're not able to see any exception here. (The subject's OnNext method throws, the only observer is disconnected, the exception is caught and given back to the subject in order to redispatch it to the other subscribers).

Let's try with two more subscribers :

[Test]
public void MultipleSubscribersWithOneThrowing()
{
    var s = new SourceList<int>();
    s.Connect().Subscribe(i => { });
    s.Connect().Subscribe(i => { throw new NotImplementedException(); });
    s.Connect().Subscribe(i => { });

    s.Edit(u => u.Add(1));
}

When you run this you actually have an exception thrown because there is still two other subscribers behind the underlying subject, and the exception is re-thrown because none of these subscribers handle correctly their OnError actions.

Now let's try with proper OnError handling :

[Test]
public void MultipleSubscribersHandlingErrorsWithOneThrowing()
{
    var firstSubscriberExceptionHandleCount = 0;
    var secondSubscriberExceptionHandleCount = 0;
    var s = new SourceList<int>();
    s.Connect().Subscribe(i => { }, e => firstSubscriberExceptionHandleCount++);
    s.Connect().Subscribe(i => { throw new NotImplementedException(); });
    s.Connect().Subscribe(i => { }, e => secondSubscriberExceptionHandleCount++);

    s.Edit(u => u.Add(1));

    Assert.That(firstSubscriberExceptionHandleCount, Is.EqualTo(1));
    Assert.That(secondSubscriberExceptionHandleCount, Is.EqualTo(1));
}

This test is successful ! The two OnError actions are correctly called by the underlying subject. And the exception is thus correctly handled.

The code behind this behavior is the InvokeNext method on SourceList (and same on SourceCache) :

private void InvokeNext(IChangeSet<T> changes)
{
    if (changes.Count == 0) return;

    lock (_locker)
    {
        try
        {
            _changes.OnNext(changes);

            if (_countChanged.IsValueCreated)
                _countChanged.Value.OnNext(_readerWriter.Count);
        }
        catch (Exception ex)
        {
            _changes.OnError(ex);
        }
    }
}

I understand that using the OnError action of a subscriber is pretty useful when handling exceptions in filters or tranformers but catching exceptions the way it's done in InvokeNext method is very dangerous in my opinion.
It's okay when multiple subscribers are connected to the observable because the exception will be given to them or thrown by the underlying subject when the OnError action is not given to all subscribers ... there is a way to be notified of the exception (it's just okay for me because this way feels a bit weird ๐Ÿ˜„ )

But it's very dangerous when only one subscriber is connected : the exception will be swallowed and the subscription will remain broken due to the behavior of the underlying subject. No changes will be propagated after that.

And finally it's mega dangerous when using the DynamicData's RefCount operator because it wraps the underlying observer and give an empty implementation to the OnError action that swallows the exception.

My last test was using the RefCount operator:

[Test]
public void ExceptionShouldBeThrownInSubscriberCode()
{
    var changesCountFromSubscriber1 = 0;
    var changesCountFromSubscriber2 = 0;
    var source = new SourceList<int>();

    var observable = source.Connect().RefCount();
    observable.Subscribe(i => changesCountFromSubscriber1++);
    observable.Subscribe(i => { throw new NotImplementedException(); });
    observable.Subscribe(i => changesCountFromSubscriber2++);

    source.Edit(l => l.Add(1));
    source.Edit(l => l.Add(2));

    Assert.That(changesCountFromSubscriber1, Is.EqualTo(2));
    Assert.That(changesCountFromSubscriber2, Is.EqualTo(2));
}

The result is scary :

  • no exception thrown anywhere
  • changesCountFromSubscriber1 equals 1 instead of 2.
  • changesCountFromSubscriber2 equals 0 instead of 2.
  • all the changes after that will be ignored

The first Add works and then the exception is thrown, the second subscriber is not called, the subject is completed and the following calls are just dropped. You have to unsubscribe and subscribe again to make it work.

Sorry for the super long issue but I think it can be a tricky bug to find for the users of this library (I am one, and I had this bug once ๐Ÿ˜„ )
I really don't have any good idea to solve this at the moment but I'm sure we can find something great ๐Ÿ‘

Any thoughts ?

Throw more meaningful exceptions

Index out of range exceptions can be thrown for various reasons when sorting, virtualising or paging. It would be more meaningful to pre-check for index out of range and throw a wrapped exception.

Incompatible with Reactive v3.0.0.0

Not really a bug. But unfortunately Reactive v3.0.0 changed their publickey which means dynamicdata cannot be used with reactive v3.0.0 even with an assembly version redirect in place.

From Reactive v3.0.0 release notes...
"The strong name key has been changed. The old key was a Microsoft key that could not be fully checked-in. The new key is checked in, allowing anyone to create custom builds easily. The package names have all changed"

Will need to update the reactive dependant reference to v3 before dynamicdata can be used with it.

getting a random item from a paged, filtered list

I have an ItemCache connected like so, where Items is an OC<ItemVm>

ItemCache.Connect()
                        .Filter(FilterController).Do(OnNext)
                        .Sort(SortController).Do(OnNext)
                        .Page(PageController).Do(OnNext)
                        .ObserveOnDispatcher()
                        .Do(changes => _pageParameters.Update(changes.Response))
                        .Bind(Items)
                        .Subscribe();

Here is my attempt at getting a random item. It is using Items

            var rInt = r.Next(0, Items.Count - 1);
            var randomItemVm = Items.Skip(rInt).Take(1).Single();

The problem is that Items only ever holds as many items as there are per page. How can I pull a random item from ItemCache whether or not it is on the current page, keeping in mind that a filter has been applied?

Consolidate conversion from observable collections to change sets

There is duplicated code for converting to observable change sets from the following sources:

ObservableCollection
ReadOnlyObservableCollection
ReactiveList

This needs to be consolidated into common code.

Additionally there is a bug where the index on reported changes does not always match the true source items index

MissingKeyException on Filter and Group

Hi there,

first of all, thanks for your great work. Looks like an amazing useful utility and I would love to integrate it in some parts of my application. Unfortunately, on my first test I run into an issue with grouping and filtering.
So here is what I try to do:

  1. A SourceCache is filled from a Service. Items are only added so far.
  2. After the Connect I apply a Filter and then I Group the data.
  3. The Filter may change and additional data may be added.
    But now I run into MissingKeyException("{0} is missing from previous value on remove. Object type {1}, Key type {2}, Group key type {3}" in my big Application.
    I created a little Test Project and there I have the same issue. At least it looks like, because in my big application it also crashed when the SourceCache is not changed. Just starting Edit and doing a loop inside makes the difference. If I do not call Edit after changing the filter it does work. Also it does "work" if I throttle 250ms before and after the Filter part...
    However, here is the sample. Hope you could help me with that and tell my what I'm doing wrong here... Thanks!
public sealed partial class MainPage : Page
   {
      private SourceCache<TestObject, int> m_sourceCache;
      private ReplaySubject<Func<TestObject, bool>> m_changeObs;
      private ReadOnlyObservableCollection<GroupContainer> m_entries;
      public ReadOnlyObservableCollection<GroupContainer> Entries => m_entries;

      public MainPage()
      {
         this.InitializeComponent();

         m_changeObs = new ReplaySubject<Func<TestObject, bool>>();

         var dispatcher = new CoreDispatcherScheduler(Window.Current.Dispatcher);

         m_sourceCache = new SourceCache<TestObject, int>(x => x.Id);
         m_sourceCache.Connect()
            .Filter(m_changeObs)
            .Group(x => x.Group)
            .Transform(x => new GroupContainer(x, dispatcher))
            .ObserveOn(dispatcher)
            .Bind(out m_entries)
            .Subscribe();

         Observable.Interval(TimeSpan.FromMilliseconds(250))
            .Subscribe(x => DoFilterAndAdd());
      }

      private int m_count;
      private void AddItems()
      {
         m_sourceCache.Edit(innerCache =>
         {
            var list = new List<TestObject>();
            for (var i = 0; i < 10; i++)
            {
               m_count++;
               list.Add(new TestObject() {Id = m_count, Group = i, Text = DateTime.Now.ToString("T")});
            }
            innerCache.AddOrUpdate(list);
         });
      }

      private bool m_bool;
      private void DoFilterAndAdd()
      {
         m_bool = !m_bool;
         m_changeObs.OnNext(o => m_bool);
         AddItems();
      }
   }

   public class TestObject
   {
      public int Id { get; set; }

      public string Text { get; set; }

      public int Group { get; set; }
   }

   public class GroupContainer
   {
      private readonly ReadOnlyObservableCollection<GroupEntry> m_entries;

      public ReadOnlyObservableCollection<GroupEntry> Entries => m_entries;

      public GroupContainer(IGroup<TestObject, int, int> groupEntries, IScheduler scheduler)
      {
         groupEntries.Cache.Connect()
                                 .Transform(x => (new GroupEntry(x)))
                                 .ObserveOn(scheduler)
                                 .Bind(out m_entries)
                                 .Subscribe();
         Title = groupEntries.Key.ToString("D");
      }
      public string Title { get; set; }

   }

   public class GroupEntry
   {
      public string Title { get; set; }

      public GroupEntry(TestObject obj)
      {
         Title = obj.Text;
      }
   }

Operators to convert a standard observable into an observable change set

Require operators to take a standard rx observable and convert is into an observable change set

IObservable<IChangeSet<TObject>> ToObservableChangeSet<TObject>(this IObservable<T> source)

also need a buffered converter

IObservable<IChangeSet<TObject>> ToObservableChangeSet<TObject>(this IObservable<IEnumerabe<T>> source)

Additionally add overloads to limit by size and by time

RX based sort throws error.

SortController is now marked obsolete. When I try and use the preferred RX based overloads of .Sort, the method throws an ArgumentNullException.

You can reproduce this easily by by creating a new Console App, and with the following Program.cs contents:

using System;
using System.Collections.ObjectModel;
using System.Reactive;
using System.Reactive.Subjects;
using DynamicData;
using DynamicData.Binding;

namespace DynamicDataSortWithRx
{
    public class Data
    {
        public Data(int id, string value)
        {
            Id = id;
            Value = value;
        }

        public int Id { get; }
        public string Value { get; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var cache = new SourceCache<Data, int>(d => d.Id);

            var sortPump = new Subject<Unit>();

            ReadOnlyObservableCollection<Data> datas;
            var disposable = cache.Connect()
                .Sort(SortExpressionComparer<Data>.Ascending(d => d.Id), sortPump)
                .Bind(out datas)
                .Subscribe();

            disposable.Dispose();
        }
    }
}

Stack trace:

   at System.Reactive.Linq.Observable.Synchronize[TSource](IObservable`1 source, Object gate)
   at DynamicData.Cache.Internal.Sort`2.<Run>b__7_0(IObserver`1 observer)
   at System.Reactive.AnonymousObservable`1.SubscribeCore(IObserver`1 observer)
   at System.Reactive.ObservableBase`1.Subscribe(IObserver`1 observer)
--- End of stack trace from previous location where exception was thrown ---
   at System.Reactive.PlatformServices.ExceptionServicesImpl.Rethrow(Exception exception)
   at System.Reactive.Stubs.<>c.<.cctor>b__2_1(Exception ex)
   at System.Reactive.AnonymousObserver`1.OnErrorCore(Exception error)
   at System.Reactive.ObserverBase`1.OnError(Exception error)
   at System.Reactive.SafeObserver`1.OnError(Exception error)
   at System.Reactive.Linq.ObservableImpl.Do`1._.OnError(Exception error)
   at System.Reactive.AutoDetachObserver`1.OnErrorCore(Exception exception)
   at System.Reactive.ObserverBase`1.Fail(Exception error)
   at System.Reactive.ObservableBase`1.Subscribe(IObserver`1 observer)
   at System.ObservableExtensions.SubscribeSafe[T](IObservable`1 source, IObserver`1 observer)
   at System.Reactive.Linq.ObservableImpl.Do`1.Run(IObserver`1 observer, IDisposable cancel, Action`1 setSink)
   at System.Reactive.Producer`1.Run(IScheduler _, State x)
   at System.Reactive.Concurrency.ScheduledItem`2.InvokeCore()
   at System.Reactive.Concurrency.CurrentThreadScheduler.Trampoline.Run(SchedulerQueue`1 queue)
   at System.Reactive.Concurrency.CurrentThreadScheduler.Schedule[TState](TState state, TimeSpan dueTime, Func`3 action)
   at System.Reactive.Concurrency.LocalScheduler.Schedule[TState](TState state, Func`3 action)
   at System.Reactive.Producer`1.SubscribeRaw(IObserver`1 observer, Boolean enableSafeguard)
   at System.Reactive.Producer`1.Subscribe(IObserver`1 observer)
   at System.ObservableExtensions.Subscribe[T](IObservable`1 source)
   at DynamicDataSortWithRx.Program.Main(String[] args) in H:\GitHub\DynamicDataSortWithRx\DynamicDataSortWithRx\Program.cs:line 35
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Combine list operators

Write operators to combine lists using the following logic operators:

Or
And
Xor
Except

Wrappers generated by Transform are reused after being disposed

If a given type is being directed to different observable collections based on some filter, and with a transform, and it moves from one collection to another and then back, then the wrapper type is no longer disposed and recreated. On the last move, it's disposed and reused, leading to Bad Things. The test below fails:

using System;
using DynamicData;
using DynamicData.Binding;
using DynamicData.Controllers;
using NUnit.Framework;

namespace DDataTest {
    public class Widget {
        public int Id { get; set; }
        public bool Active { get; set; }
    }

    public class WidgetWrapper : IDisposable {
        public bool IsDisposed { get; private set; }
        public Widget Wrapped { get; private set; }
        public WidgetWrapper(Widget Target) {
            IsDisposed = false;
            Wrapped = Target;
        }

        public void Dispose() {
            IsDisposed = true;
        }
    }

    public class ThrowawayTest {

        [Test]
        public void Does_Not_Reuse_Disposed_Wrappers() {
            var subject = new SourceCache<Widget, int>(w => w.Id);

            var activeObservable = new ObservableCollectionExtended<WidgetWrapper>();
            var inactiveObservable = new ObservableCollectionExtended<WidgetWrapper>();

            var activeFilter = new FilterController<WidgetWrapper>(wrapper => wrapper.Wrapped.Active);
            var inactiveFilter = new FilterController<WidgetWrapper>(wrapper => !wrapper.Wrapped.Active);

            subject.Connect()
                   .Transform(w => new WidgetWrapper(w))
                   .Filter(activeFilter)
                   .Bind(activeObservable)
                   .DisposeMany()
                   .Subscribe();

            subject.Connect()
                   .Transform(w => new WidgetWrapper(w))
                   .Filter(inactiveFilter)
                   .Bind(inactiveObservable)
                   .DisposeMany()
                   .Subscribe();

            var w1 = new Widget() { Id = 0, Active = true };
            var w2 = new Widget() { Id = 1, Active = true };

            subject.AddOrUpdate(w1);
            subject.AddOrUpdate(w2);
            Assert.AreEqual(2, activeObservable.Count);
            Assert.AreEqual(0, inactiveObservable.Count);
            Assert.IsFalse(activeObservable[0].IsDisposed);
            Assert.IsFalse(activeObservable[1].IsDisposed);

            //This needs to be done twice to trigger the behavior
            w1.Active = !w1.Active;
            activeFilter.Reevaluate();
            inactiveFilter.Reevaluate();

            w1.Active = !w1.Active;
            activeFilter.Reevaluate();
            inactiveFilter.Reevaluate();

            Assert.AreEqual(2, activeObservable.Count);

            Assert.False(activeObservable[0].IsDisposed);
            Assert.False(activeObservable[1].IsDisposed);
        }

    }
}

GroupOn does not bind to target collection when only items within a group have changed.

When I have a source collection that I project into a target collection with grouping, the target collection is correctly updated if a new item is added that causes the creation of a new group.
If I add an item to the source list that does not cause the creation of a new group, then the target collection won't reflect the newly added item.

The following unit test shows this issue clearly:

// A sample source list.
var sourceList = new ObservableCollection<string>(new[]
{
    "Ape",
    "Ant",
    "Bear",
    "Boar",
    "Cougar",
});

// The target list to update when the source list changes.
var targetList = new ObservableCollectionExtended<IGroup<string, char>>();
bool targetListIsUpdated;

sourceList
    .ToObservableChangeSet()
    .Sort(SortExpressionComparer<string>.Descending(x => x))
    .GroupOn(x => x[0])
    .Bind(targetList)
    .Subscribe(x =>
    {
        targetListIsUpdated = true;
    });

// This will pass the assert, because a new group will be created (there's no group by 'D' yet).
targetListIsUpdated = false;
sourceList.Add("Dolphin");
Assert.IsTrue(targetListIsUpdated);

// This will fail the assert, because no new group is created (there's already a group by 'A').
targetListIsUpdated = false;
sourceList.Add("Algae");
Assert.IsTrue(targetListIsUpdated);

help running out of memory, need a better suggestion/sample on observing

Hi, can you please help, I am using the library to watch a list of books from a table.

//Books is the catalog list, title, username, checkin Date, Check Out date
var llibraryCatalog = new SourceList<Books>();

On the server-side I am using the reactive extensions to observe the llibraryCatalog. So, when the books are checked out, or check in, over due, I need to send notifications to the user. So, I created the observable and when that list changes, I write to the list.

Each user has an inbox when they login, and I need to subscribe to changes on that filtered list.
// Users Inbox - notifications IObservable<IChangeSet<Books>> myIntsObservable = llibraryCatalog.Connect();

The list is at 12000 and I've 2 issues, and seeking help

  1. I get an out of memory exception, or it get slow can you please help redo this better. ๐Ÿ”ข
  2. How to implement the filter for the users, or Design should I simply have one master filtered list of all late books, all new books etc, and then have the users object subscribe to it?

thanks

Can't combine SourceLists logically because of clear()

The below test fails

    [Fact]
    public void MergeManyShouldWork()
    {
        var a = new SourceList<int>();
        var b = new SourceList<int>();
        var c = new SourceList<int>();

        var parent = new SourceList<SourceList<int>>();
        parent.Add(a);
        parent.Add(b);
        parent.Add(c);

        var d = parent.Connect().MergeMany(e => e.Connect()).AsObservableList();

        d.Count.Should().Be(0);

        a.Add(1);

        d.Count.Should().Be(1);
        a.Add(2);
        d.Count.Should().Be(2);

        b.Add(3);
        d.Count.Should().Be(3);
        b.Add(5);
        d.Count.Should().Be(4);
        d.Items.Should().BeEquivalentTo(new[] {1, 2, 3, 5});


        b.Clear();

        // Fails below
        d.Count.Should().Be(2);
        d.Items.Should().BeEquivalentTo(new[] {1, 2});
    }

The Clear() methods breaks the changeset because it cannot be combined with other change sets and totally blows away result.

Add OnItemAdded() for IObservableCache<TObject, TKey>

var cache = new SourceCache<Person, int>(x => x.Id);

cache.Connect()
    .OnItemAdded(a => a.MyEvent += OnMyEvent) // does not exist
    .OnItemRemoved(a => a.MyEvent -= OnMyEvent)
    .Subscribe();

A similar approach works with SourceList and IObservableList, but not the cache equivalent.
Have I missed something?

Here is my current approach that seems to work if you are interested in a PR:

    internal static class DynamicDataEx
    {
        public static IObservable<IChangeSet<TObject, TKey>> OnItemAdded<TObject, TKey>(
            this IObservable<IChangeSet<TObject, TKey>> source,
            Action<TObject, TKey> addAction)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            if (addAction == null)
                throw new ArgumentNullException("addAction");
            return new OnBeingAdded<TObject, TKey>(source, addAction).Run();

        }
    }

    internal sealed class OnBeingAdded<TObject, TKey>
    {
        private readonly Action<TObject, TKey> _callback;
        private readonly IObservable<IChangeSet<TObject, TKey>> _source;

        public OnBeingAdded(IObservable<IChangeSet<TObject, TKey>> source, Action<TObject, TKey> callback)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            if (callback == null)
                throw new ArgumentNullException("callback");
            _source = source;
            _callback = callback;
        }

        public IObservable<IChangeSet<TObject, TKey>> Run()
        {
            return _source.Do(RegisterForAddition);
        }

        private void RegisterForAddition(IChangeSet<TObject, TKey> changes)
        {
            foreach (Change<TObject, TKey> change in changes)
            {
                switch (change.Reason)
                {
                    case ChangeReason.Add:
                        _callback(change.Current, change.Key);
                        continue;
                        //                        case ChangeReason.AddRange:
                        //                            change.Range.ForEach<TObject, TKey>(this._callback);
                        //                            continue;
                    case ChangeReason.Update:
                        _callback(change.Current, change.Key);
                        continue;
                    default:
                        continue;
                }
            }
        }
    }

Paging Error

This is thrown after I add an item to a bound collection. I can't really seem to narrow down the exact cause, and my stuff still all works.

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.ObjectModel.Collection1.RemoveAt(Int32 index) at DynamicData.Binding.SortedObservableCollectionAdaptor2.<>c__DisplayClass5.b__6(Change`2 update) in C:\projects\dynamicdata\DynamicData\Binding\SortedObservableCollectionAdaptor.cs:line 90

at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.ObjectModel.Collection1.RemoveAt(Int32 index) at DynamicData.Binding.SortedObservableCollectionAdaptor2.<>c__DisplayClass5.b__6(Change2 update) in C:\projects\dynamicdata\DynamicData\Binding\SortedObservableCollectionAdaptor.cs:line 90 at DynamicData.Kernel.EnumerableEx.ForEach[T](IEnumerable1 source, Action1 action) in C:\projects\dynamicdata\DynamicData\Kernel\EnumerableEx.cs:line 30 at DynamicData.Binding.SortedObservableCollectionAdaptor2.DoUpdate(ISortedChangeSet2 updates, IObservableCollection1 list) in C:\projects\dynamicdata\DynamicData\Binding\SortedObservableCollectionAdaptor.cs:line 82
at DynamicData.Binding.SortedObservableCollectionAdaptor2.Adapt(ISortedChangeSet2 changes, IObservableCollection1 collection) in C:\projects\dynamicdata\DynamicData\Binding\SortedObservableCollectionAdaptor.cs:line 59 at DynamicData.DynamicDataEx.<>c__DisplayClass3312.b__333(ISortedChangeSet2 updates) in C:\projects\dynamicdata\DynamicData\DynamicDataEx.cs:line 2505 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Reactive.PlatformServices.ExceptionServicesImpl.Rethrow(Exception exception) at System.Reactive.ExceptionHelpers.Throw(Exception exception) at System.Reactive.Stubs.<.cctor>b__1(Exception ex) at System.Reactive.AnonymousObserver1.OnErrorCore(Exception error)
at System.Reactive.ObserverBase1.OnError(Exception error) at System.Reactive.AutoDetachObserver1.OnErrorCore(Exception exception)
at System.Reactive.ObserverBase1.OnError(Exception error) at DynamicData.DynamicDataEx.<>c__DisplayClass3312.b__333(ISortedChangeSet`2 updates) in C:\projects\dynamicdata\DynamicData\DynamicDataEx.cs:line 2509

Code formatting/style

I've got a couple of suggestions, let me know if you agree and I'll create a pull request.

  1. Only use tabs - no spaces for indentation
  2. Don't use IEnumerable.ForEach, use foreach instead
  3. Put switch break; statements inside the scope regions if present

Cheers

{key} is missing from previous value

I have no idea how to debug exception of type 'DynamicData.MissingKeyException' - "{key} is missing from previous value". It is happening on on .AddOrUpdate action.
Record with this {key} already exist in the SourceCash, so this is on Update.

Reverse operator

Create an operator which reverses the result set order.

I often I find I order data on most recent first. This operator will achieve that much more efficiently than using the Sort() operator.

Some operators share state

Some of the operators share state when subscribed to multiple times. This is clearly incorrect behaviour

Create dynamic Or combiner

I've had a hack at it. It seems to work but looks very inefficient.

        public static IObservable<IChangeSet<T>> Or<T>
        (IObservable<IChangeSet<SourceList<T>>> observable)
        {
            var sl = new SourceList<T>();
            var foo = observable
                .ToCollection()
                .Select
                (list => list.Count != 0 ? list.Select(l => l.Connect())
                             .Aggregate((a, b) => a.Or(b))
                             .ToCollection()
                             : Observable.Return(ImmutableList<T>.Empty)

                             )
                .Switch();

            var d = foo
                .Subscribe
                (b =>
                 {
                     sl.Edit
                         (e =>
                          {
                              e.Clear();
                              e.AddRange(b);
                          });

                 });

            return Observable.Using(()=>d,o=> sl.Connect());
        }

TransformMany operator problem

The transform many operator does not correctly handle the situation when the children are removed from the parent update

Error with .Convert().Sort()

Hello

I have an issue with Convert followed by Sort. As soon as I remove an element of the underlying source, I get an exception:

Current position cannot be found. The item is not in the collection

The query is

cache.Connect()
    .Convert(nr => new Container(nr)) 
    .Sort(SortExpressionComparer<Container>.Ascending(c => c.Nr))
    .Subscribe(x => x.Dump(), ex => ex.Dump());

Here you can find a LinqPad script as .txt file.
Convert - Sort - Problem.txt

I do not see that exception when I use Transform or TransformSafe instead of Convert.
Is that by design?
Is Convert the wrong method to use here?

Thanks a lot for any hint or explanation.

Greetings
Peter

[Question] Connect to a bound list to create a "view on a view on a source" / "derived-derived collection"?

My scenario:

  1. there is a source list of domain entities;
  2. that source list is used to produce a derived list of view models which have a property IsSelected since selection is a GUI concern;
  3. how do I react to IsSelected property changes to create a "sub-derived" collection of selected view models?

Here is a working mixed ReactiveUI solution, which unfortunately has it's own known problems.

private readonly ReactiveList<OrderViewModel> _items;
private readonly IReactiveDerivedList<OrderViewModel> _selectedItems;

orderProvider.Orders
    .Connect()
    .Transform(o => new OrderViewModel(o))
    .ObserveOnDispatcher()
    .Bind(_items)
    .DisposeMany()
    .Subscribe();

_selectedItems = _items.CreateDerivedCollection(x => x, x => x.IsSelected);

Is there any way to make it DynamicData-only maybe via WhenValueChanged? So far I have discovered Clone which doesn't seem to be the best fit.

Introduce Join Operators for ObservableCache

There are currently operators to apply logical collection operators, However there is no direct means of joining 2 different caches together.

Ideally there should be a JoinOne (for one to one mappings) and JoinMany (for one to many operators).

For example

var people = new SourceCache<Person, string>(person => person.Name);
var address = new SourceCache<Address, string>(address => address .PersonName);

For a one to one mapping

var personWithOneAddress = people.JoinOne(address, address=> address.PersonName, (person, address)=> new PersonWithAddress(person,address ));

and also something similar where a person has many addresses

Shouldn't Count() provide '0' for an empty source cache?

I came across the need of an observable for the number of elements in an source cache.

var sourceCache = new SourceCache<string, int>(s => s.GetHashCode());

sourceCache.Connect()
            .Count()
            .Dump();

sourceCache.AddOrUpdate("test");

Result is:

IObservable<Int32>
1

From my point of view, the expected behavior should be:

IObservable<Int32>
0
1

Optimise batching in change aware list

When chains of operators are applied to the observable list, unless updates are notified as batches there can be a severe performance penalty because list operations have to continually use indexof() operations.

To work around this the change aware list will attempt to batch changes even when the consumer does not do so.

Confused ObservableList when merging to ObservableCollectionExtends

Hello Roland,

thank you for providing DynamicData which we use as a standard asset in our projects and which provides very elegant solutions for many bread-and-butter programming issues.

My team and me have discovered some unexpected behavior: When merging two observable collections and then adding and removing values, there is something strange going on. We created a small LinqPad-script that illustrates the issue.

` var a = new ObservableCollectionExtended();
var b = new ObservableCollectionExtended();

Observable.Merge(
a.ToObservableChangeSet(),
b.ToObservableChangeSet())
    //      .AddKey(x => x.GetHashCode())
    .QueryWhenChanged(x => x.Dump())
    .Subscribe();

a.Add(1);
a.Add(2);
b.Add(4);
a.Remove(1);`

The result is as follows

image

When you uncomment the line .AddKey[...], the whole thing is converted to an ObservableCache and the elements are added and removed as expected so we assume it is an issue with ObservableList.

Best,
djb

Can't use DynamicData 5.0.5.2078 in Profile111

Hi,
I have a problem using the current stable in Profile111 (Xamarin Partable Library):

C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3274: The primary reference "DynamicData, Version=5.0.5.2078, Culture=neutral, processorArchitecture=MSIL" could not be resolved because it was built against the ".NETPortable,Version=v5.0" framework. This is a higher version than the currently targeted framework ".NETPortable,Version=v4.5,Profile=Profile111".

I installed the nuget package without problems, no dependency errors.
Am I doing anything wrong here? Thank you, this is an amazing library!!!

For ChangeReason.Change, must supply previous value

When transforming changes into an Observable of a single value, it is possible that the transformed value is null (e.g. when a property is not set on an object), which is perfectly normal.

However, if such a transformation leads to a result value being changed from null to something else, there is an exception thrown in the constructor of Change with the following message: "For ChangeReason.Change, must supply previous value". (See sample code here for reproducing.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.