-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresolve-action-attempt.ts
More file actions
128 lines (108 loc) · 3.6 KB
/
resolve-action-attempt.ts
File metadata and controls
128 lines (108 loc) · 3.6 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import type { ActionAttempt } from './action-attempt.js'
import type { SeamHttpActionAttempts } from './routes/index.js'
export interface ResolveActionAttemptOptions {
timeout?: number
pollingInterval?: number
}
export const resolveActionAttempt = async <T extends ActionAttempt>(
actionAttempt: T,
actionAttempts: SeamHttpActionAttempts,
{ timeout = 10_000, pollingInterval = 1_000 }: ResolveActionAttemptOptions,
): Promise<SucceededActionAttempt<T>> => {
let timeoutRef
const timeoutPromise = new Promise<SucceededActionAttempt<T>>(
(_resolve, reject) => {
timeoutRef = globalThis.setTimeout(() => {
reject(new SeamActionAttemptTimeoutError<T>(actionAttempt, timeout))
}, timeout)
},
)
try {
return await Promise.race([
pollActionAttempt<T>(actionAttempt, actionAttempts, { pollingInterval }),
timeoutPromise,
])
} finally {
if (timeoutRef != null) globalThis.clearTimeout(timeoutRef)
}
}
const pollActionAttempt = async <T extends ActionAttempt>(
actionAttempt: T,
actionAttempts: SeamHttpActionAttempts,
options: Pick<ResolveActionAttemptOptions, 'pollingInterval'>,
): Promise<SucceededActionAttempt<T>> => {
if (isSuccessfulActionAttempt(actionAttempt)) {
return actionAttempt
}
if (isFailedActionAttempt(actionAttempt)) {
throw new SeamActionAttemptFailedError(actionAttempt)
}
await new Promise((resolve) => setTimeout(resolve, options.pollingInterval))
const nextActionAttempt = await actionAttempts.get({
action_attempt_id: actionAttempt.action_attempt_id,
})
return await pollActionAttempt(
nextActionAttempt as unknown as T,
actionAttempts,
options,
)
}
export const isSeamActionAttemptError = <T extends ActionAttempt>(
error: unknown,
): error is SeamActionAttemptError<T> => {
return error instanceof SeamActionAttemptError
}
export class SeamActionAttemptError<T extends ActionAttempt> extends Error {
actionAttempt: T
constructor(message: string, actionAttempt: T) {
super(message)
this.name = this.constructor.name
this.actionAttempt = actionAttempt
}
}
export const isSeamActionAttemptFailedError = <T extends ActionAttempt>(
error: unknown,
): error is SeamActionAttemptFailedError<T> => {
return error instanceof SeamActionAttemptFailedError
}
export class SeamActionAttemptFailedError<
T extends ActionAttempt,
> extends SeamActionAttemptError<T> {
code: string
constructor(actionAttempt: FailedActionAttempt<T>) {
super(actionAttempt.error.message, actionAttempt)
this.name = this.constructor.name
this.code = actionAttempt.error.type
}
}
export const isSeamActionAttemptTimeoutError = <T extends ActionAttempt>(
error: unknown,
): error is SeamActionAttemptTimeoutError<T> => {
return error instanceof SeamActionAttemptTimeoutError
}
export class SeamActionAttemptTimeoutError<
T extends ActionAttempt,
> extends SeamActionAttemptError<T> {
constructor(actionAttempt: T, timeout: number) {
super(
`Timed out waiting for action action attempt after ${timeout}ms`,
actionAttempt,
)
this.name = this.constructor.name
}
}
const isSuccessfulActionAttempt = <T extends ActionAttempt>(
actionAttempt: T,
): actionAttempt is SucceededActionAttempt<T> =>
actionAttempt.status === 'success'
const isFailedActionAttempt = <T extends ActionAttempt>(
actionAttempt: T,
): actionAttempt is FailedActionAttempt<T> => actionAttempt.status === 'error'
export type SucceededActionAttempt<T extends ActionAttempt> = Extract<
T,
{ status: 'success' }
>
export type FailedActionAttempt<T extends ActionAttempt> = Extract<
T,
{ status: 'error' }
>