Created
September 9, 2011 22:31
-
-
Save GeorgeTsiokos/1207493 to your computer and use it in GitHub Desktop.
TaskAsyncHelper not needed with Rx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See Observable.Empty() | |
public static Task Empty { | |
get { | |
// we have to return a new one every time, other wise the task will be disposed | |
return MakeEmpty(); | |
} | |
} | |
// See Observable.Catch() | |
public static Task Catch(this Task task) { | |
return task.ContinueWith(t => { | |
if (t != null && t.IsFaulted) { | |
var ex = t.Exception; | |
Trace.TraceError("SignalR exception thrown by Task: {0}", ex); | |
} | |
return t; | |
}).Unwrap(); | |
} | |
// See Observable.Catch() | |
public static Task<T> Catch<T>(this Task<T> task) { | |
return task.ContinueWith(t => { | |
if (t != null && t.IsFaulted) { | |
var ex = t.Exception; | |
Trace.TraceError("SignalR exception thrown by Task: {0}", ex); | |
} | |
return t; | |
}) | |
.Unwrap(); | |
} | |
// See Observable.Select() | |
public static Task Success(this Task task, Action<Task> successor) { | |
return task.ContinueWith(_ => { | |
if (task.IsCanceled || task.IsFaulted) { | |
return task; | |
} | |
return Task.Factory.StartNew(() => successor(task)); | |
}).Unwrap(); | |
} | |
// See Observable.Select() | |
public static Task Success<TResult>(this Task<TResult> task, Action<Task<TResult>> successor) { | |
return task.ContinueWith(_ => { | |
if (task.IsCanceled || task.IsFaulted) { | |
return task; | |
} | |
return Task.Factory.StartNew(() => successor(task)); | |
}).Unwrap(); | |
} | |
// See Observable.Select() | |
public static Task<TResult> Success<TResult>(this Task task, Func<Task, TResult> successor) { | |
return task.ContinueWith(_ => { | |
if (task.IsFaulted) { | |
return FromError<TResult>(task.Exception); | |
} | |
if (task.IsCanceled) { | |
return Cancelled<TResult>(); | |
} | |
return Task.Factory.StartNew(() => successor(task)); | |
}).Unwrap(); | |
} | |
// See Observable.Select() | |
public static Task<TResult> Success<T, TResult>(this Task<T> task, Func<Task<T>, TResult> successor) { | |
return task.ContinueWith(_ => { | |
if (task.IsFaulted) { | |
return FromError<TResult>(task.Exception); | |
} | |
if (task.IsCanceled) { | |
return Cancelled<TResult>(); | |
} | |
return Task.Factory.StartNew(() => successor(task)); | |
}).Unwrap(); | |
} | |
// See Observable.Join().Concat(Observable.Defer()) | |
public static Task AllSucceeded(this Task[] tasks, Action continuation) { | |
return AllSucceeded(tasks, _ => continuation()); | |
} | |
// See Observable.Join().Select() | |
public static Task AllSucceeded(this Task[] tasks, Action<Task[]> continuation) { | |
return Task.Factory.ContinueWhenAll(tasks, _ => { | |
var cancelledTask = tasks.FirstOrDefault(task => task.IsCanceled); | |
if (cancelledTask != null) | |
throw new TaskCanceledException(); | |
var allExceptions = | |
tasks.Where(task => task.IsFaulted).SelectMany(task => task.Exception.InnerExceptions).ToList(); | |
if (allExceptions.Count > 0) { | |
throw new AggregateException(allExceptions); | |
} | |
return Task.Factory.StartNew(() => continuation(tasks)); | |
}).Unwrap(); | |
} | |
// See Observable.Join().Concat(Observable.Defer()) | |
public static Task<T> AllSucceeded<T>(this Task[] tasks, Func<T> continuation) { | |
return Task.Factory.ContinueWhenAll(tasks, _ => { | |
var cancelledTask = tasks.FirstOrDefault(task => task.IsCanceled); | |
if (cancelledTask != null) | |
throw new TaskCanceledException(); | |
var allExceptions = | |
tasks.Where(task => task.IsFaulted).SelectMany(task => task.Exception.InnerExceptions).ToList(); | |
if (allExceptions.Count > 0) { | |
throw new AggregateException(allExceptions); | |
} | |
return Task.Factory.StartNew(continuation); | |
}).Unwrap(); | |
} | |
// See Observable.Return() | |
public static Task<T> FromResult<T>(T value) { | |
var tcs = new TaskCompletionSource<T>(); | |
tcs.SetResult(value); | |
return tcs.Task; | |
} | |
// See Observable.Throw<T>() | |
private static Task<T> FromError<T>(Exception e) { | |
var tcs = new TaskCompletionSource<T>(); | |
tcs.SetException(e); | |
return tcs.Task; | |
} | |
// No direct, but See Observable.Return(false) | |
private static Task<T> Cancelled<T>() { | |
var tcs = new TaskCompletionSource<T>(); | |
tcs.SetCanceled(); | |
return tcs.Task; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment