From 6fa3a15922b9e48e7c75cb0bcd14eaec6d123a2c Mon Sep 17 00:00:00 2001 From: Andreas Ehrencrona Date: Mon, 31 Jul 2023 08:51:11 +0200 Subject: [PATCH] Review feedback --- .../02-client-side-component-api.md | 6 ++-- .../svelte/src/runtime/internal/Component.js | 30 +++++++++---------- packages/svelte/src/runtime/internal/dev.js | 9 ++++-- .../svelte/src/runtime/internal/private.d.ts | 4 +++ .../samples/transition-js-destroy/_config.js | 2 +- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/documentation/docs/04-compiler-and-api/02-client-side-component-api.md b/documentation/docs/04-compiler-and-api/02-client-side-component-api.md index d12021227b..959a1224ef 100644 --- a/documentation/docs/04-compiler-and-api/02-client-side-component-api.md +++ b/documentation/docs/04-compiler-and-api/02-client-side-component-api.md @@ -178,19 +178,19 @@ import { SvelteComponent, ComponentConstructorOptions } from 'svelte'; declare global { class Component extends SvelteComponent {} var component: Component; - var run_outro: boolean; + var options: { runOutro: boolean }; } export {} // @filename: index.ts // ---cut--- -component.$destroy(run_outro); +component.$destroy(options); ``` Removes a component from the DOM and triggers any `onDestroy` handlers. -If `run_outro` is `true`, any outro transitions will play before the component is destroyed. +`options` may contain the property `runOutro` which indicates whether to play any outro transitions before the component is destroyed. ## Component props diff --git a/packages/svelte/src/runtime/internal/Component.js b/packages/svelte/src/runtime/internal/Component.js index 09b84ceff0..0a9a384be6 100644 --- a/packages/svelte/src/runtime/internal/Component.js +++ b/packages/svelte/src/runtime/internal/Component.js @@ -1,23 +1,23 @@ import { - add_render_callback, - flush, - flush_render_callbacks, - schedule_update, - dirty_components -} from './scheduler.js'; -import { current_component, set_current_component } from './lifecycle.js'; -import { blank_object, is_empty, is_function, run, run_all, noop } from './utils.js'; -import { + attr, children, detach, - start_hydrating, + element, end_hydrating, get_custom_elements_slots, insert, - element, - attr + start_hydrating } from './dom.js'; +import { current_component, set_current_component } from './lifecycle.js'; +import { + add_render_callback, + dirty_components, + flush, + flush_render_callbacks, + schedule_update +} from './scheduler.js'; import { check_outros, group_outros, transition_in, transition_out } from './transitions.js'; +import { blank_object, is_empty, is_function, noop, run, run_all } from './utils.js'; /** @returns {void} */ export function bind(component, name, callback) { @@ -456,11 +456,11 @@ export class SvelteComponent { $$set = undefined; /** - * @param {boolean} [run_outro] + * @param {import('./private.js').ComponentDestroyOptions} [options] * @returns {void} */ - $destroy(run_outro) { - if (run_outro && this.$$.fragment && this.$$.fragment.o) { + $destroy(options) { + if (options?.runOutro && this.$$.fragment && this.$$.fragment.o) { group_outros(); transition_out(this.$$.fragment, 0, 0, () => { destroy_component(this, 1); diff --git a/packages/svelte/src/runtime/internal/dev.js b/packages/svelte/src/runtime/internal/dev.js index b1d89add33..d06397c556 100644 --- a/packages/svelte/src/runtime/internal/dev.js +++ b/packages/svelte/src/runtime/internal/dev.js @@ -331,9 +331,12 @@ export class SvelteComponentDev extends SvelteComponent { super(); } - /** @returns {void} */ - $destroy() { - super.$destroy(); + /** + * @param {import('./private.js').ComponentDestroyOptions} [options] + * @returns {void} + */ + $destroy(options) { + super.$destroy(options); this.$destroy = () => { console.warn('Component was already destroyed'); // eslint-disable-line no-console }; diff --git a/packages/svelte/src/runtime/internal/private.d.ts b/packages/svelte/src/runtime/internal/private.d.ts index ef2245256e..64aea23de0 100644 --- a/packages/svelte/src/runtime/internal/private.d.ts +++ b/packages/svelte/src/runtime/internal/private.d.ts @@ -75,6 +75,10 @@ export interface StyleInformation { rules: Record; } +export interface ComponentDestroyOptions { + runOutro?: boolean; +} + export type TaskCallback = (now: number) => boolean | void; export type TaskEntry = { c: TaskCallback; f: () => void }; diff --git a/packages/svelte/test/runtime/samples/transition-js-destroy/_config.js b/packages/svelte/test/runtime/samples/transition-js-destroy/_config.js index c795b14e0e..ddcea983a7 100644 --- a/packages/svelte/test/runtime/samples/transition-js-destroy/_config.js +++ b/packages/svelte/test/runtime/samples/transition-js-destroy/_config.js @@ -3,7 +3,7 @@ export default { skip_if_hydrate: true, skip_if_hydrate_from_ssr: true, test({ assert, component, target, raf }) { - component.$destroy(true); + component.$destroy({ runOutro: true }); return Promise.resolve().then(() => { const div = target.querySelector('div');