Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/apps/draft-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,12 @@ async function generateLocalFormSubmissionDraftsFromStorage({
localDraftsStorage,
abortSignal,
priorityFn,
initialBroadcastCB,
}: {
localDraftsStorage: LocalDraftsStorage
abortSignal: AbortSignal | undefined
priorityFn?: PriorityFn
initialBroadcastCB?: () => void
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
initialBroadcastCB?: () => void
onBeforeBroadcast?: () => void

?

}): Promise<LocalFormSubmissionDraft[]> {
const pendingSubmissionsDraftIds = await getPendingSubmissionsDraftIds()
const deletedDraftIds = new Set(
Expand Down Expand Up @@ -194,6 +196,7 @@ async function generateLocalFormSubmissionDraftsFromStorage({
}

await broadcastUpdate()
initialBroadcastCB?.()

if (draftsToDownload.length) {
if (priorityFn) {
Expand Down Expand Up @@ -769,10 +772,12 @@ async function setAndBroadcastDrafts({
localDraftsStorage,
abortSignal,
priorityFn,
initialBroadcastCB,
}: {
localDraftsStorage: LocalDraftsStorage
abortSignal: AbortSignal | undefined
priorityFn: PriorityFn | undefined
initialBroadcastCB?: () => void
}): Promise<void> {
const username = getUsername()
if (!username) {
Expand All @@ -794,6 +799,7 @@ async function setAndBroadcastDrafts({
localDraftsStorage,
abortSignal,
priorityFn,
initialBroadcastCB,
})
await executeDraftsListeners(localFormSubmissionDrafts)
}
Expand All @@ -818,6 +824,7 @@ let _isSyncingDrafts = false
* @returns
*/
async function syncDrafts({
initialBroadcastCB,
priorityFn,
formsAppId,
throwError,
Expand All @@ -831,6 +838,8 @@ async function syncDrafts({
* argument, zero if they're equal, and a positive value otherwise.
*/
priorityFn?: PriorityFn
/** A callback to be called when the drafts are initially broadcast */
initialBroadcastCB?: () => void
/** `true` to throw errors while syncing */
throwError?: boolean
/** Signal to abort the requests */
Expand Down Expand Up @@ -931,7 +940,12 @@ async function syncDrafts({
}

console.log('Downloading drafts in the background')
setAndBroadcastDrafts({ localDraftsStorage, abortSignal, priorityFn })
setAndBroadcastDrafts({
localDraftsStorage,
abortSignal,
priorityFn,
initialBroadcastCB,
})
.then(async () => {
console.log('Finished syncing drafts.')
})
Expand Down
27 changes: 16 additions & 11 deletions src/hooks/useDrafts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,30 @@ export function DraftsContextProvider({
}))
}

let newError = null

try {
await draftService.syncDrafts({
formsAppId,
priorityFn,
throwError: true,
abortSignal,
initialBroadcastCB: () => {
if (isMounted.current) {
setSyncState({
lastSyncTime: new Date(),
isSyncing: false,
syncError: null,
})
}
},
})
} catch (error) {
newError = error as Error
}

if (isMounted.current) {
setSyncState({
lastSyncTime: new Date(),
isSyncing: false,
syncError: newError,
})
if (isMounted.current) {
setSyncState({
lastSyncTime: new Date(),
isSyncing: false,
syncError: error as Error,
})
}
}
},
[formsAppId, isDraftsEnabled, isMounted, isUsingFormsKey],
Expand Down