import { Runner } from '../runner/runner' ; import { ExecutionOptions } from './execution-options' ; type Fn < Result , Param > = ( params : { result : Result ; param : Param ; executionOptions : ExecutionOptions ; } ) => void ; type Id = number ; export abstract class UseCase < Result = void , Param = void > { abstract readonly : boolean ; abstract internalExecute ( param ) ; private static currentId = 0 ; private subscriptions = new Map < Id , Fn < Result , Param > > ( ) ; subscribe ( fn ) { const id = UseCase . currentId ++ ; this . subscriptions . set ( id , fn ) ; return id ; } unsubscribe ( id ) { this . subscriptions . delete ( id ) ; } async execute ( param , executionOptions = { inlineError : false } ) { const value = ( await Runner . run ( this as any , executionOptions , param ) ) as Result ; this . subscriptions . forEach ( x => x ( { result : value , param , executionOptions } ) ) ; return value ; } }