nexora
Version:
A lightweight, production-ready JavaScript library for building user interfaces, supporting JSX.
68 lines (67 loc) • 2.68 kB
JavaScript
import { reactive } from '../state';
const _componentInitCallbacks = new Map();
const _componentInitResults = new WeakMap();
const _componentInitPromises = new WeakMap();
/**
* ## onInit Lifecycle ##
* @description - This function is used to register an init callback for a component.
* @param initFn - The function to be executed when the component is initialized.
* @returns A tuple containing the result of the init function and a function to set the result.
*/
export function onInit(initFn) {
const currentComponent = reactive.currentComponentFn;
if (!currentComponent) {
throw new Error('onInit must be called within a component');
}
if (!_componentInitCallbacks.has(currentComponent)) {
_componentInitCallbacks.set(currentComponent, []);
_componentInitResults.set(currentComponent, new Map());
}
_componentInitCallbacks.get(currentComponent)?.push(initFn);
const resultsMap = _componentInitResults.get(currentComponent);
if (!resultsMap.has(initFn)) {
const [getValue, setValue] = reactive.createState(null);
resultsMap.set(initFn, [getValue, setValue]);
initFn().then((result) => {
setValue(result);
reactive.triggerUpdate(currentComponent);
});
}
const [getValue, setValue] = resultsMap.get(initFn);
return [getValue(), setValue];
}
/**
* ## Execute Init Callbacks ##
* @description Execute the init callbacks for a component
* @param componentFn - The component function to execute the callbacks for.
*/
export function executeInitCallbacks(componentFn) {
const callbacks = _componentInitCallbacks.get(componentFn);
if (callbacks) {
reactive.currentComponentFn = componentFn;
callbacks.forEach((callback) => {
const promise = Promise.resolve(callback()).then((result) => {
if (result !== undefined) {
_componentInitResults.set(componentFn, result);
}
return result;
});
_componentInitPromises.set(componentFn, promise);
});
}
return _componentInitResults.get(componentFn);
}
/**
* ## Get Init Result ##
* @description - Get the result of the init function for a component
* @param component - The component to get the result for.
* @returns - The result of the init function for the component.
*/
function getInitResult(component, initFn) {
const resultsMap = _componentInitResults.get(component);
if (!resultsMap || !resultsMap.has(initFn)) {
throw new Error('Init result not found for component');
}
const [value] = resultsMap.get(initFn);
return value();
}