Ejemplo n.º 1
0
        private Task ExecuteHubEvent(IRequest request, string connectionId, Func <IHub, Task> action)
        {
            var hubs       = GetHubs(request, connectionId).ToList();
            var operations = hubs.Select(instance => action(instance).OrEmpty().Catch(Trace)).ToArray();

            if (operations.Length == 0)
            {
                DisposeHubs(hubs);
                return(TaskAsyncHelper.Empty);
            }

            var tcs = new DispatchingTaskCompletionSource <object>();

            Task.Factory.ContinueWhenAll(operations, tasks =>
            {
                DisposeHubs(hubs);
                var faulted = tasks.FirstOrDefault(t => t.IsFaulted);
                if (faulted != null)
                {
                    tcs.SetUnwrappedException(faulted.Exception);
                }
                else if (tasks.Any(t => t.IsCanceled))
                {
                    tcs.SetCanceled();
                }
                else
                {
                    tcs.SetResult(null);
                }
            });

            return(tcs.Task);
        }