You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/packages/svelte/src/internal/client/hydratable.js

39 lines
791 B

import { async_mode_flag } from '../flags/index.js';
import { hydrating } from './dom/hydration.js';
import * as w from './warnings.js';
import * as e from './errors.js';
import { DEV } from 'esm-env';
/**
* @template T
* @param {string} key
* @param {() => T} fn
* @returns {T}
*/
export function hydratable(key, fn) {
if (!async_mode_flag) {
e.experimental_async_required('hydratable');
}
if (!hydrating) {
return fn();
}
const store = window.__svelte?.h;
if (!store?.has(key)) {
hydratable_missing_but_expected(key);
return fn();
}
return /** @type {T} */ (store.get(key));
}
/** @param {string} key */
function hydratable_missing_but_expected(key) {
if (DEV) {
e.hydratable_missing_but_required(key);
} else {
w.hydratable_missing_but_expected(key);
}
}