Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(mobile): 前台恢复被中断的媒体加载
DavidShenb84d6eb39244
diff --git a/apps/mobile/src/__tests__/mediaPlayerWebView.test.ts b/apps/mobile/src/__tests__/mediaPlayerWebView.test.tsindex 7e739a4bc2..7ff264afa4 100644--- a/apps/mobile/src/__tests__/mediaPlayerWebView.test.ts+++ b/apps/mobile/src/__tests__/mediaPlayerWebView.test.ts@@ -4,8 +4,26 @@ import {buildMediaPlayerWebViewHtml,parseMediaPlayerWebViewMessage,} from '@/session/mediaPlayerWebViewHtml';+import { createMediaPlayerWebViewLifecycle } from '@/session/mediaPlayerWebViewLifecycle';describe('mediaPlayerWebView', () => {+ it('reloads only when backgrounding interrupted an unfinished load', () => {+ const lifecycle = createMediaPlayerWebViewLifecycle();++ lifecycle.onBackground();+ lifecycle.onLoadEnd();+ expect(lifecycle.consumeReloadOnActive()).toBe(true);+ expect(lifecycle.consumeReloadOnActive()).toBe(false);++ lifecycle.onLoadEnd();+ lifecycle.onBackground();+ expect(lifecycle.consumeReloadOnActive()).toBe(false);++ lifecycle.onLoadStart();+ lifecycle.onBackground();+ expect(lifecycle.consumeReloadOnActive()).toBe(true);+ });+it('builds a video player document with controls and source metadata', () => {const html = buildMediaPlayerWebViewHtml({kind: 'video',diff --git a/apps/mobile/src/__tests__/messageWebViewGovernance.test.ts b/apps/mobile/src/__tests__/messageWebViewGovernance.test.tsindex 3e25064b50..bf00608c08 100644--- a/apps/mobile/src/__tests__/messageWebViewGovernance.test.ts+++ b/apps/mobile/src/__tests__/messageWebViewGovernance.test.ts@@ -69,6 +69,8 @@ describe('message WebView visibility wiring', () => {expect(mermaid).toContain('}, [active, source]);');expect(media).toContain('stopPlaybackAndLoading');expect(media).toContain('webViewRef.current?.stopLoading();');+ expect(media).toContain('lifecycleRef.current.consumeReloadOnActive()');+ expect(media).toContain('key={reloadGeneration}');expect(media).toContain('if (!mountedRef.current) return;');expect(composer).toContain('if (disposedRef.current) return;');});diff --git a/apps/mobile/src/session/mediaPlayerWebView.tsx b/apps/mobile/src/session/mediaPlayerWebView.tsxindex 995fc05232..a743ac6230 100644--- a/apps/mobile/src/session/mediaPlayerWebView.tsx+++ b/apps/mobile/src/session/mediaPlayerWebView.tsx@@ -1,4 +1,4 @@-import { useCallback, useEffect, useRef, type ComponentRef } from 'react';+import { useCallback, useEffect, useRef, useState, type ComponentRef } from 'react';import { AppState, View, type StyleProp, type ViewStyle } from 'react-native';import { WebView, type WebViewMessageEvent } from 'react-native-webview';import {@@ -8,6 +8,7 @@ import {type MobileMediaPlayerKind,type MobileMediaPlayerStatus,} from '@/session/mediaPlayerWebViewHtml';+import { createMediaPlayerWebViewLifecycle } from '@/session/mediaPlayerWebViewLifecycle';import { registerMobileMessageWebView } from '@/session/mobileMessageWebViewMetrics';import { useTheme } from '@/theme';@@ -30,7 +31,9 @@ export function RemoteMediaPlayerWebView({}) {const { colors } = useTheme();const webViewRef = useRef<ComponentRef<typeof WebView>>(null);+ const lifecycleRef = useRef(createMediaPlayerWebViewLifecycle());const mountedRef = useRef(true);+ const [reloadGeneration, setReloadGeneration] = useState(0);useEffect(() => registerMobileMessageWebView('media'), []);const pausePlayback = useCallback(() => {webViewRef.current?.postMessage(buildMediaPlayerWebViewCommand('pause'));@@ -42,7 +45,14 @@ export function RemoteMediaPlayerWebView({useEffect(() => {const subscription = AppState.addEventListener('change', state => {- if (state !== 'active') stopPlaybackAndLoading();+ if (state !== 'active') {+ lifecycleRef.current.onBackground();+ stopPlaybackAndLoading();+ return;+ }+ if (lifecycleRef.current.consumeReloadOnActive()) {+ setReloadGeneration(generation => generation + 1);+ }});return () => {subscription.remove();@@ -54,9 +64,12 @@ export function RemoteMediaPlayerWebView({return stopPlaybackAndLoading;}, [kind, stopPlaybackAndLoading, url]);- useEffect(() => () => {- mountedRef.current = false;- stopPlaybackAndLoading();+ useEffect(() => {+ mountedRef.current = true;+ return () => {+ mountedRef.current = false;+ stopPlaybackAndLoading();+ };}, [stopPlaybackAndLoading]);const handleMessage = useCallback((event: WebViewMessageEvent) => {@@ -68,10 +81,13 @@ export function RemoteMediaPlayerWebView({return (<View style={style} testID={testID}><WebView+ key={reloadGeneration}ref={webViewRef}allowsInlineMediaPlaybackjavaScriptEnabledmediaPlaybackRequiresUserAction={false}+ onLoadEnd={lifecycleRef.current.onLoadEnd}+ onLoadStart={lifecycleRef.current.onLoadStart}onMessage={handleMessage}originWhitelist={['*']}scrollEnabled={false}diff --git a/apps/mobile/src/session/mediaPlayerWebViewLifecycle.ts b/apps/mobile/src/session/mediaPlayerWebViewLifecycle.tsnew file mode 100644index 0000000000..647e3fc2b1--- /dev/null+++ b/apps/mobile/src/session/mediaPlayerWebViewLifecycle.ts@@ -0,0 +1,22 @@+export function createMediaPlayerWebViewLifecycle() {+ let loading = true;+ let reloadOnActive = false;++ return {+ onLoadStart() {+ loading = true;+ },+ onLoadEnd() {+ loading = false;+ },+ onBackground() {+ reloadOnActive ||= loading;+ },+ consumeReloadOnActive() {+ if (!reloadOnActive) return false;+ reloadOnActive = false;+ loading = true;+ return true;+ },+ };+}