-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
29 lines (27 loc) · 751 Bytes
/
index.js
File metadata and controls
29 lines (27 loc) · 751 Bytes
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
export const useAbortable = ({ updateCb, catchCb, finallyCb } = {}) => {
const controller = new AbortController()
let inProgress = false
let $updateCb = updateCb
let $catchCb = catchCb
let $finallyCb = finallyCb
return {
setCallbacks({ updateCb, catchCb, finallyCb }) {
$updateCb = updateCb
$catchCb = catchCb
$finallyCb = finallyCb
},
get signal() { return controller.signal },
async update(...args) {
if (inProgress) controller.abort('@itsy/abortable sent abort signal during update')
inProgress = true
try {
return await $updateCb(...args)
} catch (err) {
$catchCb?.(err)
} finally {
inProgress = false
$finallyCb?.()
}
}
}
}