Last active
August 29, 2015 14:23
-
-
Save qbit86/7f018afe0a233fced2cc to your computer and use it in GitHub Desktop.
Future
This file contains hidden or 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; | |
namespace Squisqula | |
{ | |
public class Future<T> | |
{ | |
public bool IsCompleted { get { return _isCompleted; } } | |
public T Result | |
{ | |
get | |
{ | |
if (!_isCompleted) | |
throw new InvalidOperationException("Future is not completed."); | |
return _result; | |
} | |
} | |
public event EventHandler Completed; | |
protected void SetResult(T result) | |
{ | |
_result = result; | |
_isCompleted = true; | |
var handler = Completed; | |
if (handler != null) | |
{ | |
handler(this, EventArgs.Empty); | |
} | |
} | |
private bool _isCompleted; | |
private T _result; | |
} | |
public sealed class Promise<T> | |
{ | |
private class FutureImpl<U> : Future<U> | |
{ | |
internal new void SetResult(U result) | |
{ | |
base.SetResult(result); | |
} | |
} | |
public Future<T> Future { get { return _future; } } | |
public void SetResult(T result) | |
{ | |
_future.SetResult(result); | |
} | |
private readonly FutureImpl<T> _future = new FutureImpl<T>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment