dreamstate
Version:
Store management library based on react context and observers
49 lines (46 loc) • 2.2 kB
JavaScript
import { DreamstateError } from '../error/DreamstateError.js';
import { SIGNAL_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 { isCorrectSignalType } from '../../utils/typechecking.js';
/**
* Class method decorator factory that marks the decorated method as a handler for specified signal types.
*
* This decorator ensures that the decorated method is invoked when a signal of the specified type(s)
* is emitted within the current scope. It supports handling a single signal type or an array of signal types.
*
* Supported signal types include: `string`, `number`, and `symbol`.
*
* @param {(TSignalType | Array<TSignalType>)} signalType - The signal type or an array of signal types
* that the decorated method will handle.
* @returns {MethodDecorator} A method decorator that attaches the handler functionality to the method.
*/
function OnSignal(signalType) {
/*
* If Array:
* - Check not empty
* - Validate all elements
* If single type:
* - Check the only value
*/
if (Array.isArray(signalType) ? signalType.length === 0 || signalType.some(function (it) {
return !isCorrectSignalType(it);
}) : !isCorrectSignalType(signalType)) {
throw new DreamstateError(EDreamstateErrorCode.INCORRECT_PARAMETER, "Unexpected signal type provided, expected symbol, string, number or array of it. Got: ".concat(typeof signalType, "."));
}
/*
* Support old and new decorators with polyfill.
*/
return createMethodDecorator(function (method, ManagerClass) {
if (!(ManagerClass.prototype instanceof ContextManager)) {
throw new DreamstateError(EDreamstateErrorCode.INCORRECT_PARAMETER, "Only ContextManager extending classes methods can be decorated as handlers.");
}
if (SIGNAL_METADATA_REGISTRY.has(ManagerClass)) {
SIGNAL_METADATA_REGISTRY.get(ManagerClass).push([method, signalType]);
} else {
SIGNAL_METADATA_REGISTRY.set(ManagerClass, [[method, signalType]]);
}
});
}
export { OnSignal };