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
fnthrows, after the flush. An error of the flush is thrown asflushthrows it (including ShadowErrorINFINITE_LOOP); whenfnthrew as well, both are thrown together as anAggregateError.
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