@@ -31,6 +31,29 @@ import {
31
31
import { set } from './sources.js' ;
32
32
import { remove } from '../dom/reconciler.js' ;
33
33
34
+ /**
35
+ * @param {import('#client').Effect | null } effect
36
+ * @param {'$effect' | '$effect.pre' | '$inspect' } rune
37
+ * @returns {asserts effect }
38
+ */
39
+ export function validate_effect ( effect , rune ) {
40
+ if ( effect === null ) {
41
+ throw new Error (
42
+ 'ERR_SVELTE_ORPHAN_EFFECT' +
43
+ ( DEV
44
+ ? `: ${ rune } can only be used inside an effect (e.g. during component initialisation)`
45
+ : '' )
46
+ ) ;
47
+ }
48
+
49
+ if ( is_destroying_effect ) {
50
+ throw new Error (
51
+ 'ERR_SVELTE_EFFECT_IN_TEARDOWN' +
52
+ ( DEV ? `: ${ rune } cannot be used inside an effect cleanup function.` : '' )
53
+ ) ;
54
+ }
55
+ }
56
+
34
57
/**
35
58
* @param {import("#client").Effect } effect
36
59
* @param {import("#client").Reaction } parent_effect
@@ -105,18 +128,7 @@ export function effect_active() {
105
128
* @param {() => void | (() => void) } fn
106
129
*/
107
130
export function user_effect ( fn ) {
108
- if ( current_effect === null ) {
109
- throw new Error (
110
- 'ERR_SVELTE_ORPHAN_EFFECT' +
111
- ( DEV ? ': The Svelte $effect rune can only be used during component initialisation.' : '' )
112
- ) ;
113
- }
114
- if ( is_destroying_effect ) {
115
- throw new Error (
116
- 'ERR_SVELTE_EFFECT_IN_TEARDOWN' +
117
- ( DEV ? ': The Svelte $effect rune can not be used in the teardown phase of an effect.' : '' )
118
- ) ;
119
- }
131
+ validate_effect ( current_effect , '$effect' ) ;
120
132
121
133
// Non-nested `$effect(...)` in a component should be deferred
122
134
// until the component is mounted
@@ -140,23 +152,7 @@ export function user_effect(fn) {
140
152
* @returns {import('#client').Effect }
141
153
*/
142
154
export function user_pre_effect ( fn ) {
143
- if ( current_effect === null ) {
144
- throw new Error (
145
- 'ERR_SVELTE_ORPHAN_EFFECT' +
146
- ( DEV
147
- ? ': The Svelte $effect.pre rune can only be used during component initialisation.'
148
- : '' )
149
- ) ;
150
- }
151
- if ( is_destroying_effect ) {
152
- throw new Error (
153
- 'ERR_SVELTE_EFFECT_IN_TEARDOWN' +
154
- ( DEV
155
- ? ': The Svelte $effect.pre rune can not be used in the teardown phase of an effect.'
156
- : '' )
157
- ) ;
158
- }
159
-
155
+ validate_effect ( current_effect , '$effect.pre' ) ;
160
156
return render_effect ( fn ) ;
161
157
}
162
158
0 commit comments