reaction( () => calculator.a, (value, reaction) => { console.log(`a is changed to ${value}`); } );
reaction( () => calculator.b, (value, reaction) => { console.log(`b is changed to ${value}`); } )
calculator.a = 10; /** first reaction is occured. */ calculator.b = 20; /** the second one is occured. */
reaction() function have a two parameters, The first one is the function signature, which the result of it is the value is changed. and the second one is callback function is occured when the value is changed. and this callback function have a value is changed.
reaction( () => calculator.a, (value, reaction) => { console.log(`a is changed to ${value}`); } );
reaction( () => calculator.b, (value, reaction) => { console.log(`b is changed to ${value}`); } )
/** It's like a cache */ const sum = computed(() => { console.log("I am calculating."); return calculator.a + calculator.b; });
/** sum observe whether two calculator values are changed or not and It will re-calculate itself if they are changed. */ sum.observe(() => calculator.a); sum.observe(() => calculator.b);
/** sum is re-calculating with these code. */ calculator.a = 10; calculator.b = 20;
/** It's not happen */ console.log(sum.value); console.log(sum.value);