Skip to content

Changing batch to use a single flag instead of a counter #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-moose-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preact/signals-core": patch
---

Simplify `batch()` to use a single flag instead of a counter
69 changes: 37 additions & 32 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
let currentSignal: Signal | undefined;
let commitError: Error | null = null;

const pending = new Set<Signal>();
/** Batch calls can be nested. 0 means that there is no batching */
let batchPending = 0;
let batchPending: Set<Signal> | null = null;

let oldDeps = new Set<Signal>();

Expand Down Expand Up @@ -71,25 +69,15 @@ export class Signal<T = any> {

if (this._value !== value) {
this._value = value;
let isFirst = pending.size === 0;
pending.add(this);
// in batch mode this signal may be marked already
if (this._pending === 0) {
mark(this);
}

// this is the first change, not a computed and we are not
// in batch mode:
if (isFirst && batchPending === 0) {
sweep(pending);
pending.clear();
if (commitError) {
const err = commitError;
// Clear global error flag for next commit
commitError = null;
throw err;
batch(() => {
batchPending!.add(this);

// in batch mode this signal may be marked already
if (this._pending === 0) {
mark(this);
}
}
});
}
}

Expand Down Expand Up @@ -205,7 +193,10 @@ const tmpPending: Signal[] = [];
* we don't have to care about topological sorting.
*/
function refreshStale(signal: Signal) {
pending.delete(signal);
if (batchPending) {
batchPending.delete(signal);
}

signal._pending = 0;
signal._updater();
if (commitError) {
Expand Down Expand Up @@ -270,20 +261,34 @@ export function effect(callback: () => void) {
}

export function batch<T>(cb: () => T): T {
batchPending++;
try {
if (batchPending !== null) {
return cb();
} finally {
// Since stale signals are refreshed upwards, we need to
// add pending signals in reverse
let item: Signal | undefined;
while ((item = tmpPending.pop()) !== undefined) {
pending.add(item);
}

if (--batchPending === 0) {
} else {
const pending: Set<Signal> = new Set();

batchPending = pending;

try {
return cb();

} finally {
// Since stale signals are refreshed upwards, we need to
// add pending signals in reverse
let item: Signal | undefined;
while ((item = tmpPending.pop()) !== undefined) {
pending.add(item);
}

batchPending = null;

sweep(pending);
pending.clear();
if (commitError) {
const err = commitError;
// Clear global error flag for next commit
commitError = null;
throw err;
}
}
}
}