@jay-js/system
Version:
A powerful and flexible TypeScript library for UI, state management, lazy loading, routing and managing draggable elements in modern web applications.
32 lines • 1.59 kB
JavaScript
import { moduleCache } from "./configuration.js";
import { loadFromCache, loadModule } from "./module-loader.js";
/**
* Creates a lazily loaded module that will be dynamically imported when needed.
*
* @param {TLazyModule} lazy - Configuration object for the lazy module
* @param {string} [lazy.module] - Name of the module to import. If not provided, a unique default name will be generated
* @param {() => Promise<any>} lazy.import - Function that returns a promise resolving to the module
* @param {Record<string, any>} [lazy.params] - Props to pass to the module when instantiated
* @param {boolean} [lazy.collect] - Whether the module can be garbage collected
* @param {HTMLElement|DocumentFragment} [lazy.loader] - Custom loader element to show while loading
* @param {HTMLElement} [loader] - Optional loader element to show while module is loading
* @returns {HTMLElement} Element that will be replaced with the loaded module
* @throws {Error} When module configuration is undefined
*/
export function Lazy(lazy, loader) {
if (!lazy) {
throw new Error("Module is undefined");
}
if (!lazy.module) {
const randomUUID = crypto.randomUUID();
const moduleId = `default_${randomUUID}`;
lazy = Object.assign(Object.assign({}, lazy), { module: moduleId });
}
if (lazy.module && moduleCache.has(lazy.module)) {
return loadFromCache(lazy);
}
const moduleSection = loader || document.createElement("jayjs-lazy-slot");
loadModule(lazy, moduleSection);
return moduleSection;
}
//# sourceMappingURL=lazy.js.map