All files / src/legacy legacy-client.js

90.41% Statements 132/146
100% Branches 20/20
84.61% Functions 11/13
90.14% Lines 128/142

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 1432x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2315x 2315x 2315x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                       2x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 3125x 3015x 3015x 955x 3015x 3015x 3015x 2310x 3015x 3015x 3015x 3015x 2301x 2301x 16x 2301x 2301x 2301x 2301x 2301x 2315x 2315x 2315x 16x 16x 2315x 2315x 2315x 2315x 2315x 2315x 2315x 18x 18x 18x 18x 18x 18x   18x 18x 2315x 2315x 2301x 2301x 2315x 2x 2x 2x 2x 2x 2x 2x 2x 2x      
import { proxy } from '../internal/client/proxy.js';
import { user_pre_effect } from '../internal/client/reactivity/effects.js';
import { hydrate, mount, unmount } from '../internal/client/render.js';
import { define_property } from '../internal/client/utils.js';
 
/**
 * Takes the same options as a Svelte 4 component and the component function and returns a Svelte 4 compatible component.
 *
 * @deprecated Use this only as a temporary solution to migrate your imperative component code to Svelte 5.
 *
 * @template {Record<string, any>} Props
 * @template {Record<string, any>} Exports
 * @template {Record<string, any>} Events
 * @template {Record<string, any>} Slots
 *
 * @param {import('svelte').ComponentConstructorOptions<Props> & {
 * 	component: import('svelte').ComponentType<import('svelte').SvelteComponent<Props, Events, Slots>>;
 * 	immutable?: boolean;
 * 	hydrate?: boolean;
 * 	recover?: boolean;
 * }} options
 * @returns {import('svelte').SvelteComponent<Props, Events, Slots> & Exports}
 */
export function createClassComponent(options) {
	// @ts-expect-error $$prop_def etc are not actually defined
	return new Svelte4Component(options);
}
 
/**
 * Takes the component function and returns a Svelte 4 compatible component constructor.
 *
 * @deprecated Use this only as a temporary solution to migrate your imperative component code to Svelte 5.
 *
 * @template {Record<string, any>} Props
 * @template {Record<string, any>} Exports
 * @template {Record<string, any>} Events
 * @template {Record<string, any>} Slots
 *
 * @param {import('svelte').SvelteComponent<Props, Events, Slots>} component
 * @returns {import('svelte').ComponentType<import('svelte').SvelteComponent<Props, Events, Slots> & Exports>}
 */
export function asClassComponent(component) {
	// @ts-expect-error $$prop_def etc are not actually defined
	return class extends Svelte4Component {
		/** @param {any} options */
		constructor(options) {
			super({
				component,
				...options
			});
		}
	};
}
 
class Svelte4Component {
	/** @type {any} */
	#events;
 
	/** @type {Record<string, any>} */
	#instance;
 
	/**
	 * @param {import('svelte').ComponentConstructorOptions & {
	 *  component: any;
	 * 	immutable?: boolean;
	 * 	hydrate?: boolean;
	 * 	recover?: false;
	 * }} options
	 */
	constructor(options) {
		// Using proxy state here isn't completely mirroring the Svelte 4 behavior, because mutations to a property
		// cause fine-grained updates to only the places where that property is used, and not the entire property.
		// Reactive statements and actions (the things where this matters) are handling this properly regardless, so it should be fine in practise.
		const props = proxy({ ...(options.props || {}), $$events: {} }, false);
		this.#instance = (options.hydrate ? hydrate : mount)(options.component, {
			target: options.target,
			props,
			context: options.context,
			intro: options.intro,
			recover: options.recover
		});
 
		this.#events = props.$$events;
 
		for (const key of Object.keys(this.#instance)) {
			if (key === '$set' || key === '$destroy' || key === '$on') continue;
			define_property(this, key, {
				get() {
					return this.#instance[key];
				},
				/** @param {any} value */
				set(value) {
					this.#instance[key] = value;
				},
				enumerable: true
			});
		}
 
		this.#instance.$set = /** @param {Record<string, any>} next */ (next) => {
			Object.assign(props, next);
		};
		this.#instance.$destroy = () => {
			unmount(this.#instance);
		};
	}
 
	/** @param {Record<string, any>} props */
	$set(props) {
		this.#instance.$set(props);
	}
 
	/**
	 * @param {string} event
	 * @param {(...args: any[]) => any} callback
	 * @returns {any}
	 */
	$on(event, callback) {
		this.#events[event] = this.#events[event] || [];
 
		/** @param {any[]} args */
		const cb = (...args) => callback.call(this, ...args);
		this.#events[event].push(cb);
		return () => {
			this.#events[event] = this.#events[event].filter(/** @param {any} fn */ (fn) => fn !== cb);
		};
	}
 
	$destroy() {
		this.#instance.$destroy();
	}
}
 
/**
 * Runs the given function once immediately on the server, and works like `$effect.pre` on the client.
 *
 * @deprecated Use this only as a temporary solution to migrate your component code to Svelte 5.
 * @param {() => void | (() => void)} fn
 * @returns {void}
 */
export function run(fn) {
	user_pre_effect(fn);
}