Example #1
0
        public IObservable <IChangeSet <T> > Run()
        {
            return(Observable.Create <IChangeSet <T> >(observer =>
            {
                Interlocked.Increment(ref _refCount);
                if (Volatile.Read(ref _refCount) == 1)
                {
                    Interlocked.Exchange(ref _list, _source.AsObservableList());
                }

                // ReSharper disable once PossibleNullReferenceException (never the case!)
                var subscriber = _list.Connect().SubscribeSafe(observer);

                return Disposable.Create(() =>
                {
                    Interlocked.Decrement(ref _refCount);
                    subscriber.Dispose();
                    if (Volatile.Read(ref _refCount) != 0)
                    {
                        return;
                    }
                    _list.Dispose();
                    Interlocked.Exchange(ref _list, null);
                });
            }));
        }
            public IObservable <IChangeSet <TObject, TKey> > Run()
            {
                return(Observable.Create <IChangeSet <TObject, TKey> >(observer =>
                {
                    lock (_locker)
                        if (++_refCount == 1)
                        {
                            _list = _source.AsObservableList();
                        }

                    var subscriber = _list.Or().SubscribeSafe(observer);

                    return Disposable.Create(() =>
                    {
                        subscriber.Dispose();
                        IDisposable listToDispose = null;
                        lock (_locker)
                            if (--_refCount == 0)
                            {
                                listToDispose = _list;
                                _list = null;
                            }

                        listToDispose?.Dispose();
                    });
                }));
            }
Example #3
0
        public void Cast()
        {
            //take any list
            var people = new SourceList <Person>();

            //convert to dynamic
            dynamic dyn = people;

            //do a little magic
            IObservable <IChangeSet <object> > objectChanges = Converter.AsObjectList(dyn);
            IObservableList <object>           myList        = objectChanges.AsObservableList();
        }
Example #4
0
        public IObservable <IChangeSet <T> > Run()
        {
            int refCount             = 0;
            var locker               = new object();
            IObservableList <T> list = null;

            return(Observable.Create <IChangeSet <T> >(observer =>
            {
                lock (locker)
                {
                    refCount++;
                    if (refCount == 1)
                    {
                        list = _source.AsObservableList();
                    }

                    // ReSharper disable once PossibleNullReferenceException (never the case!)
                    var subscriber = list.Connect().SubscribeSafe(observer);

                    return Disposable.Create(() =>
                    {
                        lock (locker)
                        {
                            refCount--;
                            subscriber.Dispose();
                            if (refCount != 0)
                            {
                                return;
                            }
                            list.Dispose();
                            list = null;
                        }
                    });
                }
            }));
        }
Example #5
0
        public async Task TestFilterOnProperty()
        {
            var listA = new SourceList <X>();
            var listB = new SourceList <X>();

            using (IObservableList <X> list = listA.Connect()
                                              .Or(listB.Connect())
                                              .AsObservableList())
            {
                var nameA1 = "A1";
                var a1     = new X(nameA1);
                var a2     = new X("A2");
                listA.Edit(l =>
                {
                    l.Clear();
                    l.Add(a1);
                    l.Add(a2);
                });

                var b1 = new X("B1");
                var b2 = new X("B2");
                listB.Edit(l =>
                {
                    l.Clear();
                    l.Add(b1);
                    l.Add(b2);
                });

                Assert.AreEqual(4, list.Count);

                int count = await list.CountChanged.FirstAsync();

                Assert.AreEqual(4, count);

                IObservable <IChangeSet <X> > obsFiltered = list.Connect()
                                                            .FilterOnProperty(v => v.IsConnected, v => v.IsConnected);

                using (IObservableList <X> obsFilteredAsList = obsFiltered.AsObservableList())
                {
                    IObservable <IChangeSet <XVm> > obsTransformed = obsFiltered
                                                                     .Transform(v => new XVm(v))
                                                                     .DisposeMany();

                    var ctorCount = 0;
                    var dtorCount = 0;
                    using (IObservableList <XVm> obsTransformedAsList = obsTransformed.AsObservableList())
                    {
                        ctorCount += 4;

                        Assert.That(obsFilteredAsList.Items.Contains(a1));
                        Assert.That(obsFilteredAsList.Items.Count(), Is.EqualTo(obsTransformedAsList.Items.Count()));

                        a1.IsConnected = false;
                        Assert.That(obsFilteredAsList.Items, Has.No.Member(a1));
                        dtorCount++;
                        Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
                        Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));

                        a1.IsConnected = true;
                        Assert.That(obsFilteredAsList.Items, Has.Member(a1));
                        ctorCount++;
                        Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
                        Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));

                        Console.WriteLine("--remove");
                        listA.Remove(a1);
                        dtorCount++;
                        Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
                        Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));

                        Console.WriteLine("--add");
                        listA.Add(a1);
                        ctorCount++;
                        Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
                        Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));

                        Console.WriteLine("--clear");
                        listA.Clear();
                        dtorCount += 2; //FIX:  List A contains 2 items (was adding 4)
                        Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
                        Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));

                        Console.WriteLine("--add");

                        //FIX: Maybe a debate required!  List B already contains b1, so not regarded as a new item in the Or result
                        Debug.Assert(listB.Items.Contains(b1));
                        listA.Add(b1);
                        //  ctorCount++;
                        Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
                        Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));

                        Console.WriteLine("--disp");
                    }
                    dtorCount += 2; //FIX: Should be +3 s this is what
                    Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
                    Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));
                }
            }
        }