コード例 #1
0
        /// <summary>
        /// Returns a observable of cache changes preceeded with the initital cache state
        /// </summary>
        /// <param name="predicate">The result will be filtered using the specfied predicate.</param>
        /// <returns></returns>
        public IObservable <IChangeSet <TObject, TKey> > Connect(Func <TObject, bool> predicate = null)
        {
            return(Observable.Create <IChangeSet <TObject, TKey> >
                   (
                       observer =>
            {
                if (predicate == null)
                {
                    return Observable.Return(_innerCache.AsInitialUpdates())
                    .Concat(_changes)
                    .SubscribeSafe(observer);
                }

                var updater = new FilteredUpdater <TObject, TKey>(new ChangeAwareCache <TObject, TKey>(), predicate);
                var filtered = updater.Update(_innerCache.AsInitialUpdates(predicate));
                if (filtered.Count != 0)
                {
                    observer.OnNext(filtered);
                }

                return _changes
                .Select(updater.Update)
                .NotEmpty()
                .SubscribeSafe(observer);
            }));
        }
コード例 #2
0
ファイル: DynamicFilter.cs プロジェクト: mgnslndh/DynamicData
        public IObservable <IChangeSet <TObject, TKey> > Run()
        {
            return(Observable.Create <IChangeSet <TObject, TKey> >(observer =>
            {
                var allData = new Cache <TObject, TKey>();
                var filteredData = new ChangeAwareCache <TObject, TKey>();
                var updater = new FilteredUpdater <TObject, TKey>(filteredData, x => false);

                var locker = new object();

                var evaluate = _refilterObservable.
                               Synchronize(locker)
                               .Select(_ => Reevaluate(updater, allData.KeyValues));

                var predicateChanged = _predicateChanged
                                       .Synchronize(locker)
                                       .Select(predicate =>
                {
                    updater = new FilteredUpdater <TObject, TKey>(filteredData, predicate);
                    return Reevaluate(updater, allData.KeyValues);
                });

                var dataChanged = _source
                                  .Finally(observer.OnCompleted)
                                  .Synchronize(locker)
                                  .Select(changes =>
                {
                    allData.Clone(changes);
                    return updater.Update(changes);
                });

                return predicateChanged.Merge(evaluate).Merge(dataChanged).NotEmpty().SubscribeSafe(observer);
            }));
        }
コード例 #3
0
        private IChangeSet <TObject, TKey> Reevaluate(FilteredUpdater <TObject, TKey> updater,
                                                      IEnumerable <KeyValuePair <TKey, TObject> > items)
        {
            var result  = updater.Refresh(items);
            var changes = result.Where(u => u.Reason == ChangeReason.Add || u.Reason == ChangeReason.Remove);

            return(new ChangeSet <TObject, TKey>(changes));
        }
コード例 #4
0
        public IObservable <IChangeSet <TObject, TKey> > Run()
        {
            if (_filter == null)
            {
                return(_source);
            }

            return(Observable.Create <IChangeSet <TObject, TKey> >(observer =>
            {
                var updater = new FilteredUpdater <TObject, TKey>(new ChangeAwareCache <TObject, TKey>(), _filter);
                return _source.Select(updater.Update)
                .NotEmpty()
                .SubscribeSafe(observer);
            }));
        }