Skip to content

Commit 06bf3a4

Browse files
committed
optimise
1 parent 70f0ad5 commit 06bf3a4

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

packages/svelte/src/internal/client/reactivity/effects.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import {
3030
ROOT_EFFECT,
3131
EFFECT_TRANSPARENT,
3232
DERIVED,
33-
UNOWNED
33+
UNOWNED,
34+
CLEAN
3435
} from '../constants.js';
3536
import { set } from './sources.js';
3637
import { remove } from '../dom/reconciler.js';
@@ -68,7 +69,7 @@ export function push_effect(effect, parent_effect) {
6869

6970
/**
7071
* @param {number} type
71-
* @param {(() => void | (() => void))} fn
72+
* @param {null | (() => void | (() => void))} fn
7273
* @param {boolean} sync
7374
* @returns {import('#client').Effect}
7475
*/
@@ -121,7 +122,7 @@ function create_effect(type, fn, sync) {
121122
} finally {
122123
set_is_flushing_effect(previously_flushing_effect);
123124
}
124-
} else {
125+
} else if (fn !== null) {
125126
schedule_effect(effect);
126127
}
127128

@@ -148,7 +149,10 @@ export function effect_active() {
148149
* @param {() => void} fn
149150
*/
150151
export function teardown(fn) {
151-
return render_effect(() => fn);
152+
const effect = create_effect(RENDER_EFFECT, null, false);
153+
set_signal_status(effect, CLEAN);
154+
effect.teardown = fn;
155+
return effect;
152156
}
153157

154158
/**
@@ -371,7 +375,6 @@ export function destroy_effect(effect, remove_dom = true) {
371375
effect.dom =
372376
effect.deps =
373377
effect.parent =
374-
// @ts-expect-error
375378
effect.fn =
376379
null;
377380
}

packages/svelte/src/internal/client/reactivity/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface Value<V = unknown> extends Signal {
1818

1919
export interface Reaction extends Signal {
2020
/** The reaction function */
21-
fn: Function;
21+
fn: null | Function;
2222
/** Signals that this signal reads from */
2323
deps: null | Value[];
2424
/** First child effect created inside this signal */
@@ -40,7 +40,7 @@ export interface Effect extends Reaction {
4040
/** The associated component context */
4141
ctx: null | ComponentContext;
4242
/** The effect function */
43-
fn: () => void | (() => void);
43+
fn: null | (() => void) | (() => void);
4444
/** The teardown function returned from the effect function */
4545
teardown: null | (() => void);
4646
/** Transition managers created with `$.transition` */

packages/svelte/src/internal/client/runtime.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ function handle_error(error, effect, component_context) {
290290

291291
const component_stack = [];
292292

293-
const effect_name = effect.fn.name;
293+
const effect_name = effect.fn?.name;
294294

295295
if (effect_name) {
296296
component_stack.push(effect_name);
@@ -358,7 +358,7 @@ export function execute_reaction_fn(signal) {
358358
current_untracking = false;
359359

360360
try {
361-
let res = (0, signal.fn)();
361+
let res = /** @type {Function} */ (0, signal.fn)();
362362
let dependencies = /** @type {import('#client').Value<unknown>[]} **/ (signal.deps);
363363
if (current_dependencies !== null) {
364364
let i;

0 commit comments

Comments
 (0)