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): iOS 上翻历史时立即启动预取

Dash4689b021f699
diff --git a/apps/mobile/src/__tests__/messageHistoryAutofillCallbacks.test.ts b/apps/mobile/src/__tests__/messageHistoryAutofillCallbacks.test.ts
index caa8fcd5d7..5b0c44ec18 100644
--- a/apps/mobile/src/__tests__/messageHistoryAutofillCallbacks.test.ts
+++ b/apps/mobile/src/__tests__/messageHistoryAutofillCallbacks.test.ts
@@ -10,7 +10,12 @@ const source = ts.createSourceFile('renderer.tsx', readFileSync(
), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
let callbackSource = '';
let progressKeySource = '';
+const pagingCallbacks = new Map<string, string>();
function visit(node: ts.Node) {
+ if (ts.isVariableDeclaration(node) && node.initializer && ts.isCallExpression(node.initializer)
+ && ['requestLoadEarlier', 'flushQueuedLoadEarlier', 'scheduleQueuedLoadEarlierFlush'].includes(node.name.getText(source))) {
+ pagingCallbacks.set(node.name.getText(source), node.initializer.arguments[0].getText(source));
+ }
if (ts.isVariableDeclaration(node) && node.name.getText(source) === 'attemptAutoLoadEarlier'
&& node.initializer && ts.isCallExpression(node.initializer)) {
callbackSource = node.initializer.arguments[0].getText(source);
@@ -98,3 +103,72 @@ describe('history autofill production callback', () => {
expect(bindings.initialHistoryAutofillRemainingRef.current).toBe(3);
});
});
+
+describe('history prefetch during a gesture', () => {
+ function pagingFixture(appOwnedAnchor: boolean) {
+ const frames: Array<() => void> = [];
+ const bindings = {
+ MOBILE_HISTORY_PREPEND_USES_APP_OWNED_ANCHOR: appOwnedAnchor,
+ onLoadEarlier: vi.fn(),
+ beginLoadEarlier: vi.fn(),
+ readingOlderRef: { current: false },
+ queuedLoadEarlierRef: { current: false },
+ queuedLoadEarlierFlushFrameRef: { current: null as number | null },
+ userScrollForOlderRef: { current: true },
+ nearBottomRef: { current: false },
+ setIsAwayFromBottom: vi.fn(),
+ isDraggingRef: { current: false },
+ isMomentumScrollingRef: { current: false },
+ historyTouchStartYRef: { current: null as number | null },
+ historyPrependNativeMvcpDisabledRef: { current: false },
+ setHistoryPrependNativeMvcpDisabled: vi.fn(),
+ requestAnimationFrame: (fn: () => void) => frames.push(fn),
+ };
+ const compiled = ts.transpileModule([...pagingCallbacks].map(([name, body]) =>
+ `const ${name} = ${body};`).join('\n'), {
+ compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS },
+ }).outputText;
+ const callbacks = new Function(...Object.keys(bindings), `${compiled}
+return { requestLoadEarlier, flushQueuedLoadEarlier };`)(...Object.values(bindings));
+ return { bindings, callbacks, frame: () => frames.shift()?.() };
+ }
+
+ it.each(['drag', 'momentum', 'touch'])('starts iOS prefetch without waiting for %s to end', (gesture) => {
+ const h = pagingFixture(false);
+ h.bindings.isDraggingRef.current = gesture === 'drag';
+ h.bindings.isMomentumScrollingRef.current = gesture === 'momentum';
+ h.bindings.historyTouchStartYRef.current = gesture === 'touch' ? 100 : null;
+ h.callbacks.requestLoadEarlier();
+ h.callbacks.requestLoadEarlier();
+ h.frame();
+ expect(h.bindings.beginLoadEarlier).toHaveBeenCalledTimes(1);
+ expect(h.bindings.setHistoryPrependNativeMvcpDisabled).not.toHaveBeenCalled();
+ h.callbacks.requestLoadEarlier();
+ h.frame();
+ expect(h.bindings.beginLoadEarlier).toHaveBeenCalledTimes(1);
+ });
+
+ it('keeps Android gesture and committed native-anchor handoff protection', () => {
+ const h = pagingFixture(true);
+ h.bindings.isDraggingRef.current = true;
+ h.callbacks.requestLoadEarlier();
+ h.frame();
+ expect(h.bindings.beginLoadEarlier).not.toHaveBeenCalled();
+ expect(h.bindings.setHistoryPrependNativeMvcpDisabled).not.toHaveBeenCalled();
+ h.bindings.isDraggingRef.current = false;
+ h.callbacks.flushQueuedLoadEarlier();
+ expect(h.bindings.setHistoryPrependNativeMvcpDisabled).toHaveBeenCalledWith(true);
+ expect(h.bindings.beginLoadEarlier).not.toHaveBeenCalled();
+ h.bindings.historyPrependNativeMvcpDisabledRef.current = true;
+ h.callbacks.flushQueuedLoadEarlier();
+ expect(h.bindings.beginLoadEarlier).toHaveBeenCalledTimes(1);
+ });
+
+ it('does not start a queued request invalidated before the next frame', () => {
+ const h = pagingFixture(false);
+ h.callbacks.requestLoadEarlier();
+ h.bindings.queuedLoadEarlierRef.current = false;
+ h.frame();
+ expect(h.bindings.beginLoadEarlier).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/mobile/src/__tests__/messageListVirtualization.test.ts b/apps/mobile/src/__tests__/messageListVirtualization.test.ts
index 002812488b..f8a62a4ba0 100644
--- a/apps/mobile/src/__tests__/messageListVirtualization.test.ts
+++ b/apps/mobile/src/__tests__/messageListVirtualization.test.ts
@@ -160,7 +160,7 @@ describe('mobile message list container', () => {
expect(maybeFinishSource).toContain('historyTouchStartYRef.current !== null');
expect(source).toContain('transaction.userHandoffPending = true;');
expect(source).toContain('currentTransaction.userHandoffPending = false;');
- // 直接拖动结束前不发请求,避免远端页在手指仍控制 ScrollView 时落地。
+ // Android 的手动锚定等手势结束;iOS 由 native MVCP 保持视口,可在手势中预取。
expect(source).toContain('queuedLoadEarlierRef.current = true');
expect(source).toContain('setHistoryPrependNativeMvcpDisabled(true)');
expect(source).toMatch(
diff --git a/apps/mobile/src/session/MessageRenderer.tsx b/apps/mobile/src/session/MessageRenderer.tsx
index 452617d24d..283e045dc9 100644
--- a/apps/mobile/src/session/MessageRenderer.tsx
+++ b/apps/mobile/src/session/MessageRenderer.tsx
@@ -1830,15 +1830,17 @@ export function MessageRenderer({
}, [getCurrentHistoryTopOffsetAdjustment, historyProgressKey, onLoadEarlier]);
const flushQueuedLoadEarlier = useCallback(() => {
- if (
- !queuedLoadEarlierRef.current
- || isDraggingRef.current
+ if (!queuedLoadEarlierRef.current) return;
+ // Native MVCP can prepend while iOS is scrolling. Only the app-owned Android anchor
+ // needs a quiet gesture boundary; delaying both platforms defeats near-start prefetch.
+ if (MOBILE_HISTORY_PREPEND_USES_APP_OWNED_ANCHOR && (
+ isDraggingRef.current
|| isMomentumScrollingRef.current
|| historyTouchStartYRef.current !== null
- ) return;
+ )) return;
// Android must not start until a committed render removes RN's native MVCP prop. Otherwise a
// fast local/relay response can prepend before that prop update lands. iOS keeps native MVCP
- // throughout and can start immediately once the user's gesture is no longer active.
+ // throughout and can start while the user's gesture is still active.
if (
MOBILE_HISTORY_PREPEND_USES_APP_OWNED_ANCHOR
&& !historyPrependNativeMvcpDisabledRef.current