static void Main(string[] args) { var o = new Foo { Prop1 = 1, Prop2 = 1 }; Console.WriteLine("Initial run"); Observe.Autorun(() => { if (o.Prop1 == 3) { Console.WriteLine($"Prop1: {o.Prop1} Prop2: {o.Prop2}"); } else { Console.WriteLine($"Prop1: {o.Prop1}"); } }); Console.WriteLine("Setting Prop1 = 2, expecting update"); o.Prop1 = 2; Console.WriteLine("Setting Prop2 = 2, it wasn't read last time, not expecting update"); o.Prop2 = 2; Console.WriteLine("Setting Prop1 = 3, expecting update"); o.Prop1 = 3; Console.WriteLine("Setting Prop2 = 3, it was read last time, expecting update"); o.Prop2 = 3; }
public void Autorun_Should_Be_Triggered_On_Property_Set() { var p = new PropertyHelper <int>(1); var p2 = new PropertyHelper <int>(2); p.Value = 1; var sequence = new List <int>(); Observe.Autorun(() => { sequence.Add(p.Value); if (p.Value == 10) { sequence.Add(p2.Value); } }); p.Value = 2; p2.Value = 2; p.Value = 3; p2.Value = 3; p.Value = 10; p2.Value = 10; Assert.True(sequence.SequenceEqual(new[] { 1, 2, 3, 10, 3, 10, 10 })); }
public void Multiple_Property_Changes_Should_Be_Grouped_Inside_Action() { var p1 = new PropertyHelper <int>(1) { Value = 1 }; var p2 = new PropertyHelper <int>(2) { Value = 1 }; var seq = new List <int>(); Observe.Autorun(() => { seq.Add(p1.Value); seq.Add(p2.Value); }); Observe.RunInAction(() => { p1.Value = 2; p2.Value = 2; }); Observe.RunInAction(() => { p1.Value = 3; p1.Value = 4; }); Assert.True(seq.SequenceEqual(new [] { 1, 1, 2, 2, 4, 2 })); }
public void Autorun_Should_Be_Unsubcribeable() { var p = new PropertyHelper <int>(1) { Value = 1 }; var seq = new List <int>(); var d = Observe.Autorun(() => seq.Add(p.Value)); p.Value = 2; d.Dispose(); p.Value = 3; Assert.True(seq.SequenceEqual(new [] { 1, 2 })); }
public void Autorun_Should_Be_Able_To_Unsubcribe() { var p = new PropertyHelper <int>(1) { Value = 1 }; var seq = new List <int>(); IDisposable d = null; d = Observe.Autorun(() => { seq.Add(p.Value); if (p.Value == 2) { d.Dispose(); } }); p.Value = 2; p.Value = 3; Assert.True(seq.SequenceEqual(new [] { 1, 2 })); }