Пример #1
0
        /// <summary>
        /// Creates a task that will complete when all of the supplied tasks have completed.
        /// </summary>
        public static IAwaitable <T[]> WhenAll <T>(params IAwaitable <T>[] inputs)
        {
            var result = new Awaitable <T[]>();

            var set    = new HashSet <IAwaitable <T> >(inputs);
            var output = new T[inputs.Length];
            var i      = 0;

            foreach (var input in set)
            {
                var index = i;
                var ip    = input;
                ip.Subscribe(v =>
                {
                    output[index] = v;
                    if (set.Remove(ip) && set.Count == 0)
                    {
                        if (!result.IsCompleted)
                        {
                            result.Emit(output);
                        }
                    }
                });

                i++;
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Projects the result of the task into a new form.
        /// </summary>
        public static IAwaitable <TResult> Select <TResult>(this IAwaitable input, Func <TResult> f)
        {
            var result = new Awaitable <TResult>();

            input.Subscribe(() => result.Emit(f()));
            return(result);
        }
Пример #3
0
        /// <summary>
        /// Creates a task that completes (or cancels) when either the input task completes or the cancellation token is signalled.
        /// On cancellation, the original task still runs to completion because there is no way to preemptively cancel it.
        /// </summary>
        public static IAwaitable WithCancellation(this IAwaitable input, CancellationToken ct)
        {
            var result = new Awaitable(ct);

            input.Subscribe(() => result.Emit());
            return(result);
        }
Пример #4
0
        /// <summary>
        /// Creates a task that completes (or cancels) when either the input task completes or the cancellation token is signalled.
        /// On cancellation, the original task still runs to completion because there is no way to preemptively cancel it.
        /// </summary>
        public static IAwaitable <T> WithCancellation <T>(this IAwaitable <T> input, CancellationToken ct)
        {
            var result = new Awaitable <T>(ct);

            input.Subscribe(v => result.Emit(v));
            return(result);
        }
Пример #5
0
        /// <summary>
        /// Creates a continuation that executes when the target task completes.
        /// </summary>
        public static IAwaitable <TResult> ContinueWith <TResult>(this IAwaitable awaitable, Func <TResult> fun)
        {
            var result = new Awaitable <TResult>();

            awaitable.Subscribe(() =>
            {
                result.Emit(fun());
            });

            return(result);
        }
Пример #6
0
        /// <summary>
        /// Creates a continuation that executes when the target task completes.
        /// </summary>
        public static IAwaitable ContinueWith <TResult>(this IAwaitable awaitable, Action fun)
        {
            var result = new Awaitable();

            awaitable.Subscribe(() =>
            {
                fun();
                result.Emit();
            });

            return(result);
        }
Пример #7
0
        /// <summary>
        /// Creates a continuation that executes when the target task completes.
        /// </summary>
        public static IAwaitable ContinueWith <T>(this IAwaitable <T> awaitable, Action <T> action)
        {
            var result = new Awaitable();

            awaitable.Subscribe(v =>
            {
                action(v);
                result.Emit();
            });

            return(result);
        }
Пример #8
0
        static async void Run(Awaitable <int> a, Awaitable <int> b, CancellationToken ct)
        {
            try
            {
                var i = await Await.WhenAny(a, b).WithCancellation(ct);

                Console.WriteLine("got: {0}", i);
            }
            catch (OperationCanceledException)
            {
                Report.Line("cancelled");
            }
        }
Пример #9
0
        /// <summary>
        /// </summary>
        public static void Run()
        {
            var c  = new CancellationTokenSource();
            var ct = c.Token;

            var a = new Awaitable <int>();
            var b = new Awaitable <int>();

            Run(a, b, ct);

            //c.Cancel();
            a.Emit(1);
            b.Emit(2);

            Console.ReadLine();
        }
Пример #10
0
        /// <summary>
        /// Creates a task that will complete when any of the supplied tasks have completed.
        /// </summary>
        public static IAwaitable <IAwaitable <T> > WhenAny <T>(params IAwaitable <T>[] inputs)
        {
            var result = new Awaitable <IAwaitable <T> >();

            foreach (var i in inputs)
            {
                i.Subscribe(v =>
                {
                    if (!result.IsCompleted)
                    {
                        result.Emit(i);
                    }
                });
            }

            return(result);
        }
Пример #11
0
        /// <summary>
        /// Creates a task that will complete when all of the supplied tasks have completed.
        /// </summary>
        public static IAwaitable WhenAll(params IAwaitable[] inputs)
        {
            var result = new Awaitable();

            var set = new HashSet <IAwaitable>(inputs);

            foreach (var input in set)
            {
                var ip = input;
                ip.Subscribe(() =>
                {
                    if (set.Remove(ip) && set.Count == 0)
                    {
                        if (!result.IsCompleted)
                        {
                            result.Emit();
                        }
                    }
                });
            }

            return(result);
        }
Пример #12
0
 public Awaiter(Awaitable source)
 {
     m_source = source;
 }