Skip to content

Commit

Permalink
feat: allow non-synchronous legacy component instantiation (#12970)
Browse files Browse the repository at this point in the history
* feat: allow non-synchronous legacy component instantiation

Add a new option to the legacy class component interface so that `flush_sync` can be omitted. Part of sveltejs/kit#12248

* lint

---------

Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
dummdidumm and Rich-Harris authored Aug 24, 2024
1 parent c5c54da commit 72b066b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-pears-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: allow non-synchronous legacy component instantiation
2 changes: 2 additions & 0 deletions packages/svelte/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export interface ComponentConstructorOptions<
context?: Map<any, any>;
hydrate?: boolean;
intro?: boolean;
recover?: boolean;
sync?: boolean;
$$inline?: boolean;
}

Expand Down
10 changes: 2 additions & 8 deletions packages/svelte/src/legacy/legacy-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import { define_property } from '../internal/shared/utils.js';
*
* @param {ComponentConstructorOptions<Props> & {
* component: ComponentType<SvelteComponent<Props, Events, Slots>> | Component<Props>;
* immutable?: boolean;
* hydrate?: boolean;
* recover?: boolean;
* }} options
* @returns {SvelteComponent<Props, Events, Slots> & Exports}
*/
Expand Down Expand Up @@ -64,9 +61,6 @@ class Svelte4Component {
/**
* @param {ComponentConstructorOptions & {
* component: any;
* immutable?: boolean;
* hydrate?: boolean;
* recover?: false;
* }} options
*/
constructor(options) {
Expand Down Expand Up @@ -110,8 +104,8 @@ class Svelte4Component {
recover: options.recover
});

// We don't flush_sync for custom element wrappers
if (!options?.props?.$$host) {
// We don't flush_sync for custom element wrappers or if the user doesn't want it
if (!options?.props?.$$host || options.sync === false) {
flush_sync();
}

Expand Down
1 change: 0 additions & 1 deletion packages/svelte/tests/runtime-legacy/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ async function run_test_variant(
component: mod.default,
props: config.props,
target,
immutable: config.immutable,
intro: config.intro,
recover: config.recover ?? false,
hydrate: variant === 'hydrate'
Expand Down
5 changes: 2 additions & 3 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ declare module 'svelte' {
context?: Map<any, any>;
hydrate?: boolean;
intro?: boolean;
recover?: boolean;
sync?: boolean;
$$inline?: boolean;
}

Expand Down Expand Up @@ -2101,9 +2103,6 @@ declare module 'svelte/legacy' {
* */
export function createClassComponent<Props extends Record<string, any>, Exports extends Record<string, any>, Events extends Record<string, any>, Slots extends Record<string, any>>(options: ComponentConstructorOptions<Props> & {
component: ComponentType<SvelteComponent<Props, Events, Slots>> | Component<Props>;
immutable?: boolean;
hydrate?: boolean;
recover?: boolean;
}): SvelteComponent<Props, Events, Slots> & Exports;
/**
* Takes the component function and returns a Svelte 4 compatible component constructor.
Expand Down

0 comments on commit 72b066b

Please sign in to comment.