Пример #1
0
        public AggregateGroupedComparableSubject(IPushObservable <TIn> observable, Func <TAggr, TIn, TAggr> reducer, IEqualityComparer <TIn> equalityComparer, Func <TIn, TAggr> createInitialValue, Func <TIn, TAggr, TOut> resultSelector)
        {
            _resultSelector = resultSelector;
            var _isAggrDisposable = typeof(IDisposable).IsAssignableFrom(typeof(TAggr));

            _subscription = observable.Subscribe(i =>
            {
                lock (_lockSync)
                {
                    if (_hasValue && !equalityComparer.Equals(_currentAggregation.InValue, i))
                    {
                        PushValue(_resultSelector(_currentAggregation.InValue, _currentAggregation.CurrentAggregation));
                    }
                    if (!_hasValue || !equalityComparer.Equals(_currentAggregation.InValue, i))
                    {
                        var aggr = createInitialValue(i);
                        if (_isAggrDisposable)
                        {
                            _disposable.Set(aggr as IDisposable);
                        }
                        _currentAggregation = new DicoAggregateElement <TIn, TAggr> {
                            InValue = i, CurrentAggregation = aggr
                        };
                        _hasValue = true;
                    }
                    _currentAggregation.CurrentAggregation = reducer(_currentAggregation.CurrentAggregation, i);
                }
            }, this.HandleComplete, this.PushException);
        }
Пример #2
0
 public AggregateSubject(IPushObservable <TIn> observable, Func <TAggr, TIn, TAggr> reducer, Func <TIn, TKey> getKey, Func <TIn, TAggr> createInitialValue, Func <TIn, TKey, TAggr, TOut> resultSelector)
 {
     _resultSelector   = resultSelector;
     _isAggrDisposable = typeof(IDisposable).IsAssignableFrom(typeof(TAggr));
     _subscription     = observable.Subscribe(i =>
     {
         lock (_lockSync)
         {
             TKey key = getKey(i);
             if (key != null)
             {
                 if (!_dictionary.TryGetValue(key, out DicoAggregateElement <TIn, TAggr> aggr))
                 {
                     aggr = new DicoAggregateElement <TIn, TAggr> {
                         CurrentAggregation = createInitialValue(i), InValue = i
                     };
                     //aggr.CurrentAggregation = createInitialValue(i);
                     _dictionary[key] = aggr;
                     if (_isAggrDisposable)
                     {
                         _disposable.Set(aggr as IDisposable);
                     }
                 }
                 aggr.CurrentAggregation = reducer(aggr.CurrentAggregation, i);
                 _dictionary[key]        = aggr;
             }
         }
     }, this.HandleComplete, this.PushException);
 }