forked from reconbot/graphql-lambda-subscriptions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleStepFunctionEvent.ts
38 lines (34 loc) · 1.16 KB
/
handleStepFunctionEvent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { MessageType } from 'graphql-ws'
import { ServerClosure, SubscriptionServer } from './types'
import { postToConnection } from './utils/postToConnection'
import { deleteConnection } from './utils/deleteConnection'
export const handleStepFunctionEvent = (serverPromise: Promise<ServerClosure>): SubscriptionServer['stepFunctionsHandler'] => async (input) => {
const server = await serverPromise
if (!server.pingpong) {
throw new Error('Invalid pingpong settings')
}
// Initial state - send ping message
if (input.state === 'PING') {
await postToConnection(server)({ ...input, message: { type: MessageType.Ping } })
await server.models.connection.update({ id: input.connectionId }, { hasPonged: false })
return {
...input,
state: 'REVIEW',
seconds: server.pingpong.delay,
}
}
// Follow up state - check if pong was returned
const conn = await server.models.connection.get({ id: input.connectionId })
if (conn?.hasPonged) {
return {
...input,
state: 'PING',
seconds: server.pingpong.timeout,
}
}
await deleteConnection(server)({ ...input })
return {
...input,
state: 'ABORT',
}
}