"use strict" ; import { FunctionalUseCaseContext } from "./FunctionalUseCaseContext" ; import { UseCaseLike } from "./UseCaseLike" ; import { Dispatcher } from "./Dispatcher" ; import { generateNewId } from "./UseCaseIdGenerator" ; import { DispatcherPayloadMetaImpl } from "./DispatcherPayloadMeta" ; import { ErrorPayload } from "./payload/ErrorPayload" ; import { Payload } from "./payload/Payload" ; import { assertOK } from "./util/assert" ; export const defaultUseCaseName = "<Functiona-UseCase>" ; export type UseCaseArgs = ( context : FunctionalUseCaseContext ) => Function ; const getFunctionalExecute = ( functionalUseCase , context ) : Function => { try { const execute = functionalUseCase ( context ) ; if ( process . env . NODE_ENV !== "production" ) { assertOK ( typeof execute === "function" , "Functional UseCase should return a executor function." ) ; } return execute ; } catch ( error ) { if ( process . env . NODE_ENV !== "production" ) { console . error ( `Warning(UseCase): This is wrong Functional UseCase.
Example:
    
    const useCase = ({ dispatcher }) => {
        return (args) => {
            // execute
        }
    };
    
    context.useCase(useCase).execute("args");
` ) ; } throw error ; } } ; example export class FunctionalUseCase extends Dispatcher implements UseCaseLike { /**
     * unique id in each UseCase instances.
     */ id : string ; /**
     * Executor function
     */ executor : Function ; /**
     * The default is UseCase name
     */ name : string ; constructor ( functionUseCase ) { super ( ) ; const dispatcher = { dispatch : ( payload ) => { const useCaseMeta = new DispatcherPayloadMetaImpl ( { useCase : this , parentUseCase : null , isTrusted : false , isUseCaseFinished : false } ) ; this . dispatch ( payload , useCaseMeta ) ; } } ; const context = { dispatcher } ; this . executor = getFunctionalExecute ( functionUseCase , context ) ; this . id = generateNewId ( ) ; this . name = ( functionUseCase as any ) . displayName || functionUseCase . name || defaultUseCaseName ; } /**
     * execute functional UseCase
     */ execute ( ... _ ) { return this . executor ( ... _ ) ; } param error throwError ( error ? ) { const meta = new DispatcherPayloadMetaImpl ( { useCase : this , isTrusted : true , isUseCaseFinished : false } ) ; const payload = new ErrorPayload ( { error } ) ; this . dispatch ( payload , meta ) ; } }