[]previous and next page

batch (@shadow-js/core/extend)

Runs fn with effects deferred until the outermost batch ends, then runs them synchronously, so several writes wake each effect once. (batch of @shadow-js/core/extend)

Runs fn with effects deferred until the outermost batch ends, then runs them synchronously, so several writes wake each effect once. Reads inside the batch always see fresh values (G-6). Inside a running flush the effects are left to that flush.

Import#

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

Signature#

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

Parameters#

Name Description
fn The writes to batch.

Returns#

What fn returns.

Throws#

  • What fn throws, after the flush. An error of the flush is thrown as flush throws it (including ShadowError INFINITE_LOOP); when fn threw as well, both are thrown together as an AggregateError.

Examples#

ts
import { batch, useSignal } from "@shadow-js/core";

const [first, setFirst] = useSignal("Ada");
const [last, setLast] = useSignal("Lovelace");
batch(() => {
  setFirst("Grace");
  setLast("Hopper");
}); // effects reading both have run once, here