接入原生壳生命周期事件
新增 app.lifecycle HostBridge 能力与 H5 订阅入口 Expo 壳通过 React Native AppState 注入真实前后台状态 Tauri 壳通过主窗口 focus 和 blur 注入真实激活状态 更新壳能力漂移检查、测试和架构文档
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
import { StatusBar } from 'expo-status-bar';
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { BackHandler, Linking, Platform, StyleSheet, View } from 'react-native';
|
||||
import {
|
||||
AppState,
|
||||
type AppStateStatus,
|
||||
BackHandler,
|
||||
Linking,
|
||||
Platform,
|
||||
StyleSheet,
|
||||
View,
|
||||
} from 'react-native';
|
||||
import type { WebViewMessageEvent } from 'react-native-webview';
|
||||
import { WebView } from 'react-native-webview';
|
||||
|
||||
@@ -10,6 +18,7 @@ import {
|
||||
resolveMobileHostCapabilities,
|
||||
} from './src/mobileHostBridge';
|
||||
import { buildMobileShellUrlFromDeepLink } from './src/mobileShellDeepLink';
|
||||
import { lifecyclePayloadFromAppState } from './src/mobileShellLifecycle';
|
||||
import {
|
||||
resolveMobileShellExternalUrl,
|
||||
shouldOpenInMobileShellWebView,
|
||||
@@ -97,6 +106,27 @@ export default function App() {
|
||||
return () => subscription.remove();
|
||||
}, [canGoBack]);
|
||||
|
||||
useEffect(() => {
|
||||
const sendLifecycleEvent = (state: AppStateStatus) => {
|
||||
webViewRef.current?.injectJavaScript(
|
||||
buildHostBridgeMessageScript({
|
||||
bridge: 'GenarrativeHostBridge',
|
||||
version: 1,
|
||||
event: 'app.lifecycle',
|
||||
payload: lifecyclePayloadFromAppState(state),
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const subscription = AppState.addEventListener(
|
||||
'change',
|
||||
sendLifecycleEvent,
|
||||
);
|
||||
sendLifecycleEvent(AppState.currentState);
|
||||
|
||||
return () => subscription.remove();
|
||||
}, []);
|
||||
|
||||
const handleMessage = (event: WebViewMessageEvent) => {
|
||||
void handleMobileHostBridgeMessage(event.nativeEvent.data, (response) => {
|
||||
webViewRef.current?.injectJavaScript(
|
||||
|
||||
Reference in New Issue
Block a user