Last active
August 29, 2015 13:56
-
-
Save ismaelhamed/8851435 to your computer and use it in GitHub Desktop.
Telerik's Everlive SDK for Windows Phone 7.5 is missing ExecuteAsync and TryExecuteAsync methods, and thus support for await/async calls.
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
using System.Threading.Tasks; | |
using Telerik.Everlive.Sdk.Core.Result; | |
namespace Telerik.Everlive.Sdk.Core.Facades | |
{ | |
public static class FacadesExtensions | |
{ | |
public static Task<T> ExecuteAsync<T>(this AsyncFacade<T> facade) | |
{ | |
var tcs = new TaskCompletionSource<T>(); | |
facade.Execute(result => tcs.TrySetResult(result)); | |
return tcs.Task; | |
} | |
public static Task<T> ExecuteAsync<T>(this BaseFacade<T> facade) | |
{ | |
var tcs = new TaskCompletionSource<T>(); | |
facade.TryExecute(result => HandleResult(tcs, result)); | |
return tcs.Task; | |
} | |
public static Task<RequestResult<T>> TryExecuteAsync<T>(this BaseFacade<T> facade) | |
{ | |
var tcs = new TaskCompletionSource<RequestResult<T>>(); | |
facade.TryExecute(result => tcs.TrySetResult(result)); | |
return tcs.Task; | |
} | |
internal static void HandleResult<T>(TaskCompletionSource<T> tcs, RequestResult<T> result) | |
{ | |
if (!result.Success) | |
{ | |
tcs.TrySetException(result.Error); | |
return; | |
} | |
var t1 = (Equals(result.Value, default(T)) ? default(T) : result.Value); | |
tcs.TrySetResult(t1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment