Exemple #1
0
        private IDisposable Subscribe(IObserver <CollectionNotification <T> > observer, bool startWithExisting)
        {
            Contract.Requires(observer != null);
            Contract.Ensures(Contract.Result <IDisposable>() != null);

            lock (gate)
            {
                if (isDisposed)
                {
                    return(Observable
                           .Throw <CollectionNotification <T> >(new ObjectDisposedException("ListSubject<T>"))
                           .Subscribe(observer));
                }

                if (exception != null)
                {
                    return(Observable
                           .Throw <CollectionNotification <T> >(exception)
                           .Subscribe(observer));
                }

                if (startWithExisting)
                {
                    IList <T> clonedList = list.ToList().AsReadOnly();

                    observer.OnNext(CollectionNotification.CreateExists(clonedList));
                }

                return(subject.Subscribe(observer));
            }
        }
Exemple #2
0
        /// <summary>
        /// Returns an observable sequence of collection notifications that represent changes to the specified <paramref name="key"/>,
        /// starting with the existing value if the dictionary already contains the <paramref name="key"/>.
        /// </summary>
        /// <param name="key">The key for which changes are to be observed.</param>
        /// <returns>
        /// An observable sequence of collection notifications that represent changes to the specified <paramref name="key"/>,
        /// starting with the existing value if the dictionary already contains the <paramref name="key"/>.
        /// </returns>
        public IObservable <CollectionNotification <KeyValuePair <TKey, TValue> > > Changes(TKey key)
        {
            return(Observable.Create <CollectionNotification <KeyValuePair <TKey, TValue> > >(
                       observer =>
            {
                lock (gate)
                {
                    if (isDisposed)
                    {
                        return Observable
                        .Throw <CollectionNotification <KeyValuePair <TKey, TValue> > >(new ObjectDisposedException("DictionarySubject<TKey, TValue>"))
                        .Subscribe(observer);
                    }

                    if (exception != null)
                    {
                        return Observable
                        .Throw <CollectionNotification <KeyValuePair <TKey, TValue> > >(exception)
                        .Subscribe(observer);
                    }

                    if (dictionary.Contains(key))
                    {
                        observer.OnNext(CollectionNotification.CreateExists(dictionary[key]));
                    }

                    var comparer = dictionary.Comparer;

                    return subject
                    .Where(change => change.HasValue && comparer.Equals(change.Value.Key, key))
                    .Subscribe(observer);
                }
            }));
        }
        public static ReadOnlyDictionarySubject <TKey, TResult> Collect <TSource, TKey, TResult>(
            this IObservable <TSource> existing,
            Func <TSource, TKey> keySelector,
            IObservable <CollectionModification <KeyValuePair <TKey, TSource> > > changes,
            Func <IObservable <CollectionNotification <KeyValuePair <TKey, TSource> > >, IObservable <CollectionModification <KeyValuePair <TKey, TResult> > > > selector,
            IEqualityComparer <TKey> comparer)
        {
            Contract.Requires(existing != null);
            Contract.Requires(keySelector != null);
            Contract.Requires(changes != null);
            Contract.Requires(selector != null);
            Contract.Requires(comparer != null);
            Contract.Ensures(Contract.Result <ReadOnlyDictionarySubject <TKey, TResult> >() != null);

            return(new ReadOnlyDictionarySubject <TKey, TResult>(
                       Collect(
                           d => new DictionarySubject <TKey, TResult>(d, comparer),
                           existing,
                           changes,
                           keySelector,
                           pair => pair.Key,
                           (key, value) => CollectionNotification.CreateExists(new KeyValuePair <TKey, TSource>(key, value)),
                           selector,
                           comparer)));
        }
Exemple #4
0
        public static ReadOnlyListSubject <TResult> Collect <TSource, TResult>(
            this IObservable <TSource> existing,
            IObservable <CollectionModification <TSource> > changes,
            Func <IObservable <CollectionNotification <TSource> >, IObservable <CollectionModification <TResult> > > selector,
            IEqualityComparer <TSource> comparer)
        {
            Contract.Requires(existing != null);
            Contract.Requires(changes != null);
            Contract.Requires(selector != null);
            Contract.Requires(comparer != null);
            Contract.Ensures(Contract.Result <ReadOnlyListSubject <TResult> >() != null);

            return(new ReadOnlyListSubject <TResult>(
                       Collect(
                           d => new ListSubject <TResult>(d),
                           existing,
                           changes,
                           k => k,
                           k => k,
                           (k, v) => CollectionNotification.CreateExists(v),
                           selector,
                           comparer)));
        }