chore: remove signal field from effects (#10984)

pull/10987/head
Dominic Gannaway 10 months ago committed by GitHub
parent 696b6922e8
commit a1d74bd56d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -3,7 +3,6 @@ import { CLEAN, DERIVED, DESTROYED, DIRTY, MAYBE_DIRTY, UNOWNED } from '../const
import { import {
current_reaction, current_reaction,
current_effect, current_effect,
destroy_children,
remove_reactions, remove_reactions,
set_signal_status, set_signal_status,
mark_reactions, mark_reactions,
@ -11,6 +10,7 @@ import {
execute_reaction_fn execute_reaction_fn
} from '../runtime.js'; } from '../runtime.js';
import { equals, safe_equals } from './equality.js'; import { equals, safe_equals } from './equality.js';
import { destroy_effect } from './effects.js';
export let updating_derived = false; export let updating_derived = false;
@ -42,10 +42,11 @@ export function derived(fn) {
} }
if (current_reaction !== null && (current_reaction.f & DERIVED) !== 0) { if (current_reaction !== null && (current_reaction.f & DERIVED) !== 0) {
if (current_reaction.deriveds === null) { var current_derived = /** @type {import('#client').Derived<V>} */ (current_reaction);
current_reaction.deriveds = [signal]; if (current_derived.deriveds === null) {
current_derived.deriveds = [signal];
} else { } else {
current_reaction.deriveds.push(signal); current_derived.deriveds.push(signal);
} }
} }
@ -64,6 +65,27 @@ export function derived_safe_equal(fn) {
return signal; 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 {import('#client').Derived} derived
* @param {boolean} force_schedule * @param {boolean} force_schedule
@ -72,7 +94,7 @@ export function derived_safe_equal(fn) {
export function update_derived(derived, force_schedule) { export function update_derived(derived, force_schedule) {
var previous_updating_derived = updating_derived; var previous_updating_derived = updating_derived;
updating_derived = true; updating_derived = true;
destroy_children(derived); destroy_derived_children(derived);
var value = execute_reaction_fn(derived); var value = execute_reaction_fn(derived);
updating_derived = previous_updating_derived; updating_derived = previous_updating_derived;
@ -98,7 +120,7 @@ export function update_derived(derived, force_schedule) {
* @returns {void} * @returns {void}
*/ */
export function destroy_derived(signal) { export function destroy_derived(signal) {
destroy_children(signal); destroy_derived_children(signal);
remove_reactions(signal, 0); remove_reactions(signal, 0);
set_signal_status(signal, DESTROYED); set_signal_status(signal, DESTROYED);

@ -46,7 +46,6 @@ function create_effect(type, fn, sync) {
f: type | DIRTY, f: type | DIRTY,
fn, fn,
effects: null, effects: null,
deriveds: null,
teardown: null, teardown: null,
ctx: current_component_context, ctx: current_component_context,
transitions: null transitions: null

@ -23,13 +23,13 @@ export interface Reaction extends Signal {
deps: null | Value[]; deps: null | Value[];
/** Effects created inside this signal */ /** Effects created inside this signal */
effects: null | Effect[]; effects: null | Effect[];
/** Deriveds created inside this signal */
deriveds: null | Derived[];
} }
export interface Derived<V = unknown> extends Value<V>, Reaction { export interface Derived<V = unknown> extends Value<V>, Reaction {
/** The derived function */ /** The derived function */
fn: () => V; fn: () => V;
/** Deriveds created inside this signal */
deriveds: null | Derived[];
} }
export interface Effect extends Reaction { export interface Effect extends Reaction {

@ -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} * @returns {void}
*/ */
export function destroy_children(signal) { export function destroy_children(signal) {
@ -359,13 +359,6 @@ export function destroy_children(signal) {
} }
signal.effects = null; signal.effects = null;
} }
if (signal.deriveds) {
for (i = 0; i < signal.deriveds.length; i += 1) {
destroy_derived(signal.deriveds[i]);
}
signal.deriveds = null;
}
} }
/** /**

Loading…
Cancel
Save