From 11b88c67be6f80c50fe6408d046b541138d03e03 Mon Sep 17 00:00:00 2001 From: wkj <278599135@.com> Date: Mon, 20 May 2024 13:34:42 +0800 Subject: [PATCH] =?UTF-8?q?native=20=E6=8D=95=E8=8E=B7=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/native/configuration/index.ts | 18 -------- template/native/index.tsx | 11 +++-- template/native/promiseTracker/index.ts | 46 ++++++++++++++++++++ template/wechatMp/src/configuration/index.ts | 9 +--- 4 files changed, 55 insertions(+), 29 deletions(-) delete mode 100644 template/native/configuration/index.ts create mode 100644 template/native/promiseTracker/index.ts diff --git a/template/native/configuration/index.ts b/template/native/configuration/index.ts deleted file mode 100644 index 0a26063..0000000 --- a/template/native/configuration/index.ts +++ /dev/null @@ -1,18 +0,0 @@ - -/** - * 初始化,App也必须输入访问的目标域名,系统根据domain和system的关系来判定appId - */ -const env = process.env.NODE_ENV; - -const URL = { - // 服务器地址数组,和application的domain中要保持一致以确定application - development: 'localhost', - staging: 'test.com', - production: 'test.com', -}; - -const host = URL[env]; - -export { - host, -}; \ No newline at end of file diff --git a/template/native/index.tsx b/template/native/index.tsx index 6fe431b..4784651 100644 --- a/template/native/index.tsx +++ b/template/native/index.tsx @@ -8,6 +8,7 @@ import { setJSExceptionHandler } from 'react-native-exception-handler'; import features from './initialize'; import App from './App'; import { name as appName } from './app.json'; +import {setPromiseUnCatchHandler} from './promiseTracker'; import { handler as exceptionHandler } from '@project/exceptionHandler'; import { @@ -15,9 +16,13 @@ import { } from 'oak-frontend-base/es/platforms/native/features'; // to 详化 -setJSExceptionHandler((exception) => { - exceptionHandler(exception, features); -}); +setJSExceptionHandler(exception => { + exceptionHandler(exception, features); +}, true); + +setPromiseUnCatchHandler((id, exception) => { + exceptionHandler(exception, features); +}, true); function Root() { features.navigator.setNamespace('/frontend'); diff --git a/template/native/promiseTracker/index.ts b/template/native/promiseTracker/index.ts new file mode 100644 index 0000000..db65e29 --- /dev/null +++ b/template/native/promiseTracker/index.ts @@ -0,0 +1,46 @@ +export type PromiseUnCatchHandler = (id: string, error: Error) => void; + +const noop: PromiseUnCatchHandler = () => { }; + +/** + * + * @param {Function} customHandler + * @param {Boolean} allowedInDevMode + * @returns + */ +export const setPromiseUnCatchHandler = (customHandler: PromiseUnCatchHandler = noop, allowedInDevMode = false) => { + if (typeof customHandler !== 'function' || typeof allowedInDevMode !== 'boolean') { + console.log('setPromiseUnCatchHandler is called with wrong argument types.. first argument should be callback function and second argument is optional should be a boolean'); + console.log('Not setting the JS handler .. please fix setPromiseUnCatchHandler call'); + return; + } + const customRejectionTrackingOptions = { + allRejections: true, + onUnhandled: customHandler, + onHandled: (id: string) => { + const warning = + `Promise Rejection Handled (id: ${id})\n` + + 'This means you can ignore any previous messages of the form ' + + `"Possible Unhandled Promise Rejection (id: ${id}):"`; + console.warn(warning); + }, + }; + // @ts-ignore + const __DEV__ = process.env.NODE_ENV === 'development'; + const allowed = allowedInDevMode ? true : !__DEV__; + if (allowed) { + // @ts-ignore + if (global?.HermesInternal?.hasPromise?.()) { + // @ts-ignore + global.HermesInternal?.enablePromiseRejectionTracker?.( + customRejectionTrackingOptions, + ); + } else { + require('promise/setimmediate/rejection-tracking').enable( + customRejectionTrackingOptions, + ); + } + } else { + console.log('Skipping setPromiseUnCatchHandler: Reason: In DEV mode and allowedInDevMode = false'); + } +}; \ No newline at end of file diff --git a/template/wechatMp/src/configuration/index.ts b/template/wechatMp/src/configuration/index.ts index aad5856..9988274 100644 --- a/template/wechatMp/src/configuration/index.ts +++ b/template/wechatMp/src/configuration/index.ts @@ -5,16 +5,9 @@ const accountInfo = wx.getAccountInfoSync(); const envVersion = accountInfo.miniProgram.envVersion; //develop\trial\release -const URL = { // 服务器地址数组,和domain中要保持一致以界定application - develop: 'localhost', - trial: 'dev.oak-app-id.com', - release: 'www.oak-app-id.com', -}; - -const host = URL[envVersion]; const sdkVersion = '2.25.1'; // 小程序运行所需要的sdk最低版本 export { - host, + envVersion, sdkVersion, }; \ No newline at end of file