Last active
December 13, 2015 19:19
-
-
Save jwatte/4962218 to your computer and use it in GitHub Desktop.
Doing reactive/data-flow programming in C# is ludicrously verbose.
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
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