(fn)
| 201 | * @param {() => void | (() => void)} fn |
| 202 | */ |
| 203 | export 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 |
no test coverage detected