UNPKG

dreamstate

Version:

Store management library based on react context and observers

39 lines (36 loc) 1.84 kB
import { DreamstateError } from '../error/DreamstateError.js'; import { QUERY_METADATA_REGISTRY } from '../internals.js'; import { ContextManager } from '../management/ContextManager.js'; import { EDreamstateErrorCode } from '../../types/error.js'; import { createMethodDecorator } from '../../utils/polyfills/createMethodDecorator.js'; import { isCorrectQueryType } from '../../utils/typechecking.js'; /** * Class method decorator factory that marks the decorated method as a handler for specified query types. * * This decorator ensures that the decorated method will be invoked when a query of the specified type(s) * is triggered within the current scope. It supports handling a single query type or an array of query types. * The supported query types include `string`, `number`, and `symbol`. * * @param {TQueryType | Array<TQueryType>} queryType - The query type or an array of query types * that the decorated method will handle. * @returns {MethodDecorator} A method decorator that attaches the query handler functionality to the method. */ function OnQuery(queryType) { if (!isCorrectQueryType(queryType)) { throw new DreamstateError(EDreamstateErrorCode.INCORRECT_QUERY_TYPE, typeof queryType); } /* * Support old and new decorators with polyfill. */ return createMethodDecorator(function (method, ManagerClass) { if (!(ManagerClass.prototype instanceof ContextManager)) { throw new DreamstateError(EDreamstateErrorCode.TARGET_CONTEXT_MANAGER_EXPECTED, "Only ContextManager extending classes methods can be decorated as query handlers."); } if (QUERY_METADATA_REGISTRY.has(ManagerClass)) { QUERY_METADATA_REGISTRY.get(ManagerClass).push([method, queryType]); } else { QUERY_METADATA_REGISTRY.set(ManagerClass, [[method, queryType]]); } }); } export { OnQuery };