Beispiel #1
0
        public IDisposable Subscribe(IObserver <ListChange <T> > observer)
        {
            var mapper = new WhereChangeMapper();
            var gate   = new object();

            return(_source.Subscribe(
                       Observer.Create <ListChange <T> >(
                           onNext: change =>
            {
                lock (gate)
                {
                    var newChanges = new List <ListChange <T> >();

                    change(
                        insert: (index, item) => newChanges.AddRange(mapper.Insert(index, item, _predicate(item))),
                        replace: (index, item) => newChanges.AddRange(mapper.Replace(index, item, _predicate(item))),
                        remove: index => newChanges.AddRange(mapper.Remove <T>(index)),
                        clear: () => newChanges.AddRange(mapper.Clear <T>()));

                    if (newChanges.Count > 0)
                    {
                        observer.OnNext(ListChange.Combine(newChanges));
                    }
                }
            },
                           onCompleted: observer.OnCompleted,
                           onError: observer.OnError)));
        }
Beispiel #2
0
        public IDisposable Subscribe(IObserver <ListChange <T> > observer)
        {
            var    gate  = new object();
            var    items = new List <IDisposable>();
            Action clear = () =>
            {
                Disposable.Combine(items).Dispose();
                items.Clear();
            };

            return(Disposable.Combine(
                       _source.Subscribe(
                           Observer.Create <ListChange <T> >(
                               onNext: changes =>
            {
                lock (gate)
                {
                    observer.OnNext(changes);
                    changes(
                        insert: (index, item) => items.Insert(index, _getDisposable(item)),
                        remove: index =>
                    {
                        items[index].Dispose();
                        items.RemoveAt(index);
                    },
                        replace: (index, item) =>
                    {
                        items[index].Dispose();
                        items[index] = _getDisposable(item);
                    },
                        clear: clear);
                }
            },
                               onError: error =>
            {
                lock (gate)
                {
                    observer.OnError(error);
                    clear();
                }
            },
                               onCompleted: () =>
            {
                lock (gate)
                    observer.OnCompleted();
            })),
                       Disposable.Create(clear)));
        }
Beispiel #3
0
        public IDisposable Connect()
        {
            lock (_gate)
            {
                if (_state != State.Unconnected)
                {
                    throw new Exception("Can't connect an already connected ReplayObservableList");
                }

                _state = State.Connected;

                var subscription = _source.Subscribe(
                    Observer.Create <ListChange <T> >(
                        onNext: change =>
                {
                    lock (_gate)
                    {
                        _value = change.Apply(_value);

                        foreach (var observer in _observers)
                        {
                            observer.OnNext(change);
                        }
                    }
                },
                        onError: error =>
                {
                    lock (_gate)
                    {
                        _state = State.Error;
                        _error = error;

                        foreach (var observer in _observers)
                        {
                            observer.OnError(error);
                        }
                        _observers = ImmutableList <IObserver <ListChange <T> > > .Empty;
                    }
                },
                        onCompleted: () =>
                {
                    lock (_gate)
                    {
                        _state = State.Completed;

                        foreach (var observer in _observers)
                        {
                            observer.OnCompleted();
                        }
                        _observers = ImmutableList <IObserver <ListChange <T> > > .Empty;
                    }
                }));

                return(Disposable.Combine(
                           subscription,
                           Disposable.Create(
                               () =>
                {
                    _state = State.Unconnected;
                    _value = ImmutableList <T> .Empty;
                    _observers = ImmutableList <IObserver <ListChange <T> > > .Empty;
                })));
            }
        }