public IDisposable Subscribe(IObserver <DictionaryNotification <TKey, TValue> > observer)
        {
            var futureDisposable = new SingleAssignmentDisposable();

            var scheduledWorkDisposable = _scheduler.Schedule(() =>
            {
                if (_isDisposed)
                {
                    futureDisposable.Disposable =
                        Observable.Throw <DictionaryNotification <TKey, TValue> >(
                            new ObjectDisposedException(this.GetType().Name)).Subscribe(observer);
                }

                if (_error != null)
                {
                    futureDisposable.Disposable = Observable.Throw <DictionaryNotification <TKey, TValue> >(_error).Subscribe(observer);
                }

                var existingValues = _state.ToList();

                foreach (var existingValue in existingValues)
                {
                    observer.OnNext(DictionaryNotification.Existing(existingValue.Key, existingValue.Value));
                }
                observer.OnNext(DictionaryNotification.Initialised <TKey, TValue>());

                futureDisposable.Disposable = new CompositeDisposable(_subject.Subscribe(observer), Connect());
            });

            return(new CompositeDisposable(scheduledWorkDisposable, futureDisposable));
        }
예제 #2
0
        public IDisposable Subscribe(IObserver <DictionaryNotification <TKey, TValue> > observer)
        {
            lock (_lock)
            {
                if (_isDisposed)
                {
                    return
                        (Observable.Throw <DictionaryNotification <TKey, TValue> >(
                             new ObjectDisposedException(this.GetType().Name)).Subscribe(observer));
                }

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

                var existingValues = _state.ToList();

                foreach (var existingValue in existingValues)
                {
                    observer.OnNext(DictionaryNotification.Existing(existingValue.Key, existingValue.Value));
                }
                observer.OnNext(DictionaryNotification.Initialised <TKey, TValue>());

                return(new CompositeDisposable(_subject.Subscribe(observer), Connect()));
            }
        }
 public IObservable <DictionaryNotification <TKey, TValue> > Get(TKey key)
 {
     return(Observable.Create <DictionaryNotification <TKey, TValue> >(observer =>
     {
         TValue existingValue;
         if (_state.TryGetValue(key, out existingValue))
         {
             observer.OnNext(DictionaryNotification.Existing(key, existingValue));
         }
         else
         {
             observer.OnNext(DictionaryNotification.Missing <TKey, TValue>(key));
         }
         return new CompositeDisposable(_subject
                                        .Where(dn => dn.Type == DictionaryNotificationType.KeyCleared || Equals(dn.Key, key))
                                        .Subscribe(observer), Connect());
     })
            .SubscribeOn(_scheduler));
 }
예제 #4
0
 public IObservable <DictionaryNotification <TKey, TValue> > Get(TKey key)
 {
     return(Observable.Create <DictionaryNotification <TKey, TValue> >(observer =>
     {
         lock (_lock)
         {
             TValue existingValue;
             if (_state.TryGetValue(key, out existingValue))
             {
                 observer.OnNext(DictionaryNotification.Existing(key, existingValue));
             }
             else
             {
                 observer.OnNext(DictionaryNotification.Missing <TKey, TValue>(key));
             }
             return new CompositeDisposable(_subject.Subscribe(observer), Connect());
         }
     }));
 }