Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 复制验证码依次执行,连点时最后一次仍能证明归属并被清理

Dash5c7e826d38db
diff --git a/apps/mobile/src/__tests__/copiedCodeClipboard.test.ts b/apps/mobile/src/__tests__/copiedCodeClipboard.test.ts
index c4d5d86b85..a49882f6df 100644
--- a/apps/mobile/src/__tests__/copiedCodeClipboard.test.ts
+++ b/apps/mobile/src/__tests__/copiedCodeClipboard.test.ts
@@ -70,6 +70,17 @@ it("keeps a later iOS copy, including one that lands before the version read", a
expect(h.getString).not.toHaveBeenCalled();
});
+it("keeps the latest iOS copy clearable when the user taps copy twice quickly", async () => {
+ const [first, second] = await Promise.all([
+ copyCode("CODE-1"),
+ copyCode("CODE-1"),
+ ]);
+ expect(first.version).toBe("6");
+ expect(second.version).toBe("7");
+ await clearCopiedCode(second);
+ expect(h.text).toBe("");
+});
+
it("compares contents on Android, so returning from the browser still clears the code", async () => {
h.os = "android";
const copied = await copyCode("CODE-1");
diff --git a/apps/mobile/src/plugins/copiedCodeClipboard.ts b/apps/mobile/src/plugins/copiedCodeClipboard.ts
index a0f26c2875..9432ad4f28 100644
--- a/apps/mobile/src/plugins/copiedCodeClipboard.ts
+++ b/apps/mobile/src/plugins/copiedCodeClipboard.ts
@@ -12,8 +12,18 @@ const iosVersion = () =>
remotePresentation?.clipboardVersion?.().catch(() => undefined) ??
Promise.resolve(undefined);
+// Our own copies run one at a time, so their writes never fall between
+// another copy's version reads.
+let copying: Promise<unknown> = Promise.resolve();
+
/** Copy a device code and remember enough to clear it later only if it is still ours. */
-export async function copyCode(code: string): Promise<CopiedCode> {
+export function copyCode(code: string): Promise<CopiedCode> {
+ const result = copying.then(() => writeCode(code));
+ copying = result.catch(() => {});
+ return result;
+}
+
+async function writeCode(code: string): Promise<CopiedCode> {
if (Platform.OS !== "ios") {
await Clipboard.setStringAsync(code);
return { code };