mirror of https://github.com/sveltejs/svelte
parent
7ee0ce8da7
commit
90b85d152f
@ -0,0 +1 @@
|
||||
export { get_hydratable_value as getHydratableValue } from '../internal/client/hydratable.js';
|
||||
@ -0,0 +1,60 @@
|
||||
/** @import { Parse, Transport } from '#shared' */
|
||||
import { hydrating } from './dom/hydration';
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param {string} key
|
||||
* @param {() => T} fn
|
||||
* @param {{ transport?: Transport<T> }} [options]
|
||||
* @returns {T}
|
||||
*/
|
||||
export function hydratable(key, fn, options = {}) {
|
||||
if (!hydrating) {
|
||||
return fn();
|
||||
}
|
||||
var store = window.__svelte?.h;
|
||||
if (store === undefined) {
|
||||
throw new Error('TODO this should be impossible?');
|
||||
}
|
||||
const val = store.get(key);
|
||||
if (val === undefined) {
|
||||
throw new Error(
|
||||
`TODO Expected hydratable key "${key}" to exist during hydration, but it does not`
|
||||
);
|
||||
}
|
||||
return parse(val, options.transport?.parse);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param {string} key
|
||||
* @param {{ parse?: Parse<T> }} [options]
|
||||
* @returns {T | undefined}
|
||||
*/
|
||||
export function get_hydratable_value(key, options = {}) {
|
||||
// TODO probably can DRY this out with the above
|
||||
if (!hydrating) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
var store = window.__svelte?.h;
|
||||
if (store === undefined) {
|
||||
throw new Error('TODO this should be impossible?');
|
||||
}
|
||||
const val = store.get(key);
|
||||
if (val === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return parse(val, options.parse);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param {string} val
|
||||
* @param {Parse<T> | undefined} parse
|
||||
* @returns {T}
|
||||
*/
|
||||
function parse(val, parse) {
|
||||
return (parse ?? ((val) => new Function(`return (${val})`)()))(val);
|
||||
}
|
||||
@ -1 +1,2 @@
|
||||
export { render } from '../internal/server/index.js';
|
||||
export { set_hydratable_value as setHydratableValue } from '../internal/server/hydratable.js';
|
||||
|
||||
Loading…
Reference in new issue