MCPcopy
hub / github.com/sveltejs/svelte / user_effect

Function user_effect

packages/svelte/src/internal/client/reactivity/effects.js:203–229  ·  view source on GitHub ↗
(fn)

Source from the content-addressed store, hash-verified

201 * @param {() => void | (() => void)} fn
202 */
203export function user_effect(fn) {
204 validate_effect('$effect');
205
206 if (DEV) {
207 define_property(fn, 'name', {
208 value: '$effect'
209 });
210 }
211
212 // Non-nested `$effect(...)` in a component should be deferred
213 // until the component is mounted
214 var flags = /** @type {Effect} */ (active_effect).f;
215 var defer =
216 !active_reaction &&
217 (flags & BRANCH_EFFECT) !== 0 &&
218 component_context !== null &&
219 !component_context.i;
220
221 if (defer) {
222 // Top-level `$effect(...)` in an unmounted component — defer until mount
223 var context = /** @type {ComponentContext} */ (component_context);
224 (context.e ??= []).push(fn);
225 } else {
226 // Everything else — create immediately
227 return create_user_effect(fn);
228 }
229}
230
231/**
232 * @param {() => void | (() => void)} fn

Callers 3

onMountFunction · 0.90
initFunction · 0.90
test.tsFile · 0.90

Calls 3

validate_effectFunction · 0.85
create_user_effectFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected