fix: back off conversion status polling
This commit is contained in:
parent
add83528d2
commit
ffc8fb0eee
|
|
@ -10,12 +10,22 @@ let syncPromise: Promise<void> | null = null;
|
|||
let syncTriggersStarted = false;
|
||||
let conversionPollTimer: number | null = null;
|
||||
let conversionPollAttempts = 0;
|
||||
let conversionPollDeadline = 0;
|
||||
const conversionPollEntryIds = new Set<string>();
|
||||
|
||||
function scheduleConversionPoll(entryIds: string[], resetAttempts = true) {
|
||||
for (const entryId of entryIds) conversionPollEntryIds.add(entryId);
|
||||
if (conversionPollTimer !== null || typeof window === "undefined") return;
|
||||
if (resetAttempts) conversionPollAttempts = 0;
|
||||
if (resetAttempts) {
|
||||
conversionPollAttempts = 0;
|
||||
conversionPollDeadline = Date.now() + 60_000;
|
||||
}
|
||||
const remainingMs = conversionPollDeadline - Date.now();
|
||||
if (remainingMs <= 0) {
|
||||
conversionPollEntryIds.clear();
|
||||
return;
|
||||
}
|
||||
const delayMs = Math.min(1_500 * 2 ** conversionPollAttempts, remainingMs);
|
||||
const poll = async () => {
|
||||
conversionPollTimer = null;
|
||||
const store = useEntryStore();
|
||||
|
|
@ -25,11 +35,11 @@ function scheduleConversionPoll(entryIds: string[], resetAttempts = true) {
|
|||
store.entries.some((entry) => entry.id === entryId && entry.conversionStatus === "pending"),
|
||||
);
|
||||
conversionPollEntryIds.clear();
|
||||
if (!remaining.length || conversionPollAttempts >= 6) return;
|
||||
if (!remaining.length || Date.now() >= conversionPollDeadline) return;
|
||||
conversionPollAttempts += 1;
|
||||
scheduleConversionPoll(remaining, false);
|
||||
};
|
||||
conversionPollTimer = window.setTimeout(() => void poll(), 1_500);
|
||||
conversionPollTimer = window.setTimeout(() => void poll(), delayMs);
|
||||
}
|
||||
|
||||
export const useEntryStore = defineStore("entries", {
|
||||
|
|
|
|||
Loading…
Reference in New Issue