Skip to content

Commit 6b1ac77

Browse files
committed
only update $effect.pending() if someone is listening, since it causes a double flush and makes debugging harder
1 parent cf2bdd1 commit 6b1ac77

File tree

1 file changed

+28
-10
lines changed
  • packages/svelte/src/internal/client/dom/blocks

1 file changed

+28
-10
lines changed

packages/svelte/src/internal/client/dom/blocks/boundary.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
/** @import { Effect, TemplateNode, } from '#client' */
2-
1+
/** @import { Effect, Source, TemplateNode, } from '#client' */
32
import { BOUNDARY_EFFECT, EFFECT_PRESERVED, EFFECT_TRANSPARENT } from '#client/constants';
43
import { component_context, set_component_context } from '../../context.js';
54
import { invoke_error_boundary } from '../../error-handling.js';
@@ -25,8 +24,9 @@ import * as e from '../../../shared/errors.js';
2524
import { DEV } from 'esm-env';
2625
import { from_async_derived, set_from_async_derived } from '../../reactivity/deriveds.js';
2726
import { Batch } from '../../reactivity/batch.js';
28-
import { source, update } from '../../reactivity/sources.js';
27+
import { internal_set, source } from '../../reactivity/sources.js';
2928
import { tag } from '../../dev/tracing.js';
29+
import { createSubscriber } from '../../../../reactivity/create-subscriber.js';
3030

3131
/**
3232
* @typedef {{
@@ -85,7 +85,22 @@ export class Boundary {
8585
#pending_count = 0;
8686
#is_creating_fallback = false;
8787

88-
effect_pending = source(0);
88+
/**
89+
* @type {Source<number> | null}
90+
*/
91+
#effect_pending = null;
92+
93+
#effect_pending_subscriber = createSubscriber(() => {
94+
this.#effect_pending = source(this.#pending_count);
95+
96+
if (DEV) {
97+
tag(this.#effect_pending, '$effect.pending()');
98+
}
99+
100+
return () => {
101+
this.#effect_pending = null;
102+
};
103+
});
89104

90105
/**
91106
* @param {TemplateNode} node
@@ -103,10 +118,6 @@ export class Boundary {
103118

104119
this.pending = !!this.#props.pending;
105120

106-
if (DEV) {
107-
tag(this.effect_pending, '$effect.pending()');
108-
}
109-
110121
this.#effect = block(() => {
111122
/** @type {Effect} */ (active_effect).b = this;
112123

@@ -237,10 +248,17 @@ export class Boundary {
237248
}
238249

239250
queueMicrotask(() => {
240-
update(this.effect_pending, d);
251+
if (this.#effect_pending) {
252+
internal_set(this.#effect_pending, this.#pending_count);
253+
}
241254
});
242255
}
243256

257+
get_effect_pending() {
258+
this.#effect_pending_subscriber();
259+
return get(/** @type {Source<number>} */ (this.#effect_pending));
260+
}
261+
244262
/** @param {unknown} error */
245263
error(error) {
246264
var onerror = this.#props.onerror;
@@ -429,5 +447,5 @@ export function pending() {
429447
return 0; // TODO eventually we will need this to be global
430448
}
431449

432-
return get(boundary.effect_pending);
450+
return boundary.get_effect_pending();
433451
}

0 commit comments

Comments
 (0)