pull/17124/head
Elliott Johnson 3 weeks ago
parent 5adb3f1df6
commit 8179c6a643

@ -1 +1,4 @@
export { get_hydratable_value as getHydratableValue } from '../internal/client/hydratable.js';
export {
get_hydratable_value as getHydratableValue,
has_hydratable_value as hasHydratableValue
} from '../internal/client/hydratable.js';

@ -49,6 +49,21 @@ export function get_hydratable_value(key, options = {}) {
return parse(val, options.parse);
}
/**
* @param {string} key
* @returns {boolean}
*/
export function has_hydratable_value(key) {
if (!hydrating) {
return false;
}
var store = window.__svelte?.h;
if (store === undefined) {
throw new Error('TODO this should be impossible?');
}
return store.has(key);
}
/**
* @template T
* @param {string} val

@ -665,7 +665,7 @@ export class SSRState {
}
}
class MemoizedUneval {
export class MemoizedUneval {
/** @type {Map<unknown, { value?: string }>} */
#cache = new Map();

@ -1,7 +1,8 @@
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
import { Renderer, SSRState } from './renderer.js';
import { MemoizedUneval, Renderer, SSRState } from './renderer.js';
import type { Component } from 'svelte';
import { disable_async_mode_flag, enable_async_mode_flag } from '../flags/index.js';
import { uneval } from 'devalue';
test('collects synchronous body content by default', () => {
const component = (renderer: Renderer) => {
@ -355,3 +356,39 @@ describe('async', () => {
expect(destroyed).toEqual(['c', 'e', 'a', 'b', 'b*', 'd']);
});
});
describe('MemoizedDevalue', () => {
test.each([
1,
'general kenobi',
{ foo: 'bar' },
[1, 2],
null,
undefined,
new Map([[1, '2']])
] as const)('has same behavior as unmemoized devalue for %s', (input) => {
expect(new MemoizedUneval().uneval(input)).toBe(uneval(input));
});
test('caches results', () => {
const memoized = new MemoizedUneval();
let calls = 0;
const input = {
get only_once() {
calls++;
return 42;
}
};
const first = memoized.uneval(input);
const max_calls = calls;
const second = memoized.uneval(input);
memoized.uneval(input);
expect(first).toBe(second);
// for reasons I don't quite comprehend, it does get called twice, but both calls happen in the first
// serialization, and don't increase afterwards
expect(calls).toBe(max_calls);
});
});

Loading…
Cancel
Save