export function createLazyRuntimeSurface( importer: () => Promise, select: (module: TModule) => TSurface, ): () => Promise { let cached: Promise | null = null; return () => { cached ??= importer().then(select); return cached; }; } /** Cache the raw dynamically imported runtime module behind a stable loader. */ export function createLazyRuntimeModule( importer: () => Promise, ): () => Promise { return createLazyRuntimeSurface(importer, (module) => module); } /** Cache a single named runtime export without repeating a custom selector closure per caller. */ export function createLazyRuntimeNamedExport( importer: () => Promise, key: TKey, ): () => Promise { return createLazyRuntimeSurface(importer, (module) => module[key]); } export function createLazyRuntimeMethod( load: () => Promise, select: (surface: TSurface) => (...args: TArgs) => TResult, ): (...args: TArgs) => Promise> { const invoke = async (...args: TArgs): Promise> => { const method = select(await load()); return await method(...args); }; return invoke; } export function createLazyRuntimeMethodBinder(load: () => Promise) { return function ( select: (surface: TSurface) => (...args: TArgs) => TResult, ): (...args: TArgs) => Promise> { return createLazyRuntimeMethod(load, select); }; }