[]previous and next page

group

Groups writes like batch but never flushes synchronously: when the outermost group ends, the effects wait for the microtask (or an enclosing batch / flushSync), exactly as after a single signal write.

Groups writes like batch but never flushes synchronously: when the outermost group ends, the effects wait for the microtask (or an enclosing batch / flushSync), exactly as after a single signal write. Store setters use it so that one setStore(...) behaves like one set(); use it for a primitive whose one call makes several writes.

Import#

ts
import { group } from "@shadow-js/core/extend";

Signature#

ts
function group<T>(fn: () => T): T

Parameters#

Name Description
fn The writes to group. Its throw is rethrown after the group ends.

Returns#

What fn returns.

Examples#

ts
import { useSignal } from "@shadow-js/core";
import { group } from "@shadow-js/core/extend";

const [first, setFirst] = useSignal("Ada");
const [last, setLast] = useSignal("Lovelace");
group(() => {
  setFirst("Grace");
  setLast("Hopper");
}); // effects reading both run once, in the next microtask