Skip to content

Instantly share code, notes, and snippets.

@qbit86
Last active August 29, 2015 14:23
Show Gist options
  • Save qbit86/7f018afe0a233fced2cc to your computer and use it in GitHub Desktop.
Save qbit86/7f018afe0a233fced2cc to your computer and use it in GitHub Desktop.
Future
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