tansu icon indicating copy to clipboard operation
tansu copied to clipboard

Wrong computed value

Open neuronetio opened this issue 8 months ago • 0 comments

import { writable, computed } from "@amadeus-it-group/tansu";

const s = writable(1);

const cs1 = computed(() => {
  s.set(2);
  return s.get() + 1;
});

const cs2 = computed(() => {
  return s.get() + 1;
});

const cs3 = computed(() => {
  s.set(3);
  return s.get() + 1; // should be 4
});

cs1.subscribe((value) => {
  console.log("cs1", value); // 3
});

cs2.subscribe((value) => {
  console.log("cs2", value); // 3
});

cs3.subscribe((value) => {
  console.log("cs3", value); // 3?
});

const firstCS2 = cs2.get();
cs1.get();
const secondCS2 = cs2.get();
console.log(firstCS2 === secondCS2, firstCS2, secondCS2); // true 3 3

console.log("--cs1", cs1.get()); // 3
console.log("--cs2", cs2.get()); // 3
console.log("--cs3", cs3.get()); // 3 - why not 4?

Without any subscirption code works differently too (the same effect as in signal-polyfill)

import { writable, computed } from "@amadeus-it-group/tansu";

const s = writable(1);

const cs1 = computed(() => {
  s.set(2);
  return s.get() + 1;
});

const cs2 = computed(() => {
  return s.get() + 1;
});

const firstCS2 = cs2.get();
cs1.get();
const secondCS2 = cs2.get();
console.log(firstCS2 === secondCS2, firstCS2, secondCS2); // false 2 3

neuronetio avatar May 05 '25 11:05 neuronetio