@remit/web-client 0.0.75 → 0.0.76

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remit/web-client",
3
- "version": "0.0.75",
3
+ "version": "0.0.76",
4
4
  "type": "module",
5
5
  "description": "Remit web client, published as composable primitives — the app shell, auth shells, and runtime config. A distributor imports what it composes and bundles it.",
6
6
  "exports": {
@@ -130,8 +130,8 @@ export const MoveToTrigger = ({
130
130
  );
131
131
 
132
132
  const handleCreateFolder = useCallback(
133
- async (name: string): Promise<MoveMailboxOption> => {
134
- const folder = await createFolder(name);
133
+ async (name: string, signal?: AbortSignal): Promise<MoveMailboxOption> => {
134
+ const folder = await createFolder(name, signal);
135
135
  return { id: folder.id, label: folder.label };
136
136
  },
137
137
  [createFolder],
@@ -103,8 +103,9 @@ export function DeleteFolderDialog({
103
103
 
104
104
  const handleCreateFolder = async (
105
105
  name: string,
106
+ signal?: AbortSignal,
106
107
  ): Promise<MoveMailboxOption> => {
107
- const created = await createFolder(name);
108
+ const created = await createFolder(name, signal);
108
109
  return { id: created.id, label: created.label };
109
110
  };
110
111
 
@@ -1,9 +1,11 @@
1
1
  /**
2
- * useCreateMailbox.createFolder — the shared create seam the kit surfaces call.
3
- * It validates the typed name against the account's current folders with the
4
- * same IMAP-aware rules the settings form uses, and rejects with the
5
- * human-readable reason before any request. The mailbox list is seeded into the
6
- * query cache the hook reads, so validation runs against real paths.
2
+ * useCreateMailbox.createFolder — the shared create seam the kit surfaces call
3
+ * for a dependent write. It validates the typed name against the account's
4
+ * current folders with the same IMAP-aware rules the settings form uses, rejects
5
+ * with the human-readable reason before any request, then waits for the mail
6
+ * server to confirm the folder before resolving — so a filter or a move never
7
+ * binds to a still-pending row. The mailbox list is seeded into the query cache
8
+ * the hook reads, so validation runs against real paths.
7
9
  */
8
10
 
9
11
  import assert from "node:assert/strict";
@@ -13,8 +15,10 @@ import type {
13
15
  MailboxOperationsListMailboxesResponse,
14
16
  RemitImapMailboxResponse,
15
17
  } from "@remit/api-http-client/types.gen.ts";
18
+ import { MailboxSyncStatus } from "@remit/domain-enums";
16
19
  import type { FolderOption } from "@remit/ui";
17
20
  import { act, createElement } from "react";
21
+ import { MAILBOX_SYNC_FAILED_MESSAGE } from "../lib/mailbox-sync-wait";
18
22
  import { createDomHarness, type DomHarness } from "../test-support/dom";
19
23
  import { type HttpMock, mockFetch } from "../test-support/http";
20
24
  import { useCreateMailbox } from "./useCreateMailbox";
@@ -23,7 +27,9 @@ const ACCOUNT = "acc-1";
23
27
 
24
28
  let harness: DomHarness | undefined;
25
29
  let http: HttpMock | undefined;
26
- let createFolder: ((name: string) => Promise<FolderOption>) | undefined;
30
+ let createFolder:
31
+ | ((name: string, signal?: AbortSignal) => Promise<FolderOption>)
32
+ | undefined;
27
33
 
28
34
  afterEach(() => {
29
35
  harness?.close();
@@ -49,13 +55,23 @@ function Probe() {
49
55
  return null;
50
56
  }
51
57
 
52
- const mount = (items: RemitImapMailboxResponse[]) => {
58
+ const mount = (
59
+ items: RemitImapMailboxResponse[],
60
+ createdSyncStatus: RemitImapMailboxResponse["syncStatus"] = MailboxSyncStatus.synced,
61
+ ) => {
62
+ const created: RemitImapMailboxResponse[] = [];
53
63
  http = mockFetch((call) => {
54
64
  if (call.method === "POST") {
55
65
  const body = call.body as { fullPath: string };
66
+ created.push({
67
+ mailboxId: `mbx-${body.fullPath}`,
68
+ accountId: ACCOUNT,
69
+ fullPath: body.fullPath,
70
+ syncStatus: createdSyncStatus,
71
+ } as RemitImapMailboxResponse);
56
72
  return { mailboxId: `mbx-${body.fullPath}`, fullPath: body.fullPath };
57
73
  }
58
- return { items };
74
+ return { items: [...items, ...created] };
59
75
  });
60
76
  harness = createDomHarness();
61
77
  harness.queryClient.setQueryData<MailboxOperationsListMailboxesResponse>(
@@ -114,7 +130,7 @@ describe("useCreateMailbox.createFolder validation", () => {
114
130
  assert.equal(postCount(), 0);
115
131
  });
116
132
 
117
- it("passes a valid name through to the create request and maps the result", async () => {
133
+ it("passes a valid name through and resolves once the folder is confirmed synced", async () => {
118
134
  mount([mailbox("INBOX", "/")]);
119
135
  let result: FolderOption | undefined;
120
136
  await act(async () => {
@@ -126,6 +142,90 @@ describe("useCreateMailbox.createFolder validation", () => {
126
142
  fullPath: "Taxes",
127
143
  namespaceType: "personal",
128
144
  });
145
+ // It polled the list after the create to confirm the folder before resolving.
146
+ const gets = (http?.calls ?? []).filter((call) => call.method === "GET");
147
+ assert.ok(gets.length >= 1, "polls the mailbox list for confirmation");
129
148
  assert.equal(result?.label, "Taxes");
130
149
  });
150
+
151
+ it("rejects — no folder to bind a dependent write to — when the create is reported failed", async () => {
152
+ mount([mailbox("INBOX", "/")], MailboxSyncStatus.failed);
153
+ let caught: unknown;
154
+ await act(async () => {
155
+ caught = await createFolder?.("Taxes").then(
156
+ () => undefined,
157
+ (error: unknown) => error,
158
+ );
159
+ });
160
+ assert.ok(caught instanceof Error);
161
+ assert.equal(caught.message, MAILBOX_SYNC_FAILED_MESSAGE);
162
+ });
163
+
164
+ it("retry resumes the wait on the folder it already made — no second create, no 'already exists'", async () => {
165
+ // The created folder is reported failed on the first attempt, then synced.
166
+ let status: RemitImapMailboxResponse["syncStatus"] =
167
+ MailboxSyncStatus.failed;
168
+ const created: RemitImapMailboxResponse[] = [];
169
+ http = mockFetch((call) => {
170
+ if (call.method === "POST") {
171
+ const body = call.body as { fullPath: string };
172
+ created.push({
173
+ mailboxId: `mbx-${body.fullPath}`,
174
+ accountId: ACCOUNT,
175
+ fullPath: body.fullPath,
176
+ } as RemitImapMailboxResponse);
177
+ return { mailboxId: `mbx-${body.fullPath}`, fullPath: body.fullPath };
178
+ }
179
+ return {
180
+ items: [
181
+ mailbox("INBOX", "/"),
182
+ ...created.map((entry) => ({ ...entry, syncStatus: status })),
183
+ ],
184
+ };
185
+ });
186
+ harness = createDomHarness();
187
+ harness.queryClient.setQueryData<MailboxOperationsListMailboxesResponse>(
188
+ mailboxOperationsListMailboxesQueryKey({ path: { accountId: ACCOUNT } }),
189
+ { items: [mailbox("INBOX", "/")] },
190
+ );
191
+ harness.renderApp(createElement(Probe));
192
+
193
+ let first: unknown;
194
+ await act(async () => {
195
+ first = await createFolder?.("Taxes").then(
196
+ () => undefined,
197
+ (error: unknown) => error,
198
+ );
199
+ });
200
+ assert.ok(first instanceof Error);
201
+ assert.equal(first.message, MAILBOX_SYNC_FAILED_MESSAGE);
202
+
203
+ // The server confirms; the user presses "Create folder" again, same name.
204
+ status = MailboxSyncStatus.synced;
205
+ let result: FolderOption | undefined;
206
+ await act(async () => {
207
+ result = await createFolder?.("Taxes");
208
+ });
209
+ assert.equal(result?.label, "Taxes");
210
+
211
+ // Exactly one create across both attempts — the retry resumed, it did not
212
+ // re-validate (which would throw "already exists") or re-POST.
213
+ const posts = (http?.calls ?? []).filter((call) => call.method === "POST");
214
+ assert.equal(posts.length, 1);
215
+ });
216
+
217
+ it("abort stops the wait so a folder that confirms later never resolves", async () => {
218
+ mount([mailbox("INBOX", "/")], MailboxSyncStatus.pending);
219
+ const controller = new AbortController();
220
+ let caught: unknown;
221
+ await act(async () => {
222
+ const promise = createFolder?.("Taxes", controller.signal);
223
+ controller.abort();
224
+ caught = await promise?.then(
225
+ () => undefined,
226
+ (error: unknown) => error,
227
+ );
228
+ });
229
+ assert.equal((caught as { name?: string })?.name, "AbortError");
230
+ });
131
231
  });
@@ -5,21 +5,40 @@ import {
5
5
  } from "@remit/api-http-client/@tanstack/react-query.gen.ts";
6
6
  import type { FolderOption } from "@remit/ui";
7
7
  import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
8
- import { useCallback } from "react";
8
+ import { useCallback, useRef } from "react";
9
9
  import { getMailboxDisplayName } from "@/lib/folder-roles";
10
+ import { waitForMailboxSynced } from "@/lib/mailbox-sync-wait";
10
11
  import { composeFolderPath, validateNewFolderName } from "@/lib/new-folder";
11
12
 
12
13
  /**
13
14
  * Creates a mailbox for an account and refreshes the folder list on success.
14
15
  * The backend creates the row with a pending sync status and queues the IMAP
15
- * create, so the folder is usable as a move destination immediately.
16
+ * create.
16
17
  *
17
- * `createFolder` takes a leaf name, validates it against the account's current
18
- * folders with the same IMAP-aware rules the settings form uses (non-empty, no
19
- * hierarchy delimiter, no collision INBOX case-insensitive), and rejects with
20
- * the human-readable reason before any request. The kit surfaces that pick the
21
- * result render that rejection inline. `mutation` is exposed for callers that
22
- * drive their own form state and error surface.
18
+ * `createFolder` is the seam for dependent writes: a folder created so a filter
19
+ * can move mail into it, or so a move can land mail there. It takes a leaf name,
20
+ * validates it against the account's current folders with the same IMAP-aware
21
+ * rules the settings form uses (non-empty, no hierarchy delimiter, no collision
22
+ * INBOX case-insensitive), and rejects with the human-readable reason before any
23
+ * request. It then WAITS for the mail server to confirm the folder before
24
+ * resolving — a folder is not a valid target until it exists on the server, and
25
+ * binding a filter or a move to a still-pending row races the folder into
26
+ * existence and cannot report a create that fails. It resolves with the confirmed
27
+ * folder (carrying the path the server normalized to), rejects with a distinct
28
+ * message when the create fails or never confirms, and the kit surfaces that
29
+ * render either the "Creating folder…" wait or the failure inline.
30
+ *
31
+ * Retry is a resume, not a re-create: a create that timed out or failed leaves
32
+ * the row already made, so pressing "Create folder" again calls `createFolder`
33
+ * with the same name — which resumes the wait on the mailboxId it already made
34
+ * rather than re-validating (the pending row would collide as "already exists")
35
+ * and re-POSTing. The mailboxId is carried per-name until the folder confirms.
36
+ *
37
+ * `createFolder` takes an `AbortSignal` the surface aborts on unmount/cancel/
38
+ * close, so a folder that confirms after the surface is gone resolves nothing.
39
+ *
40
+ * `mutation` is exposed for callers that drive their own form state and want the
41
+ * optimistic, non-waiting create (the standalone settings create).
23
42
  */
24
43
  export function useCreateMailbox(accountId: string) {
25
44
  const queryClient = useQueryClient();
@@ -39,26 +58,48 @@ export function useCreateMailbox(accountId: string) {
39
58
  },
40
59
  });
41
60
 
61
+ // fullPath -> mailboxId for a folder created but not yet confirmed, so a retry
62
+ // resumes the wait on it instead of re-creating. Cleared once it confirms.
63
+ const pendingByPath = useRef(new Map<string, string>());
64
+
42
65
  const createFolder = useCallback(
43
- async (name: string): Promise<FolderOption> => {
44
- const items = data?.items ?? [];
45
- const delimiter = items[0]?.hierarchyDelimiter ?? "/";
46
- const problem = validateNewFolderName({
47
- name,
48
- delimiter,
49
- existingPaths: items.map((item) => item.fullPath),
50
- });
51
- if (problem) throw new Error(problem);
52
- const mailbox = await mutation.mutateAsync({
53
- path: { accountId },
54
- body: { fullPath: composeFolderPath(name), namespaceType: "personal" },
66
+ async (name: string, signal?: AbortSignal): Promise<FolderOption> => {
67
+ const fullPath = composeFolderPath(name);
68
+ let mailboxId = pendingByPath.current.get(fullPath);
69
+ if (!mailboxId) {
70
+ const items = data?.items ?? [];
71
+ const delimiter = items[0]?.hierarchyDelimiter ?? "/";
72
+ const problem = validateNewFolderName({
73
+ name,
74
+ delimiter,
75
+ existingPaths: items.map((item) => item.fullPath),
76
+ });
77
+ if (problem) throw new Error(problem);
78
+ const mailbox = await mutation.mutateAsync({
79
+ path: { accountId },
80
+ body: { fullPath, namespaceType: "personal" },
81
+ });
82
+ mailboxId = mailbox.mailboxId;
83
+ pendingByPath.current.set(fullPath, mailboxId);
84
+ }
85
+ const confirmed = await waitForMailboxSynced({
86
+ mailboxId,
87
+ signal,
88
+ fetchMailboxes: async () => {
89
+ const response = await queryClient.fetchQuery({
90
+ ...mailboxOperationsListMailboxesOptions({ path: { accountId } }),
91
+ staleTime: 0,
92
+ });
93
+ return response.items ?? [];
94
+ },
55
95
  });
96
+ pendingByPath.current.delete(fullPath);
56
97
  return {
57
- id: mailbox.mailboxId,
58
- label: getMailboxDisplayName(mailbox.fullPath),
98
+ id: confirmed.mailboxId,
99
+ label: getMailboxDisplayName(confirmed.fullPath),
59
100
  };
60
101
  },
61
- [mutation, accountId, data],
102
+ [mutation, accountId, data, queryClient],
62
103
  );
63
104
 
64
105
  return { createFolder, mutation };
@@ -0,0 +1,186 @@
1
+ /**
2
+ * waitForMailboxSynced — the gate a dependent write (a filter, a move) holds
3
+ * behind while a freshly-created folder is confirmed on the mail server. It
4
+ * resolves only on `synced`, rejects distinctly on `failed` and on timeout, and
5
+ * keeps polling while the row is still `pending` or not yet listed.
6
+ */
7
+
8
+ import assert from "node:assert/strict";
9
+ import { describe, it } from "node:test";
10
+ import { MailboxSyncStatus } from "@remit/domain-enums";
11
+ import {
12
+ MAILBOX_SYNC_FAILED_MESSAGE,
13
+ MAILBOX_SYNC_TIMEOUT_MESSAGE,
14
+ type MailboxSyncSignal,
15
+ waitForMailboxSynced,
16
+ } from "./mailbox-sync-wait.js";
17
+
18
+ const row = (
19
+ mailboxId: string,
20
+ syncStatus?: MailboxSyncSignal["syncStatus"],
21
+ extra: Record<string, unknown> = {},
22
+ ): MailboxSyncSignal & Record<string, unknown> => ({
23
+ mailboxId,
24
+ syncStatus,
25
+ ...extra,
26
+ });
27
+
28
+ const noDelay = () => Promise.resolve();
29
+
30
+ describe("waitForMailboxSynced", () => {
31
+ it("resolves with the confirmed row once it reaches synced", async () => {
32
+ const responses = [
33
+ [row("mbx-1", MailboxSyncStatus.pending)],
34
+ [row("mbx-1", MailboxSyncStatus.pending)],
35
+ [row("mbx-1", MailboxSyncStatus.synced, { fullPath: "Server/Receipts" })],
36
+ ];
37
+ let call = 0;
38
+ const result = await waitForMailboxSynced({
39
+ mailboxId: "mbx-1",
40
+ fetchMailboxes: async () => responses[call++],
41
+ delay: noDelay,
42
+ });
43
+ assert.equal(result.syncStatus, MailboxSyncStatus.synced);
44
+ assert.equal(
45
+ (result as Record<string, unknown>).fullPath,
46
+ "Server/Receipts",
47
+ );
48
+ assert.equal(call, 3);
49
+ });
50
+
51
+ it("keeps polling while the row is not yet listed", async () => {
52
+ const responses = [
53
+ [] as MailboxSyncSignal[],
54
+ [row("other", MailboxSyncStatus.synced)],
55
+ [row("mbx-1", MailboxSyncStatus.synced)],
56
+ ];
57
+ let call = 0;
58
+ const result = await waitForMailboxSynced({
59
+ mailboxId: "mbx-1",
60
+ fetchMailboxes: async () => responses[call++],
61
+ delay: noDelay,
62
+ });
63
+ assert.equal(result.mailboxId, "mbx-1");
64
+ assert.equal(call, 3);
65
+ });
66
+
67
+ it("rejects with the failure message when the create is reported failed", async () => {
68
+ await assert.rejects(
69
+ waitForMailboxSynced({
70
+ mailboxId: "mbx-1",
71
+ fetchMailboxes: async () => [row("mbx-1", MailboxSyncStatus.failed)],
72
+ delay: noDelay,
73
+ }),
74
+ (error: unknown) =>
75
+ error instanceof Error && error.message === MAILBOX_SYNC_FAILED_MESSAGE,
76
+ );
77
+ });
78
+
79
+ it("rejects with the timeout message when the row never confirms", async () => {
80
+ let clock = 0;
81
+ let fetches = 0;
82
+ await assert.rejects(
83
+ waitForMailboxSynced({
84
+ mailboxId: "mbx-1",
85
+ fetchMailboxes: async () => {
86
+ fetches += 1;
87
+ return [row("mbx-1", MailboxSyncStatus.pending)];
88
+ },
89
+ timeoutMs: 30_000,
90
+ pollIntervalMs: 1_000,
91
+ now: () => clock,
92
+ delay: async (ms) => {
93
+ clock += ms;
94
+ },
95
+ }),
96
+ (error: unknown) =>
97
+ error instanceof Error &&
98
+ error.message === MAILBOX_SYNC_TIMEOUT_MESSAGE,
99
+ );
100
+ assert.ok(fetches > 1, "polls more than once before timing out");
101
+ });
102
+
103
+ it("does not treat failed as timeout even past the deadline", async () => {
104
+ let clock = 100_000;
105
+ await assert.rejects(
106
+ waitForMailboxSynced({
107
+ mailboxId: "mbx-1",
108
+ fetchMailboxes: async () => [row("mbx-1", MailboxSyncStatus.failed)],
109
+ timeoutMs: 1,
110
+ now: () => clock++,
111
+ delay: noDelay,
112
+ }),
113
+ (error: unknown) =>
114
+ error instanceof Error && error.message === MAILBOX_SYNC_FAILED_MESSAGE,
115
+ );
116
+ });
117
+
118
+ const isAbort = (error: unknown): boolean =>
119
+ typeof error === "object" &&
120
+ error !== null &&
121
+ (error as { name?: unknown }).name === "AbortError";
122
+
123
+ it("rejects without polling when the signal is already aborted", async () => {
124
+ const controller = new AbortController();
125
+ controller.abort();
126
+ let fetches = 0;
127
+ await assert.rejects(
128
+ waitForMailboxSynced({
129
+ mailboxId: "mbx-1",
130
+ signal: controller.signal,
131
+ fetchMailboxes: async () => {
132
+ fetches += 1;
133
+ return [row("mbx-1", MailboxSyncStatus.pending)];
134
+ },
135
+ delay: noDelay,
136
+ }),
137
+ isAbort,
138
+ );
139
+ assert.equal(fetches, 0);
140
+ });
141
+
142
+ it("stops polling and rejects when the signal aborts mid-wait", async () => {
143
+ const controller = new AbortController();
144
+ let fetches = 0;
145
+ await assert.rejects(
146
+ waitForMailboxSynced({
147
+ mailboxId: "mbx-1",
148
+ signal: controller.signal,
149
+ fetchMailboxes: async () => {
150
+ fetches += 1;
151
+ if (fetches === 2) controller.abort();
152
+ return [row("mbx-1", MailboxSyncStatus.pending)];
153
+ },
154
+ delay: noDelay,
155
+ }),
156
+ isAbort,
157
+ );
158
+ assert.equal(fetches, 2);
159
+ });
160
+
161
+ it("resolves across the real timer delay between polls", async () => {
162
+ const responses = [
163
+ [row("mbx-1", MailboxSyncStatus.pending)],
164
+ [row("mbx-1", MailboxSyncStatus.synced)],
165
+ ];
166
+ let call = 0;
167
+ const result = await waitForMailboxSynced({
168
+ mailboxId: "mbx-1",
169
+ pollIntervalMs: 1,
170
+ fetchMailboxes: async () => responses[call++],
171
+ });
172
+ assert.equal(result.syncStatus, MailboxSyncStatus.synced);
173
+ });
174
+
175
+ it("aborts an in-progress real timer delay", async () => {
176
+ const controller = new AbortController();
177
+ const pending = waitForMailboxSynced({
178
+ mailboxId: "mbx-1",
179
+ signal: controller.signal,
180
+ pollIntervalMs: 10_000,
181
+ fetchMailboxes: async () => [row("mbx-1", MailboxSyncStatus.pending)],
182
+ });
183
+ setTimeout(() => controller.abort(), 5);
184
+ await assert.rejects(pending, isAbort);
185
+ });
186
+ });
@@ -0,0 +1,98 @@
1
+ import { MailboxSyncStatus } from "@remit/domain-enums";
2
+
3
+ type MailboxSyncStatusValue =
4
+ (typeof MailboxSyncStatus)[keyof typeof MailboxSyncStatus];
5
+
6
+ /**
7
+ * A folder created for a dependent write — a filter that will move mail into it,
8
+ * or a move that lands mail there — is not usable the instant the create is
9
+ * queued: the row exists locally with `syncStatus: pending`, and the folder does
10
+ * not exist on the mail server until the imap-worker confirms the create and
11
+ * flips it to `synced`. Binding the dependent write to a `pending` row races the
12
+ * folder into existence across separate FIFO queues and cannot report a create
13
+ * that fails. This waits for the confirmation before the dependent write runs.
14
+ *
15
+ * The standalone create (a folder made in settings with no dependent write) does
16
+ * not use this — it may stay optimistic. The wait is only for the dependent case.
17
+ *
18
+ * The wait honours an `AbortSignal`: the surface that started the create passes
19
+ * one and aborts it on unmount/cancel/close, so a folder that confirms after the
20
+ * surface is gone never resolves and never fires the dependent bind or move.
21
+ */
22
+
23
+ /** The read fields the wait needs off a mailbox row. */
24
+ export interface MailboxSyncSignal {
25
+ mailboxId: string;
26
+ syncStatus?: MailboxSyncStatusValue;
27
+ }
28
+
29
+ export interface WaitForMailboxSyncedOptions<T extends MailboxSyncSignal> {
30
+ /** Reads the current mailbox rows; called once per poll (forces a fresh read). */
31
+ fetchMailboxes: () => Promise<readonly T[]>;
32
+ /** The row to wait on. */
33
+ mailboxId: string;
34
+ /** Aborts the wait; a late confirmation after abort resolves nothing. */
35
+ signal?: AbortSignal;
36
+ /** How long to wait for confirmation before giving up. */
37
+ timeoutMs?: number;
38
+ /** Gap between polls. */
39
+ pollIntervalMs?: number;
40
+ /** Injectable clock/sleep for tests. */
41
+ delay?: (ms: number, signal?: AbortSignal) => Promise<void>;
42
+ now?: () => number;
43
+ }
44
+
45
+ export const MAILBOX_SYNC_TIMEOUT_MS = 30_000;
46
+ export const MAILBOX_SYNC_POLL_INTERVAL_MS = 1_000;
47
+
48
+ export const MAILBOX_SYNC_FAILED_MESSAGE =
49
+ "The folder couldn't be created on the mail server. Please try again.";
50
+ export const MAILBOX_SYNC_TIMEOUT_MESSAGE =
51
+ "The folder was created but the mail server hasn't confirmed it yet, so nothing was attached to it. It's in your folder list — try again in a moment.";
52
+
53
+ const defaultDelay = (ms: number, signal?: AbortSignal): Promise<void> =>
54
+ new Promise((resolve, reject) => {
55
+ if (signal?.aborted) {
56
+ reject(signal.reason);
57
+ return;
58
+ }
59
+ const timer = setTimeout(() => {
60
+ signal?.removeEventListener("abort", onAbort);
61
+ resolve();
62
+ }, ms);
63
+ const onAbort = () => {
64
+ clearTimeout(timer);
65
+ reject(signal?.reason);
66
+ };
67
+ signal?.addEventListener("abort", onAbort, { once: true });
68
+ });
69
+
70
+ /**
71
+ * Resolve with the mailbox row once its `syncStatus` reaches `synced` — the
72
+ * server-confirmed row, carrying the path the server normalized the create to.
73
+ * Reject with a failure message when the create is reported `failed`, with a
74
+ * distinct timeout message when it never confirms within `timeoutMs`, and with
75
+ * the signal's reason (an `AbortError`) when `signal` aborts. A row that is still
76
+ * `pending` (or not yet in the list) keeps the poll running.
77
+ */
78
+ export async function waitForMailboxSynced<T extends MailboxSyncSignal>({
79
+ fetchMailboxes,
80
+ mailboxId,
81
+ signal,
82
+ timeoutMs = MAILBOX_SYNC_TIMEOUT_MS,
83
+ pollIntervalMs = MAILBOX_SYNC_POLL_INTERVAL_MS,
84
+ delay = defaultDelay,
85
+ now = Date.now,
86
+ }: WaitForMailboxSyncedOptions<T>): Promise<T> {
87
+ const deadline = now() + timeoutMs;
88
+ for (;;) {
89
+ signal?.throwIfAborted();
90
+ const mailboxes = await fetchMailboxes();
91
+ const mailbox = mailboxes.find((entry) => entry.mailboxId === mailboxId);
92
+ if (mailbox?.syncStatus === MailboxSyncStatus.synced) return mailbox;
93
+ if (mailbox?.syncStatus === MailboxSyncStatus.failed)
94
+ throw new Error(MAILBOX_SYNC_FAILED_MESSAGE);
95
+ if (now() >= deadline) throw new Error(MAILBOX_SYNC_TIMEOUT_MESSAGE);
96
+ await delay(pollIntervalMs, signal);
97
+ }
98
+ }