proposal-signals / signal-utils

MIT License
69 stars 6 forks source link

Add batchedEffect() #69

Closed justinfagnani closed 1 month ago

justinfagnani commented 1 month ago

This adds a batchedEffect(cb) utility that synchronously runs its callback when dependencies are updated inside a batch() call.

const a = new Signal.State(0);
const b = new Signal.State(0);

batchedEffect(() => {
  console.log("a + b =", a.get() + b.get());
});

// Logs: a + b = 0

batch(() => {
  a.set(1);
  b.set(1);
});

// Logs: a + b = 2