import { Context , Store , Dispatcher , UseCase , Payload , WillExecutedPayload , DidExecutedPayload , CompletedPayload , ErrorPayload , DispatcherPayloadMeta , FunctionalUseCaseContext , StoreGroup } from "../../src/index" ; const dispatcher = new Dispatcher ( ) ; interface AState { a : number ; } class AStore extends Store < AState > { state : AState ; constructor ( ) { super ( ) ; this . state = { a : 1 } ; } getState ( ) { return this . state ; } } interface BState { b : number ; } class BStore extends Store < BState > { state : BState ; constructor ( ) { super ( ) ; this . state = { b : 2 } ; } receivePayload ( _payload ) { this . setState ( { b : 1 } ) ; } getState ( ) { return this . state ; } } const storeGroup = new StoreGroup ( { aState : new AStore ( ) , bState : new BStore ( ) } ) ; const context = new Context ( { dispatcher , store : storeGroup , options : { strict : true } } ) ; const log = ( ... _messages ) => { } ; context . events . onWillExecuteEachUseCase ( ( payload , meta ) => { log ( payload . args , meta ) ; } ) ; context . events . onDidExecuteEachUseCase ( ( payload , meta ) => { log ( payload . value , meta ) ; } ) ; context . events . onCompleteEachUseCase ( ( payload , meta ) => { log ( payload . value , meta ) ; } ) ; context . events . onErrorDispatch ( ( payload , meta ) => { log ( payload . error , meta ) ; } ) ; context . events . onDispatch ( ( payload , meta ) => { log ( payload . type , meta ) ; } ) ; class ChildUseCase extends UseCase { execute ( value ) { this . dispatch ( { type : "ChildUseCase" , value } ) ; } } type ParentUseCaseArgs = string ; class ParentUseCase extends UseCase { execute ( value ) { return this . context . useCase ( new ChildUseCase ( ) ) . execute ( value ) ; } } const parentUseCase = new ParentUseCase ( ) ; type functionUseCaseArgs = string | undefined ; const functionalUseCase = ( context ) => { return ( value ) => { context . dispatcher . dispatch ( { type : value } ) ; } ; } ; context . useCase ( functionalUseCase ) . execute ( "1" ) . then ( ( ) => { const state = context . getState ( ) ; log ( state . aState . a ) ; log ( state . bState . b ) ; } ) ; context . useCase ( functionalUseCase ) . execute ( "value" ) . then ( ( ) => { } ) ; context . useCase ( parentUseCase ) . execute ( "value" ) . then ( ( ) => { const state = context . getState ( ) ; log ( state . aState . a ) ; log ( state . bState . b ) ; } ) . catch ( ( error ) => { console . error ( error ) ; } ) ; context . useCase ( parentUseCase ) . execute ( "value" ) . then ( ( ) => { } ) ; class MyUseCase extends UseCase { execute ( value ) { this . dispatch ( { type : "ChildUseCase" , value } ) ; } } context . useCase ( new MyUseCase ( ) ) . executor ( ( useCase ) => useCase . execute ( "value" ) ) . then ( ( ) => { log ( "finish" ) ; } ) ; context . transaction ( "my work" , async ( transactionContext ) => { await transactionContext . useCase ( new MyUseCase ( ) ) . execute ( "string" ) ; await transactionContext . useCase ( new ParentUseCase ( ) ) . execute ( "string" ) ; transactionContext . commit ( ) ; } ) . then ( ( ) => { log ( "Finish" ) ; } ) ;