Skip to content

Instantly share code, notes, and snippets.

@jwatte
Last active December 13, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jwatte/4962218 to your computer and use it in GitHub Desktop.
Save jwatte/4962218 to your computer and use it in GitHub Desktop.
Doing reactive/data-flow programming in C# is ludicrously verbose.
public class DrawingFeature : Feature {
public ReactiveProperty<Drawing> Drawing = new ReactiveProperty<Drawing>(this.OnFeatureChanged);
}
// use: theDrawingFeature.Drawing.It = ...;
// theDrawingFeature.Drawing.Change += new EventHandler( ... );
public class ReactiveProperty<T>
{
public ReactiveProperty(ParentChange del)
{
parent_ = del;
}
private ParentChange parent_;
private T it_;
public event EventHandler Changed;
private bool inChanged_;
protected void OnChanged()
{
if (!inChanged_)
{
inChanged_ = true;
try
{
if (Changed != null)
{
Changed(this, EventArgs.Empty);
}
if (parent_)
{
parent_();
}
}
finally
{
inChanged_ = false;
}
}
}
public T It
{
get
{
return it_;
}
set
{
it_ = value;
OnChanged();
}
}
}
public delegate void ParentChanged();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment