Skip to content

chore: remove signal field from effects #10984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { CLEAN, DERIVED, DESTROYED, DIRTY, MAYBE_DIRTY, UNOWNED } from '../const
import {
current_reaction,
current_effect,
destroy_children,
remove_reactions,
set_signal_status,
mark_reactions,
current_skip_reaction,
execute_reaction_fn
} from '../runtime.js';
import { equals, safe_equals } from './equality.js';
import { destroy_effect } from './effects.js';

export let updating_derived = false;

Expand Down Expand Up @@ -42,10 +42,11 @@ export function derived(fn) {
}

if (current_reaction !== null && (current_reaction.f & DERIVED) !== 0) {
if (current_reaction.deriveds === null) {
current_reaction.deriveds = [signal];
var current_derived = /** @type {import('#client').Derived<V>} */ (current_reaction);
if (current_derived.deriveds === null) {
current_derived.deriveds = [signal];
} else {
current_reaction.deriveds.push(signal);
current_derived.deriveds.push(signal);
}
}

Expand All @@ -64,6 +65,27 @@ export function derived_safe_equal(fn) {
return signal;
}

/**
* @param {import('./types.js').Derived} signal
* @returns {void}
*/
function destroy_derived_children(signal) {
// TODO: should it be possible to create effects in deriveds given they're meant to be pure?
if (signal.effects) {
for (var i = 0; i < signal.effects.length; i += 1) {
destroy_effect(signal.effects[i]);
}
signal.effects = null;
}

if (signal.deriveds) {
for (i = 0; i < signal.deriveds.length; i += 1) {
destroy_derived(signal.deriveds[i]);
}
signal.deriveds = null;
}
}

/**
* @param {import('#client').Derived} derived
* @param {boolean} force_schedule
Expand All @@ -72,7 +94,7 @@ export function derived_safe_equal(fn) {
export function update_derived(derived, force_schedule) {
var previous_updating_derived = updating_derived;
updating_derived = true;
destroy_children(derived);
destroy_derived_children(derived);
var value = execute_reaction_fn(derived);
updating_derived = previous_updating_derived;

Expand All @@ -98,7 +120,7 @@ export function update_derived(derived, force_schedule) {
* @returns {void}
*/
export function destroy_derived(signal) {
destroy_children(signal);
destroy_derived_children(signal);
remove_reactions(signal, 0);
set_signal_status(signal, DESTROYED);

Expand Down
1 change: 0 additions & 1 deletion packages/svelte/src/internal/client/reactivity/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ function create_effect(type, fn, sync) {
f: type | DIRTY,
fn,
effects: null,
deriveds: null,
teardown: null,
ctx: current_component_context,
transitions: null
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/reactivity/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export interface Reaction extends Signal {
deps: null | Value[];
/** Effects created inside this signal */
effects: null | Effect[];
/** Deriveds created inside this signal */
deriveds: null | Derived[];
}

export interface Derived<V = unknown> extends Value<V>, Reaction {
/** The derived function */
fn: () => V;
/** Deriveds created inside this signal */
deriveds: null | Derived[];
}

export interface Effect extends Reaction {
Expand Down
9 changes: 1 addition & 8 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ export function remove_reactions(signal, start_index) {
}

/**
* @param {import('./types.js').Reaction} signal
* @param {import('./types.js').Effect} signal
* @returns {void}
*/
export function destroy_children(signal) {
Expand All @@ -359,13 +359,6 @@ export function destroy_children(signal) {
}
signal.effects = null;
}

if (signal.deriveds) {
for (i = 0; i < signal.deriveds.length; i += 1) {
destroy_derived(signal.deriveds[i]);
}
signal.deriveds = null;
}
}

/**
Expand Down