import type { Component } from 'svelte'; /** * A component that loads the first time it is asked for: every call returns * the same promise, so an `{#await}` on it never starts again. A failed load * (a chunk the server no longer has) is forgotten, so the next ask retries. */ export function lazy>( load: () => Promise<{ default: T }>, ): () => Promise { let loaded: Promise | undefined; return () => (loaded ??= load().then( (module) => module.default, (failure) => { loaded = undefined; throw failure; }, )); }