fix: move the import progress bar message by message for mbox files

This commit is contained in:
Linus Rath
2026-11-01 05:45:15 +01:00
parent d8f6be7b1e
commit b07aad512a
2 changed files with 13 additions and 1 deletions
+8
View File
@@ -129,6 +129,14 @@ describe('runImport', () => {
expect(fake.imported[0].receivedAt).toBe('2020-05-05T10:00:00Z');
});
it('moves the progress message by message through a small mbox', async () => {
const fake = fakeClient();
const fractions: number[] = [];
await runImport(options(fake.client, [file(mbox(10))], { onProgress: (p) => { if (p.phase === 'running') fractions.push(p.fraction); } }));
const between = new Set(fractions.filter((f) => f > 0 && f < 1).map((f) => f.toFixed(2)));
expect(between.size).toBeGreaterThanOrEqual(5);
});
it('puts several messages in one Email/import and keeps uploads within the server limit', async () => {
const fake = fakeClient({ uploadConcurrency: 4 });
await runImport(options(fake.client, [file(mbox(45))]));
+5 -1
View File
@@ -180,11 +180,14 @@ async function* mboxFromChunks(
options: SourceOptions,
startIndex: number,
onIndex: (next: number) => void,
/** Called with each message's offset in the stream, for a progress that moves message by message. */
onOffset?: (offset: number) => void,
): ItemSource {
const splitter = new MboxSplitter({ maxMessageBytes: options.maxMessageBytes });
let index = startIndex;
const emit = function* (messages: MboxMessage[]): Generator<ImportItem> {
for (const m of messages) {
onOffset?.(m.offset);
const i = index++;
yield i < options.skipBefore ? { kind: "skipped", index: i, reason: "resumed" } : mboxItem(m, i, folder);
}
@@ -206,7 +209,8 @@ async function* fileChunks(file: Blob, onProgress?: (fraction: number) => void):
}
export async function* mboxSource(file: File, options: SourceOptions): ItemSource {
yield* mboxFromChunks(fileChunks(file, options.onProgress), [], options, 0, () => {});
// Reads run ahead in 1 MB pieces; the messages handed over say how far the import really is.
yield* mboxFromChunks(fileChunks(file), [], options, 0, () => {}, (offset) => options.onProgress?.(offset / Math.max(1, file.size)));
options.onProgress?.(1);
}