Skip to content

Commit 7adc14e

Browse files
authored
chore: unify pre effects (#10933)
* breaking: always run pre effects synchronously * unify pre effects
1 parent f2cca53 commit 7adc14e

File tree

5 files changed

+35
-42
lines changed

5 files changed

+35
-42
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/javascript-runes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export const javascript_visitors_runes = {
371371
const func = context.visit(node.expression.arguments[0]);
372372
return {
373373
...node,
374-
expression: b.call('$.pre_effect', /** @type {import('estree').Expression} */ (func))
374+
expression: b.call('$.user_pre_effect', /** @type {import('estree').Expression} */ (func))
375375
};
376376
}
377377
}

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function setup_select_synchronization(value_binding, context) {
231231
context.state.init.push(
232232
b.stmt(
233233
b.call(
234-
'$.invalidate_effect',
234+
'$.pre_effect',
235235
b.thunk(
236236
b.block([
237237
b.stmt(
@@ -747,7 +747,7 @@ function serialize_inline_component(node, component_name, context) {
747747
binding_initializers.push(
748748
b.stmt(
749749
b.call(
750-
b.id('$.pre_effect'),
750+
b.id('$.user_pre_effect'),
751751
b.thunk(b.call(b.id('$.add_owner'), expression, b.id(component_name)))
752752
)
753753
)

packages/svelte/src/internal/client/dom/legacy/lifecycle.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { run } from '../../../common.js';
2-
import { pre_effect, user_effect } from '../../reactivity/effects.js';
2+
import { user_pre_effect, user_effect } from '../../reactivity/effects.js';
33
import {
44
current_component_context,
55
deep_read_state,
@@ -19,7 +19,7 @@ export function init() {
1919

2020
// beforeUpdate
2121
if (callbacks.b.length) {
22-
pre_effect(() => {
22+
user_pre_effect(() => {
2323
observe_all(context);
2424
callbacks.b.forEach(run);
2525
// beforeUpdate might change state that affects rendering, ensure the render effects following from it

packages/svelte/src/internal/client/reactivity/effects.js

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ export function user_effect(fn) {
108108
return effect;
109109
}
110110

111+
/**
112+
* Internal representation of `$effect.pre(...)`
113+
* @param {() => void | (() => void)} fn
114+
* @returns {import('#client').Effect}
115+
*/
116+
export function user_pre_effect(fn) {
117+
if (current_effect === null) {
118+
throw new Error(
119+
'ERR_SVELTE_ORPHAN_EFFECT' +
120+
(DEV
121+
? ': The Svelte $effect.pre rune can only be used during component initialisation.'
122+
: '')
123+
);
124+
}
125+
126+
return pre_effect(fn);
127+
}
128+
111129
/**
112130
* Internal representation of `$effect.root(...)`
113131
* @param {() => void | (() => void)} fn
@@ -128,24 +146,6 @@ export function effect(fn) {
128146
return create_effect(EFFECT, fn, false);
129147
}
130148

131-
/**
132-
* Internal representation of `$effect.pre(...)`
133-
* @param {() => void | (() => void)} fn
134-
* @returns {import('#client').Effect}
135-
*/
136-
export function pre_effect(fn) {
137-
if (current_effect === null) {
138-
throw new Error(
139-
'ERR_SVELTE_ORPHAN_EFFECT' +
140-
(DEV
141-
? ': The Svelte $effect.pre rune can only be used during component initialisation.'
142-
: '')
143-
);
144-
}
145-
146-
return create_effect(PRE_EFFECT, fn, true);
147-
}
148-
149149
/**
150150
* Internal representation of `$: ..`
151151
* @param {() => any} deps
@@ -157,19 +157,15 @@ export function legacy_pre_effect(deps, fn) {
157157
current_component_context
158158
);
159159
const token = {};
160-
return create_effect(
161-
PRE_EFFECT,
162-
() => {
163-
deps();
164-
if (component_context.l1.includes(token)) {
165-
return;
166-
}
167-
component_context.l1.push(token);
168-
set(component_context.l2, true);
169-
return untrack(fn);
170-
},
171-
true
172-
);
160+
return pre_effect(() => {
161+
deps();
162+
if (component_context.l1.includes(token)) {
163+
return;
164+
}
165+
component_context.l1.push(token);
166+
set(component_context.l2, true);
167+
return untrack(fn);
168+
});
173169
}
174170

175171
export function legacy_pre_effect_reset() {
@@ -186,13 +182,10 @@ export function legacy_pre_effect_reset() {
186182
}
187183

188184
/**
189-
* This effect is used to ensure binding are kept in sync. We use a pre effect to ensure we run before the
190-
* bindings which are in later effects. However, we don't use a pre_effect directly as we don't want to flush anything.
191-
*
192185
* @param {() => void | (() => void)} fn
193186
* @returns {import('#client').Effect}
194187
*/
195-
export function invalidate_effect(fn) {
188+
export function pre_effect(fn) {
196189
return create_effect(PRE_EFFECT, fn, true);
197190
}
198191

packages/svelte/src/internal/client/runtime.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
object_prototype
99
} from './utils.js';
1010
import { unstate } from './proxy.js';
11-
import { destroy_effect, pre_effect } from './reactivity/effects.js';
11+
import { destroy_effect, user_pre_effect } from './reactivity/effects.js';
1212
import {
1313
EFFECT,
1414
PRE_EFFECT,
@@ -1201,7 +1201,7 @@ let warned_inspect_changed = false;
12011201
export function inspect(get_value, inspect = console.log) {
12021202
let initial = true;
12031203

1204-
pre_effect(() => {
1204+
user_pre_effect(() => {
12051205
const fn = () => {
12061206
const value = untrack(() => get_value().map((v) => deep_unstate(v)));
12071207
if (value.length === 2 && typeof value[1] === 'function' && !warned_inspect_changed) {

0 commit comments

Comments
 (0)