Inheritance: IDisposable
Ejemplo n.º 1
0
        public void Subscribe(ISingleSubscriber <R> s)
        {
            ISingleSubscriber <T> sr;

            try
            {
                sr = onLift(s);
            }
            catch (Exception ex)
            {
                EmptyDisposable.Error(s, ex);
                return;
            }
            source.Subscribe(sr);
        }
        public void OnSubscribe(IDisposable d)
        {
            try
            {
                onSubscribeCall(d);
            }
            catch (Exception e)
            {
                d.Dispose();
                EmptyDisposable.Error(actual, e);
                return;
            }

            actual.OnSubscribe(d);
        }
Ejemplo n.º 3
0
        public static ISingle <T> Defer <T>(Func <ISingle <T> > singleSupplier)
        {
            return(Create <T>(s =>
            {
                ISingle <T> single;

                try {
                    single = singleSupplier();
                } catch (Exception e)
                {
                    EmptyDisposable.Error(s, e);
                    return;
                }
                single.Subscribe(s);
            }));
        }
Ejemplo n.º 4
0
        public static ICompletable Defer(Func <ICompletable> factory)
        {
            return(Create(cs =>
            {
                ICompletable c;

                try {
                    c = factory();
                } catch (Exception e)
                {
                    EmptyDisposable.Error(cs, e);
                    return;
                }

                c.Subscribe(cs);
            }));
        }
Ejemplo n.º 5
0
        public static ICompletable Using <S>(
            Func <S> stateSupplier,
            Func <S, ICompletable> completableFactory,
            Action <S> stateDisposer,
            bool eager = true)
        {
            return(Create(cs =>
            {
                S state;

                try
                {
                    state = stateSupplier();
                }
                catch (Exception ex)
                {
                    EmptyDisposable.Error(cs, ex);
                    return;
                }

                ICompletable c;

                try
                {
                    c = completableFactory(state);
                }
                catch (Exception e)
                {
                    try
                    {
                        stateDisposer(state);
                    }
                    catch (Exception ex)
                    {
                        EmptyDisposable.Error(cs, new AggregateException(e, ex));
                        return;
                    }

                    EmptyDisposable.Error(cs, e);
                    return;
                }

                c.Subscribe(new UsingCompletableSubscriber(cs, eager, () => stateDisposer(state)));
            }));
        }
Ejemplo n.º 6
0
        public static ISingle <T> Throw <T>(Func <Exception> errorSupplier)
        {
            return(Create <T>(s =>
            {
                Exception e;

                try
                {
                    e = errorSupplier();
                }
                catch (Exception ex)
                {
                    EmptyDisposable.Error(s, ex);
                    return;
                }
                EmptyDisposable.Error(s, e);
            }));
        }
Ejemplo n.º 7
0
        public async Task <JobResult> RunAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            IDisposable scope = new EmptyDisposable();

            if (!_runningContinuous)
            {
                scope = _logger.BeginScope(s => s.Property("job", _jobName));
            }

            using (scope) {
                _logger.Trace("Job run \"{0}\" starting...", _jobName);

                using (var lockValue = await GetJobLockAsync().AnyContext()) {
                    if (lockValue == null)
                    {
                        return(JobResult.SuccessWithMessage("Unable to acquire job lock."));
                    }

                    var result = await TryRunAsync(new JobRunContext(lockValue, cancellationToken)).AnyContext();

                    if (result != null)
                    {
                        if (!result.IsSuccess)
                        {
                            _logger.Error(result.Error, "Job run \"{0}\" failed: {1}", GetType().Name, result.Message);
                        }
                        else if (!String.IsNullOrEmpty(result.Message))
                        {
                            _logger.Info("Job run \"{0}\" succeeded: {1}", GetType().Name, result.Message);
                        }
                        else
                        {
                            _logger.Trace("Job run \"{0}\" succeeded.", GetType().Name);
                        }
                    }
                    else
                    {
                        _logger.Error("Null job run result for \"{0}\".", GetType().Name);
                    }

                    return(result);
                }
            }
        }
Ejemplo n.º 8
0
        public static ISingle <R> Zip <T, R>(this IEnumerable <ISingle <T> > sources, Func <T[], R> zipper)
        {
            return(Create <R>(s =>
            {
                int n = 0;

                ISingle <T>[] a = new ISingle <T> [8];

                foreach (ISingle <T> source in sources)
                {
                    if (n == a.Length)
                    {
                        ISingle <T>[] b = new ISingle <T> [n + (n >> 2)];
                        Array.Copy(a, 0, b, 0, n);
                        a = b;
                    }
                    a[n++] = source;
                }

                if (n == 0)
                {
                    EmptyDisposable.Error(s, NoSuchElementException());
                    return;
                }

                T[] array = new T[n];
                int[] counter = { n };

                SetCompositeDisposable all = new SetCompositeDisposable();

                s.OnSubscribe(all);

                for (int i = 0; i < n; i++)
                {
                    if (all.IsDisposed())
                    {
                        return;
                    }

                    a[i].Subscribe(new ZipSingleSubscriber <T, R>(s, i, array, counter, all, zipper));
                }
            }));
        }
Ejemplo n.º 9
0
        public static ISingle <T> Using <T, S>(Func <S> stateSupplier, Func <S, ISingle <T> > singleFactory, Action <S> stateDisposer, bool eager = false)
        {
            return(Create <T>(s =>
            {
                S state;

                try
                {
                    state = stateSupplier();
                }
                catch (Exception e)
                {
                    EmptyDisposable.Error(s, e);
                    return;
                }

                ISingle <T> source;

                try {
                    source = singleFactory(state);
                }
                catch (Exception e)
                {
                    try
                    {
                        stateDisposer(state);
                    }
                    catch (Exception ex)
                    {
                        EmptyDisposable.Error(s, new AggregateException(e, ex));
                        return;
                    }

                    EmptyDisposable.Error(s, e);
                    return;
                }

                source.Subscribe(new UsingSingleSubscriber <T>(s, () => stateDisposer(state), eager));
            }));
        }