run-batch-until-complete
Rich Harris 5 days ago
parent ae0038c953
commit 18ecf01382

@ -203,8 +203,10 @@ export async function async_body(fn) {
var boundary = get_boundary();
var batch = /** @type {Batch} */ (current_batch);
var blocking = !boundary.is_pending();
boundary.update_pending_count(1);
batch.increment();
batch.increment(blocking);
var active = /** @type {Effect} */ (active_effect);
@ -237,7 +239,7 @@ export async function async_body(fn) {
}
boundary.update_pending_count(-1);
batch.decrement();
batch.decrement(blocking);
unset_context();
}

@ -101,6 +101,11 @@ export class Batch {
*/
#pending = 0;
/**
* The number of async effects that are currently in flight, _not_ inside a pending boundary
*/
#blocking_pending = 0;
/**
* A deferred that resolves when the batch is committed, used with `settled()`
* TODO replace with Promise.withResolvers once supported widely enough
@ -257,7 +262,7 @@ export class Batch {
}
}
if (should_defer) {
if (this.#blocking_pending > 0) {
this.#defer_effects(target.effects);
this.#defer_effects(target.render_effects);
this.#defer_effects(target.block_effects);
@ -407,12 +412,22 @@ export class Batch {
this.#deferred?.resolve();
}
increment() {
/**
*
* @param {boolean} blocking
*/
increment(blocking) {
this.#pending += 1;
if (blocking) this.#blocking_pending += 1;
}
decrement() {
/**
*
* @param {boolean} blocking
*/
decrement(blocking) {
this.#pending -= 1;
if (blocking) this.#blocking_pending -= 1;
for (const e of this.#dirty_effects) {
set_signal_status(e, DIRTY);

@ -138,8 +138,10 @@ export function async_derived(fn, location) {
var batch = /** @type {Batch} */ (current_batch);
if (should_suspend) {
var blocking = !boundary.is_pending();
boundary.update_pending_count(1);
batch.increment();
batch.increment(blocking);
deferreds.get(batch)?.reject(STALE_REACTION);
deferreds.delete(batch); // delete to ensure correct order in Map iteration below
@ -190,7 +192,7 @@ export function async_derived(fn, location) {
if (should_suspend) {
boundary.update_pending_count(-1);
batch.decrement();
batch.decrement(blocking);
}
};

Loading…
Cancel
Save