kekekeks / NObservable

MobX like observable state management library with Blazor support
MIT License
68 stars 7 forks source link

Confused by sample on readme.. #4

Closed dazinator closed 5 years ago

dazinator commented 5 years ago

I'm a bit confused by this sample on the readme - well I think there is a mistake possibly?

var o = new Foo{Prop1 = 1, Prop2 = 1};
Observe.Autorun(() => {
     Console.WriteLine($"Prop1: {o.Prop1}");
     if(o.Prop1 == 3)
        Console.WriteLine($"Prop1: {o.Prop1} Prop2: {o.Prop2}");
     else
        Console.WriteLine($"Prop1: {o.Prop1});    
});

o.Prop1 = 2;
o.Prop2 = 2;

o.Prop1 = 3;
o.Prop2 = 3;
Console output:

Prop1: 1
Prop1: 2
Prop1: 3, Prop2: 2
Prop1: 3, Prop2: 3

After reading this, and coming from a knockoutjs background, I was expecting the output to be

// initial execution, Prop1 = 1
Prop1: 1
Prop1: 1

// execution becuase Prop1 = 2
Prop1: 2
Prop1: 2

// no execution because Prop2 = 2 but not tracked.

// execution because Prop1 = 3, this also means Prop2 now gets tracked.
Prop1: 3, Prop2: 2

// execution because Prop2 = 3
Prop1: 3, Prop2: 3

If you remove the very first Console.WriteLine() before the if statement, then I think i'd agree with you regarding the console output.. am I missing something?

kekekeks commented 5 years ago

I'm terribly sorry, README.md has incorrect information. The actual output of the code:

Initial run
Prop1: 1
Prop1: 1
Setting Prop1 = 2
Prop1: 2
Prop1: 2
Setting Prop2 = 2
Setting Prop1 = 3
Prop1: 3
Prop1: 3 Prop2: 2
Setting Prop2 = 3
Prop1: 3
Prop1: 3 Prop2: 3
kekekeks commented 5 years ago

Updated readme to make it more clear what is happening and why.

dazinator commented 5 years ago

Ok cool that makes sense, cheers