UNPKG

dreamstate

Version:

Store management library based on react context and observers

43 lines (40 loc) 1.79 kB
import { DreamstateError } from '../error/DreamstateError.js'; import { ContextManager } from '../management/ContextManager.js'; import { EDreamstateErrorCode } from '../../types/error.js'; /** * Collects metadata from the prototype chain of a given context manager class. * * This function resolves issues related to class inheritance, particularly when dealing with query * and signal handlers. It traverses the prototype chain of the provided context manager class, * gathering metadata and ensuring all inherited behaviors are properly registered. * * @template T - The type of the context manager constructor. * @template D - The type of the metadata stored in the registry. * @param {T} target - The base context manager class from which metadata should be collected. * @param {WeakMap<T, D>} registry - A weak map registry that holds metadata for context manager classes. * @returns {D} The collected metadata for the given context manager. */ function collectProtoChainMetadata(target, registry) { if (target.prototype instanceof ContextManager) { var metadata = []; var current = target; while (current !== ContextManager) { metadata.push(registry.get(current)); current = Object.getPrototypeOf(current); } /** * todo: Remove duplicates from an array? * todo: If overriding signal handling methods and adding @OnSignal, it may cause issues with double call of method. */ return metadata.reduce(function (pr, it) { if (it) { return pr.concat(it); } else { return pr; } }, []); } else { throw new DreamstateError(EDreamstateErrorCode.INCORRECT_PARAMETER, "Failed to collect metadata of class that is not extending ContextManager."); } } export { collectProtoChainMetadata };