Files
webmail/lib/mail-import/locks.ts
T
Linus Rath 8d48f98261 fix: keep failed messages for the next import run and stop an import that can't go on
A message that failed for a passing reason (a dropped connection, a
server error, an expired token) used to count as done, so continuing
the import skipped it for good, and a slow upload followed by a cancel
imported the messages after it twice.

- The checkpoint keeps every message that is done, also past a gap,
  and the next run of the same import tries only the rest again.
- Ten failures in a row, a full mailbox (overQuota) or a refusal
  (forbidden) stop the import with a message instead of going through
  the whole file.
- An upload without an answer pauses the import instead of failing the
  message: 30 seconds, then until the upload window ends.
- Email/import, the export's Blob/get and folder listing pass the
  client's own 429 pause by, and a 429 on them doesn't pause the rest
  of the app.
- Signing out stops the job of that account only, writes no checkpoint
  and imports nothing more afterwards; another account's sign-out
  leaves the job alone.
- One tab imports a file at a time (Web Locks), and Import waits until
  the dialog knows whether an earlier run can be continued.

The import dialog tests now wait for the Import button to be enabled
before clicking it, since it stays off during that lookup.
2026-11-01 06:37:08 +01:00

37 lines
1.3 KiB
TypeScript

/**
* Web Locks across the tabs of this browser, held until released: one tab
* imports a file at a time, and an export's private file isn't swept while
* the tab that wrote it still uses it. Where the browser has no Web Locks,
* every request succeeds (one tab can't know about another then).
*/
interface LockManagerLike {
request(name: string, options: { ifAvailable: boolean }, callback: (lock: unknown) => Promise<void> | void): Promise<unknown>;
}
function lockManager(): LockManagerLike | null {
const locks = typeof navigator !== "undefined" ? (navigator as { locks?: LockManagerLike }).locks : undefined;
return locks && typeof locks.request === "function" ? locks : null;
}
export function hasWebLocks(): boolean {
return lockManager() !== null;
}
/** Takes the lock if nobody holds it: a release function, or null when another tab has it. */
export function holdLock(name: string): Promise<(() => void) | null> {
const locks = lockManager();
if (!locks) return Promise.resolve(() => {});
return new Promise((resolve) => {
locks
.request(name, { ifAvailable: true }, (lock) => {
if (!lock) {
resolve(null);
return;
}
return new Promise<void>((release) => resolve(release));
})
.catch(() => resolve(() => {}));
});
}