@@ -121,6 +121,14 @@ let lastCredentialSignature = ""; |
121 | 121 | let lastOnscreenSignature = ""; |
122 | 122 | // The native index persists across launches, so assume it holds data until a clear succeeds. |
123 | 123 | let nativeMayHoldData = true; |
| 124 | +// Syncs and clears share one queue, so a write that started before a clear cannot land after it. |
| 125 | +let syncQueue: Promise<void> = Promise.resolve(); |
| 126 | + |
| 127 | +function enqueueAppleIntentsWork(work: () => Promise<void>): Promise<void> { |
| 128 | + const run = syncQueue.then(work); |
| 129 | + syncQueue = run.catch(() => undefined); |
| 130 | + return run; |
| 131 | +} |
124 | 132 |
|
125 | 133 | async function readDpopPrivateScalarBase64Url(): Promise<string | null> { |
126 | 134 | try { |
@@ -133,13 +141,69 @@ async function readDpopPrivateScalarBase64Url(): Promise<string | null> { |
133 | 141 | } |
134 | 142 | } |
135 | 143 |
|
136 | | -/** Clears the native index and credentials; the sync cache resets only once the clear succeeds. */ |
137 | | -export async function clearAppleIntentsIndex(): Promise<void> { |
138 | | - await clearAppleIntentsNativeIndex(); |
139 | | - lastSnapshot = null; |
140 | | - lastCredentialSignature = ""; |
141 | | - lastOnscreenSignature = ""; |
142 | | - nativeMayHoldData = false; |
| 144 | +async function clearNativeIndex(): Promise<void> { |
| 145 | + try { |
| 146 | + await clearAppleIntentsNativeIndex(); |
| 147 | + nativeMayHoldData = false; |
| 148 | + } finally { |
| 149 | + // A failed clear may have removed part of the index, so the next sync starts from scratch. |
| 150 | + lastSnapshot = null; |
| 151 | + lastCredentialSignature = ""; |
| 152 | + lastOnscreenSignature = ""; |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +/** Clears the native index and credentials after any sync already in flight. */ |
| 157 | +export function clearAppleIntentsIndex(): Promise<void> { |
| 158 | + return enqueueAppleIntentsWork(clearNativeIndex); |
| 159 | +} |
| 160 | + |
| 161 | +async function syncNativeIndex(source: Atom.Type<typeof appleIntentsSourceAtom>): Promise<void> { |
| 162 | + if (!source.enabled) { |
| 163 | + if (nativeMayHoldData) await clearNativeIndex(); |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + const nextSnapshot = buildAppleIntentsSnapshot(source.inputs, { |
| 168 | + indexMessagePreviews: source.indexMessagePreviews, |
| 169 | + nowMs: DateTime.toEpochMillis(DateTime.nowUnsafe()), |
| 170 | + resolve: m, |
| 171 | + }); |
| 172 | + const diff = diffAppleIntentsSnapshots(lastSnapshot, nextSnapshot); |
| 173 | + if (diff.hasChanges) { |
| 174 | + nativeMayHoldData = true; |
| 175 | + try { |
| 176 | + await syncAppleIntentsIndex(diff.delta); |
| 177 | + } catch (error) { |
| 178 | + // Native storage may have committed part of the delta, so resync everything next time. |
| 179 | + lastSnapshot = null; |
| 180 | + throw error; |
| 181 | + } |
| 182 | + lastSnapshot = nextSnapshot; |
| 183 | + } |
| 184 | + |
| 185 | + const onscreenSig = JSON.stringify(source.onscreen); |
| 186 | + if (onscreenSig !== lastOnscreenSignature) { |
| 187 | + setAppleIntentsOnscreenContext(source.onscreen); |
| 188 | + lastOnscreenSignature = onscreenSig; |
| 189 | + } |
| 190 | + |
| 191 | + const needsDpop = source.rawCredentials.some((item) => item.authScheme === "DPoP"); |
| 192 | + const dpopScalar = needsDpop ? await readDpopPrivateScalarBase64Url() : null; |
| 193 | + const credentials: AppleIntentAuthCredential[] = source.rawCredentials.map((item) => ({ |
| 194 | + environmentId: item.environmentId, |
| 195 | + httpBaseUrl: item.httpBaseUrl, |
| 196 | + accessToken: item.accessToken, |
| 197 | + authScheme: item.authScheme, |
| 198 | + relayManaged: item.relayManaged, |
| 199 | + ...(item.authScheme === "DPoP" && dpopScalar ? { dpopKeyRef: dpopScalar } : {}), |
| 200 | + })); |
| 201 | + const credSig = JSON.stringify(credentials); |
| 202 | + if (credSig !== lastCredentialSignature) { |
| 203 | + nativeMayHoldData = true; |
| 204 | + await syncAppleIntentsAuthCredentials(credentials); |
| 205 | + lastCredentialSignature = credSig; |
| 206 | + } |
143 | 207 | } |
144 | 208 |
|
145 | 209 | export function recordAppleIntentsNavigationState(state: NavigationState | undefined): void { |
@@ -154,56 +218,14 @@ export function installAppleIntents(): () => void { |
154 | 218 |
|
155 | 219 | let disposed = false; |
156 | 220 | let debounceTimer: ReturnType<typeof setTimeout> | null = null; |
157 | | - let syncQueue: Promise<void> = Promise.resolve(); |
158 | 221 |
|
159 | 222 | const flush = (source: Atom.Type<typeof appleIntentsSourceAtom>) => { |
160 | | - syncQueue = syncQueue |
161 | | - .then(async () => { |
162 | | - if (disposed || !source.preferencesReady) return; |
163 | | - |
164 | | - if (!source.enabled) { |
165 | | - if (nativeMayHoldData) await clearAppleIntentsIndex(); |
166 | | - return; |
167 | | - } |
168 | | - |
169 | | - const nextSnapshot = buildAppleIntentsSnapshot(source.inputs, { |
170 | | - indexMessagePreviews: source.indexMessagePreviews, |
171 | | - nowMs: DateTime.toEpochMillis(DateTime.nowUnsafe()), |
172 | | - resolve: m, |
173 | | - }); |
174 | | - const diff = diffAppleIntentsSnapshots(lastSnapshot, nextSnapshot); |
175 | | - if (diff.hasChanges) { |
176 | | - nativeMayHoldData = true; |
177 | | - await syncAppleIntentsIndex(diff.delta); |
178 | | - lastSnapshot = nextSnapshot; |
179 | | - } |
180 | | - |
181 | | - const onscreenSig = JSON.stringify(source.onscreen); |
182 | | - if (onscreenSig !== lastOnscreenSignature) { |
183 | | - setAppleIntentsOnscreenContext(source.onscreen); |
184 | | - lastOnscreenSignature = onscreenSig; |
185 | | - } |
186 | | - |
187 | | - const needsDpop = source.rawCredentials.some((item) => item.authScheme === "DPoP"); |
188 | | - const dpopScalar = needsDpop ? await readDpopPrivateScalarBase64Url() : null; |
189 | | - const credentials: AppleIntentAuthCredential[] = source.rawCredentials.map((item) => ({ |
190 | | - environmentId: item.environmentId, |
191 | | - httpBaseUrl: item.httpBaseUrl, |
192 | | - accessToken: item.accessToken, |
193 | | - authScheme: item.authScheme, |
194 | | - relayManaged: item.relayManaged, |
195 | | - ...(item.authScheme === "DPoP" && dpopScalar ? { dpopKeyRef: dpopScalar } : {}), |
196 | | - })); |
197 | | - const credSig = JSON.stringify(credentials); |
198 | | - if (credSig !== lastCredentialSignature) { |
199 | | - nativeMayHoldData = true; |
200 | | - await syncAppleIntentsAuthCredentials(credentials); |
201 | | - lastCredentialSignature = credSig; |
202 | | - } |
203 | | - }) |
204 | | - .catch((error: unknown) => { |
205 | | - console.warn("[apple-intents] sync failed", error); |
206 | | - }); |
| 223 | + void enqueueAppleIntentsWork(async () => { |
| 224 | + if (disposed || !source.preferencesReady) return; |
| 225 | + await syncNativeIndex(source); |
| 226 | + }).catch((error: unknown) => { |
| 227 | + console.warn("[apple-intents] sync failed", error); |
| 228 | + }); |
207 | 229 | }; |
208 | 230 |
|
209 | 231 | const unsubscribe = appAtomRegistry.subscribe( |
|