Skip to content

Commit

Permalink
fix: ensure component effects have the correct reaction owner (#13308)
Browse files Browse the repository at this point in the history
* fix: ensure component effects have the correct reaction owner

* fix: ensure component effects have the correct reaction owner

* lint
  • Loading branch information
trueadm committed Sep 18, 2024
1 parent b235161 commit c4d8d40
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/svelte/src/internal/client/reactivity/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ export function user_effect(fn) {
var context = /** @type {ComponentContext} */ (component_context);
(context.e ??= []).push({
fn,
parent: active_effect
effect: active_effect,
reaction: active_reaction
});
} else {
var signal = effect(fn);
Expand Down
5 changes: 4 additions & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,15 +1063,18 @@ export function pop(component) {
const component_effects = context_stack_item.e;
if (component_effects !== null) {
var previous_effect = active_effect;
var previous_reaction = active_reaction;
context_stack_item.e = null;
try {
for (var i = 0; i < component_effects.length; i++) {
var component_effect = component_effects[i];
set_active_effect(component_effect.parent);
set_active_effect(component_effect.effect);
set_active_reaction(component_effect.reaction);
effect(component_effect.fn);
}
} finally {
set_active_effect(previous_effect);
set_active_reaction(previous_reaction);
}
}
component_context = context_stack_item.p;
Expand Down
8 changes: 6 additions & 2 deletions packages/svelte/src/internal/client/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Store } from '#shared';
import { STATE_SYMBOL } from './constants.js';
import type { Effect, Source, Value } from './reactivity/types.js';
import type { Effect, Source, Value, Reaction } from './reactivity/types.js';

type EventCallback = (event: Event) => boolean;
export type EventCallbackMap = Record<string, EventCallback | EventCallback[]>;
Expand All @@ -15,7 +15,11 @@ export type ComponentContext = {
/** context */
c: null | Map<unknown, unknown>;
/** deferred effects */
e: null | Array<{ fn: () => void | (() => void); parent: null | Effect }>;
e: null | Array<{
fn: () => void | (() => void);
effect: null | Effect;
reaction: null | Reaction;
}>;
/** mounted */
m: boolean;
/**
Expand Down

0 comments on commit c4d8d40

Please sign in to comment.