UNPKG

@dvcol/svelte-utils

Version:

Svelte library for common utility functions and constants

39 lines (38 loc) 1.4 kB
export const isLazyComponent = (component) => !!(component && typeof component === 'function' && // Wrapped with toLazyComponent (component._isLazyComponent || // Wrapped in async function component.constructor.name === 'AsyncFunction' || // Arrow function named as component component.name === 'component')); export const isSnippet = (componentOrSnippet) => componentOrSnippet?.length === 1; export const toLazyComponent = (fn) => { const component = fn; component._isLazyComponent = true; return component; }; export function isSyncComponentOrSnippet(component) { return isSnippet(component) || !isLazyComponent(component); } export async function resolveComponent(component, { onStart, onLoading, onLoaded, onError, } = {}) { if (!component || isSyncComponentOrSnippet(component)) { await onStart?.(); await onLoaded?.(component); return component; } return resolveAsyncComponent(component, { onStart, onLoading, onLoaded, onError }); } export async function resolveAsyncComponent(component, { onStart, onLoading, onLoaded, onError, } = {}) { await onStart?.(); onLoading?.(); try { const awaited = await component(); await onLoaded?.(awaited.default); return awaited.default; } catch (error) { await onError?.(error); return undefined; } }