import { UseCaseLike } from "../UseCaseLike" ; import { StoreLike } from "../StoreLike" ; import { StoreGroupLike } from "../UILayer/StoreGroupLike" ; import { AlminPerfMarkerAbstract , DebugId , MarkType } from "./AlminAbstractPerfMarker" ; import { Transaction } from "../DispatcherPayloadMeta" ; import { Events } from "../Events" ; const canUsePerformanceMeasure = typeof performance !== "undefined" && typeof performance . mark === "function" && typeof performance . clearMarks === "function" && typeof performance . measure === "function" && typeof performance . clearMeasures === "function" ; export type AlminPerfMarkerActions = { type : "beforeStoreGroupReadPhase" ; } | { type : "afterStoreGroupReadPhase" ; } | { type : "beforeStoreGroupWritePhase" ; } | { type : "afterStoreGroupWritePhase" ; } | { type : "beforeStoreGetState" ; } | { type : "afterStoreGetState" ; } | { type : "beforeStoreReceivePayload" ; } | { type : "afterStoreReceivePayload" ; } | { type : "willUseCaseExecute" ; } | { type : "didUseCaseExecute" ; } | { type : "completeUseCaseExecute" ; } | { type : "beginTransaction" ; } | { type : "endTransaction" ; } ; export class AlminPerfMarker extends Events < AlminPerfMarkerActions > implements AlminPerfMarkerAbstract { private _isProfiling = false ; beginProfile ( ) { this . _isProfiling = true ; } get isProfiling ( ) : boolean { return this . _isProfiling ; } endProfile ( ) { this . _isProfiling = false ; this . removeAllEventListeners ( ) ; } shouldMark ( _debugId ) { if ( ! this . _isProfiling ) { return false ; } return canUsePerformanceMeasure ; } markBegin = ( debugID , markType ) => { if ( ! this . shouldMark ( debugID ) ) { return ; } const markName = `almin::${ debugID }::${ markType }` ; performance . mark ( markName ) ; } ; markEnd = ( debugID , markType , displayName ) => { if ( ! this . shouldMark ( debugID ) ) { return ; } const markName = `almin::${ debugID }::${ markType }` ; const measureName = `${ displayName } [${ markType }]` ; performance . measure ( measureName , markName ) ; performance . clearMarks ( markName ) ; performance . clearMeasures ( measureName ) ; } ; beforeStoreGroupReadPhase ( debugId , _storeGroup ) { this . markBegin ( debugId , "StoreGroup#read" ) ; this . emit ( { type : "beforeStoreGroupReadPhase" } ) ; } afterStoreGroupReadPhase ( debugId , storeGroup ) { const displayName = storeGroup . name ; this . markEnd ( debugId , "StoreGroup#read" , displayName ) ; this . emit ( { type : "afterStoreGroupReadPhase" } ) ; } beforeStoreGroupWritePhase ( debugId , _storeGroup ) { this . markBegin ( debugId , "StoreGroup#write" ) ; this . emit ( { type : "beforeStoreGroupWritePhase" } ) ; } afterStoreGroupWritePhase ( debugId , storeGroup ) { const displayName = storeGroup . name ; this . markEnd ( debugId , "StoreGroup#write" , displayName ) ; this . emit ( { type : "afterStoreGroupWritePhase" } ) ; } beforeStoreGetState ( debugId , _store ) { this . markBegin ( debugId , "Store#getState" ) ; this . emit ( { type : "beforeStoreGetState" } ) ; } afterStoreGetState ( debugId , store ) { const displayName = store . name ; this . markEnd ( debugId , "Store#getState" , displayName ) ; this . emit ( { type : "afterStoreGetState" } ) ; } beforeStoreReceivePayload ( debugId , _store ) { this . markBegin ( debugId , "Store#receivePayload" ) ; this . emit ( { type : "beforeStoreReceivePayload" } ) ; } afterStoreReceivePayload ( debugId , store ) { const displayName = store . name ; this . markEnd ( debugId , "Store#receivePayload" , displayName ) ; this . emit ( { type : "afterStoreReceivePayload" } ) ; } willUseCaseExecute ( debugId , _useCase ) { this . markBegin ( debugId , "UserCase#execute" ) ; this . emit ( { type : "willUseCaseExecute" } ) ; } didUseCaseExecute ( debugId , useCase ) { const displayName = useCase . name ; this . markEnd ( debugId , "UserCase#execute" , displayName ) ; this . markBegin ( debugId , "UserCase#complete" ) ; this . emit ( { type : "didUseCaseExecute" } ) ; } completeUseCaseExecute ( debugId , useCase ) { const displayName = useCase . name ; this . markEnd ( debugId , "UserCase#complete" , displayName ) ; this . emit ( { type : "completeUseCaseExecute" } ) ; } beginTransaction ( debugId , _transaction ) { this . markBegin ( debugId , "Transaction" ) ; this . emit ( { type : "beginTransaction" } ) ; } endTransaction ( debugId , transaction ) { const displayName = transaction . name ; this . markEnd ( debugId , "Transaction" , displayName ) ; this . emit ( { type : "endTransaction" } ) ; } }