UNPKG

@cloakui/styles

Version:

TailwindCSS presets for styling components in any JS framework.

25 lines (24 loc) 1.27 kB
/** * lazyFactory creates a lazy-initialized factory function. It ensures that the * factory function is only created once and then caches it for future invocations. * This is particularly useful for factory functions that rely on some user-config to * be set before it is intiliazed; often that user-config is specified in _app.tsx, but * that will run after module resolution, meaning your factory function would initialize * before the user-config is set, resulting in incorrect behavior. * * @param {Function} factoryFn - A function that returns a factory function when called. * @returns {Function} A new factory function that lazily initializes and caches the original factory function. */ export function lazyFactory(factoryFn) { let factoryInstance = null; let initialized = false; // Return a function with the same signature as the factory function return function (...args) { if (!initialized) { factoryInstance = factoryFn(); initialized = true; } // Since factoryInstance is guaranteed to be initialized here, we can use the non-null assertion return factoryInstance(...args); }; // Cast the function to T to ensure the returned type matches the factory function's type }