Exemple #1
0
        public IProgressObserver Create(ProgressStartEvent begin, Func <Exception, ProgressEndEvent>?onError = null, Func <ProgressEndEvent>?onComplete = null)
        {
            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            if (begin.ProgressId is null)
            {
                begin = begin with {
                    ProgressId = new ProgressToken(Guid.NewGuid().ToString())
                };
            }

            if (_activeObservers.TryGetValue(begin.ProgressId, out var item))
            {
                return(item);
            }

            onError ??= error => new ProgressEndEvent {
                Message = error.ToString()
            };

            onComplete ??= () => new ProgressEndEvent();

            var cts      = new CancellationTokenSource();
            var observer = new ProgressObserver(
                _router,
                begin,
                onError,
                onComplete,
                cts.Token
                );

            _activeObservers.TryAdd(observer.ProgressId, observer);
            _activeObserverTokens.TryAdd(observer.ProgressId, cts);

            return(observer);
        }
        public IProgressObserver Create(ProgressStartEvent begin, Func <Exception, ProgressEndEvent>?onError = null, Func <ProgressEndEvent>?onComplete = null)
        {
            if (EqualityComparer <ProgressToken> .Default.Equals(begin.ProgressId, default))
            {
                begin.ProgressId = new ProgressToken(Guid.NewGuid().ToString());
            }

            if (_activeObservers.TryGetValue(begin.ProgressId, out var item))
            {
                return(item);
            }

            onError ??= error => new ProgressEndEvent {
                Message = error.ToString()
            };

            onComplete ??= () => new ProgressEndEvent();

            var cts      = new CancellationTokenSource();
            var observer = new ProgressObserver(
                _router,
                begin,
                onError,
                onComplete,
                cts.Token
                );

            _activeObservers.TryAdd(observer.ProgressId, observer);
            _activeObserverTokens.TryAdd(observer.ProgressId, cts);

            return(observer);
        }