import { given } from "@nivinjoseph/n-defensive"; import "@nivinjoseph/n-ext"; import { ObserverEdaEventHandler, ObserverEdaEventHandlerClass } from "./observer-eda-event-handler.js"; import { EdaEvent, EdaEventClass } from "./eda-event.js"; import { ClassDefinition } from "@nivinjoseph/n-util"; export const observedEventSymbol = Symbol.for("@nivinjoseph/n-eda/observedEvent"); // public export function observedEvent< TEvent extends EdaEvent, This extends ObserverEdaEventHandler >(eventType: EdaEventClass): ObserverEventHandlerObservedEventDecorator { given(eventType, "eventType").ensureHasValue().ensureIsFunction(); const decorator: ObserverEventHandlerObservedEventDecorator = function (target, context) { given(context, "context") // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition .ensure(t => t.kind === "class", "observedEvent decorator should only be used on a class"); const className = context.name!; given(className, className).ensureHasValue().ensureIsString() .ensure(_ => typeof target.prototype["handle"] === "function", `class '${className}' should implement 'ObserverEdaEventHandler' interface`); context.metadata[observedEventSymbol] = eventType; }; return decorator; } export type ObserverEventHandlerObservedEventDecorator> = ( target: ObserverEdaEventHandlerClass, context: ClassDecoratorContext> ) => void; export const observableSymbol = Symbol.for("@nivinjoseph/n-eda/observable"); // public export function observable< TEvent extends EdaEvent, This extends ObserverEdaEventHandler >(type: ClassDefinition): ObserverEventHandlerObservableDecorator { given(type, "type").ensureHasValue().ensureIsFunction(); const decorator: ObserverEventHandlerObservableDecorator = function (target, context) { given(context, "context") // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition .ensure(t => t.kind === "class", "observable decorator should only be used on a class"); const className = context.name!; given(className, className).ensureHasValue().ensureIsString() .ensure(_ => typeof target.prototype["handle"] === "function", `class '${className}' should implement 'ObserverEdaEventHandler' interface`); context.metadata[observableSymbol] = type; }; return decorator; } export type ObserverEventHandlerObservableDecorator> = ( target: ObserverEdaEventHandlerClass, context: ClassDecoratorContext> ) => void; export const observerSymbol = Symbol.for("@nivinjoseph/n-eda/observer"); // public export function observer< TEvent extends EdaEvent, This extends ObserverEdaEventHandler >(type: ClassDefinition): ObserverEventHandlerObserverDecorator { given(type, "type").ensureHasValue().ensureIsFunction(); const decorator: ObserverEventHandlerObserverDecorator = function (target, context) { given(context, "context") // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition .ensure(t => t.kind === "class", "observer decorator should only be used on a class"); const className = context.name!; given(className, className).ensureHasValue().ensureIsString() .ensure(_ => typeof target.prototype["handle"] === "function", `class '${className}' should implement 'ObserverEdaEventHandler' interface`); context.metadata[observerSymbol] = type; }; return decorator; } export type ObserverEventHandlerObserverDecorator> = ( target: ObserverEdaEventHandlerClass, context: ClassDecoratorContext> ) => void;