milomg / reactively

MIT License
441 stars 23 forks source link

autoStabilize(stabilize) not working #22

Open wighawag opened 2 months ago

wighawag commented 2 months ago

Hey, first of all, really nice library and nice article. Really like the conciseness and the default of not running. Was not understanding why my console.log was not being called and made me look at your nice code :)

One thing I encountered but not sure if it is by design :

By default autoStabilize will defer the effect call and I guess this is in general better as you can make sure you are not reexecuting since all the value would have settled at that point.

But out of curiousity I wanted to test a sync flow and I saw that you also export the stabilize function so I did autoStabilize(stabilize);

See full example here:

      import { reactive, autoStabilize, stabilize } from "@reactively/core";
      autoStabilize(stabilize);
      // autoStabilize();

      const counter = reactive(0);

      reactive(
        () => {
          console.log("COUNTER", counter.value);
        },
        { effect: true }
      );

      const isEven = reactive(() => (counter.value & 1) == 0);
      const render = reactive(
        () => {
          const content = isEven.value ? "even" : "odd";
          console.log("RENDER", content);
          document.body.textContent = content;
        },
        { effect: true }
      );

      counter.value = 1;

      counter.value = 3;

      counter.set(5);

      counter.set(2);

      counter.set(2);

      setInterval(() => {
        console.log(`-----------CHANGING-----------------------`);
        counter.value++;
        console.log(`----------------------------------`);
      }, 2000);

And what happens is that I do not get any RENDER or COUNTER log except for the first values

Is that expected ?