import { MessageType } from 'graphql-ws' ; import { assign } from './model' ; import { ServerClosure , StateFunctionInput } from './types' ; import { sendMessage , deleteConnection } from './utils' ; export const handleStateMachineEvent = ( c ) => async ( input ) : Promise < StateFunctionInput > => { const connection = assign ( new c . model . Connection ( ) , { id : input . connectionId , } ) ; if ( input . state === 'PING' ) { await sendMessage ( { ... input , message : { type : MessageType . Ping } } ) ; await c . mapper . update ( assign ( connection , { hasPonged : false } ) , { onMissing : 'skip' , } ) ; return { ... input , state : 'REVIEW' , seconds : c . ping ! . timeout , } ; } const conn = await c . mapper . get ( connection ) ; if ( conn . hasPonged ) { return { ... input , state : 'PING' , seconds : c . ping ?. interval ! - c . ping ! . timeout , } ; } await deleteConnection ( { ... input } ) ; return { ... input , state : 'ABORT' , } ; } ;