<script lang="ts">
import { createVirtualizer, createWindowVirtualizer } from './createVirtualizer.svelte.js';
interface Props {
type: 'element' | 'window';
options: any;
onInstanceChange: (inst: any) => void;
}
let { type, options, onInstanceChange } = $props();
// Seeded from the prop rather than assigned by an effect alone: the
// virtualizer's own $effect reads the options on its first run, which is
// before any effect this component registers. Starting undefined, that
// first run had nothing to read.
let currentOptions = $state<any>(options);
// And it keeps following the prop — a parent that swaps the options after
// mount is the whole point of the cache tests.
$effect(() => {
currentOptions = options;
});
// Created at init, the way the one real caller creates it. Built lazily
// inside a $derived instead, its internal $effect registers DURING the flush
// rather than before it — so the first read of `.instance` lands while the
// instance does not yet exist, and the harness reports undefined for a
// virtualizer that is about to be perfectly fine. `type` is fixed for the
// life of a mount, so there is nothing to derive.
const virtualizer =
type === 'window'
? createWindowVirtualizer(() => currentOptions)
: createVirtualizer(() => currentOptions);
$effect(() => {
onInstanceChange(virtualizer.instance);
});
export function getVirtualizer() {
return virtualizer;
}
export function setOptions(opts: any) {
currentOptions = opts;
}
</script>