Skip to content

batchedEffect() stops running after first update outside batch() #101

Description

@medmunds

The documentation for batchedEffect() says: "The effect also runs asynchronously, on the microtask queue, if any of the signals it depends on have been updated outside of a batch() call."

This works once, but after the first update outside of batch() the effect no longer runs for any additional signal updates (whether inside or outside a batch() call).

import { Signal } from "signal-polyfill"; // 0.2.2
import { batch, batchedEffect } from "signal-utils/subtle/batched-effect"; // 0.21.1

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 (single, synchronous batched effect)

a.set(2); // update outside batch(), logs a + b = 3
await 0; // flush microtask queue

// Bug: after an update outside batch(), batchedEffect no longer runs
b.set(2); // should log a + b = 4; actually logs nothing

It looks like maybe the async case fails to re-enable the watcher.

Demo: https://codepen.io/mvedmunds/pen/xbOpbXV?editors=0011

Using signal-polyfill@0.2.2 and signal-utils@0.21.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions