Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit 2a9ab90

Browse files
committed
AP-6563 # pass abort signals around everywhere
1 parent 7c2ec41 commit 2a9ab90

1 file changed

Lines changed: 37 additions & 13 deletions

File tree

src/draft-service.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ interface LocalDraftsStorage {
4343

4444
async function checkIfUsingPrivateDrafts(
4545
formsAppId: number,
46-
abortSignal?: AbortSignal,
46+
abortSignal: AbortSignal | undefined,
4747
): Promise<boolean> {
4848
return getCurrentFormsAppUser(formsAppId, abortSignal)
4949
.then((user) => !!user)
@@ -312,6 +312,7 @@ async function upsertDraft({
312312
try {
313313
const isUsingPrivateDrafts = await checkIfUsingPrivateDrafts(
314314
draftSubmission.formsAppId,
315+
abortSignal,
315316
)
316317
const formSubmissionDraftVersion = await saveDraftSubmission({
317318
draftSubmission,
@@ -396,6 +397,7 @@ async function upsertDraft({
396397
syncDrafts({
397398
throwError: false,
398399
formsAppId: draftSubmission.formsAppId,
400+
abortSignal,
399401
})
400402
} catch (err) {
401403
Sentry.captureException(err)
@@ -518,7 +520,10 @@ async function getDraftAndData(
518520
formsAppId,
519521
abortSignal,
520522
)
521-
const isUsingPrivateDrafts = await checkIfUsingPrivateDrafts(formsAppId)
523+
const isUsingPrivateDrafts = await checkIfUsingPrivateDrafts(
524+
formsAppId,
525+
abortSignal,
526+
)
522527
if (isUsingPrivateDrafts) {
523528
const localDraftsStorage = await getLocalDraftsFromStorage()
524529
if (formSubmissionDrafts) {
@@ -564,7 +569,10 @@ async function deleteDraft(
564569
): Promise<void> {
565570
try {
566571
await removeLocalDraftSubmission(formSubmissionDraftId)
567-
const isUsingPrivateDrafts = await checkIfUsingPrivateDrafts(formsAppId)
572+
const isUsingPrivateDrafts = await checkIfUsingPrivateDrafts(
573+
formsAppId,
574+
abortSignal,
575+
)
568576
if (isUsingPrivateDrafts) {
569577
const localDraftsStorage = await getLocalDraftsFromStorage()
570578
const formSubmissionDraft =
@@ -631,6 +639,7 @@ async function deleteDraft(
631639
syncDrafts({
632640
throwError: false,
633641
formsAppId,
642+
abortSignal,
634643
})
635644
} catch (err) {
636645
Sentry.captureException(err)
@@ -675,8 +684,6 @@ async function setAndBroadcastDrafts(
675684
await executeDraftsListeners(localFormSubmissionDrafts)
676685
}
677686

678-
let _isSyncingDrafts = false
679-
680687
/**
681688
* Force a sync of remote drafts with locally stored drafts. This function will
682689
* swallow all errors thrown unless `true` is passed for the `throwError`
@@ -704,15 +711,22 @@ async function syncDrafts({
704711
/** `true` to throw errors while syncing */
705712
throwError?: boolean
706713
/** Signal to abort the requests */
707-
abortSignal?: AbortSignal
714+
abortSignal: AbortSignal | undefined
708715
}): Promise<void> {
709-
if (_isSyncingDrafts) {
716+
if (abortSignal?.aborted) {
710717
console.log('Application is currently syncing drafts.')
711718
return
712719
}
713-
_isSyncingDrafts = true
714720

715-
const isUsingPrivateDrafts = await checkIfUsingPrivateDrafts(formsAppId)
721+
const isUsingPrivateDrafts = await checkIfUsingPrivateDrafts(
722+
formsAppId,
723+
abortSignal,
724+
)
725+
726+
if (abortSignal?.aborted) {
727+
// TODO some cool console log maybe?
728+
return
729+
}
716730

717731
if (!isUsingPrivateDrafts) {
718732
const publicDrafts = await getPublicDraftsFromStorage()
@@ -728,8 +742,11 @@ async function syncDrafts({
728742
filteredPublicDrafts.push(publicDraft)
729743
}
730744
}
745+
if (abortSignal?.aborted) {
746+
// TODO some cool console log maybe?
747+
return
748+
}
731749
await setAndBroadcastPublicDrafts(filteredPublicDrafts)
732-
_isSyncingDrafts = false
733750
return
734751
}
735752

@@ -742,6 +759,10 @@ async function syncDrafts({
742759
)
743760
const newDeletedFormSubmissionDrafts: SubmissionTypes.FormSubmissionDraft[] =
744761
[]
762+
if (abortSignal?.aborted) {
763+
// TODO some cool console log maybe?
764+
return
765+
}
745766
for (const formSubmissionDraft of localDraftsStorage.deletedFormSubmissionDrafts) {
746767
const { hasDeletedRemoteDraft } = await deleteDraftData(
747768
formSubmissionDraft.id,
@@ -761,11 +782,16 @@ async function syncDrafts({
761782
const publicDraftsStorage = await getPublicDraftsFromStorage()
762783
// if public drafts exist, add them to the current logged in users' unsynced drafts
763784
// and remove them from local storage
764-
if (publicDraftsStorage.length) {
785+
if (publicDraftsStorage.length && !abortSignal?.aborted) {
765786
localDraftsStorage.unsyncedDraftSubmissions.push(...publicDraftsStorage)
766787
await utilsService.localForage.setItem(generatePublicDraftsKey(), [])
767788
}
768789

790+
if (abortSignal?.aborted) {
791+
// TODO some cool console log maybe?
792+
return
793+
}
794+
769795
if (localDraftsStorage.unsyncedDraftSubmissions.length) {
770796
console.log(
771797
`Attempting to upload ${localDraftsStorage.unsyncedDraftSubmissions.length} local unsynced drafts(s).`,
@@ -817,9 +843,7 @@ async function syncDrafts({
817843
}
818844

819845
console.log('Finished syncing drafts.')
820-
_isSyncingDrafts = false
821846
} catch (error) {
822-
_isSyncingDrafts = false
823847
if (abortSignal?.aborted) {
824848
console.log('Syncing drafts has been aborted')
825849
return

0 commit comments

Comments
 (0)