@@ -31,6 +31,29 @@ import {
3131import { set } from './sources.js' ;
3232import { remove } from '../dom/reconciler.js' ;
3333
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+
3457/**
3558 * @param {import("#client").Effect } effect
3659 * @param {import("#client").Reaction } parent_effect
@@ -105,18 +128,7 @@ export function effect_active() {
105128 * @param {() => void | (() => void) } fn
106129 */
107130export 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' ) ;
120132
121133 // Non-nested `$effect(...)` in a component should be deferred
122134 // until the component is mounted
@@ -140,23 +152,7 @@ export function user_effect(fn) {
140152 * @returns {import('#client').Effect }
141153 */
142154export 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' ) ;
160156 return render_effect ( fn ) ;
161157}
162158
0 commit comments