From 4d2dc9ec0fc6ee954f96a313c9a6a8d1dc20c6a4 Mon Sep 17 00:00:00 2001 From: louiszawadzki Date: Wed, 25 Oct 2023 14:52:10 +0200 Subject: [PATCH 1/2] Move DdSdk spec in own file --- .../react-native-webview/src/NativeDdSdk.ts | 20 +++++++++++++++++++ packages/react-native-webview/src/index.tsx | 12 +---------- 2 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 packages/react-native-webview/src/NativeDdSdk.ts diff --git a/packages/react-native-webview/src/NativeDdSdk.ts b/packages/react-native-webview/src/NativeDdSdk.ts new file mode 100644 index 000000000..53ec26002 --- /dev/null +++ b/packages/react-native-webview/src/NativeDdSdk.ts @@ -0,0 +1,20 @@ +/* + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. + * This product includes software developed at Datadog (https://www.datadoghq.com/). + * Copyright 2016-Present Datadog, Inc. + */ + +import type { TurboModule } from 'react-native'; +import { TurboModuleRegistry } from 'react-native'; + +/** + * We have to redefine the spec for the Native SDK here to be able to use the new architecture. + * We don't declare it in a spec file so we don't end up with a duplicate definition of the native module. + */ +interface PartialNativeDdSdkSpec extends TurboModule { + consumeWebviewEvent(message: string): Promise; + telemetryError(message: string, stack: string, kind: string): Promise; +} +export const NativeDdSdk = TurboModuleRegistry.get( + 'DdSdk' +); diff --git a/packages/react-native-webview/src/index.tsx b/packages/react-native-webview/src/index.tsx index 26dd270b5..b99750ec0 100644 --- a/packages/react-native-webview/src/index.tsx +++ b/packages/react-native-webview/src/index.tsx @@ -5,10 +5,9 @@ */ import type { WebViewMessageEvent, WebViewProps } from 'react-native-webview'; import { WebView as RNWebView } from 'react-native-webview'; -import type { TurboModule } from 'react-native'; -import { TurboModuleRegistry } from 'react-native'; import React, { forwardRef, useCallback } from 'react'; +import { NativeDdSdk } from './NativeDdSdk'; import { DATADOG_MESSAGE_PREFIX, getInjectedJavaScriptBeforeContentLoaded @@ -50,12 +49,3 @@ const WebViewComponent = (props: Props, ref: React.Ref>) => { export const WebView = forwardRef(WebViewComponent); export default WebView; - -/** - * We have to redefine the spec for the Native SDK here to be able to use the new architecture. - * We don't declare it in a separate file so we don't end up with a duplicate definition of the native module. - */ -interface PartialNativeDdSdkSpec extends TurboModule { - consumeWebviewEvent(message: string): Promise; -} -const NativeDdSdk = TurboModuleRegistry.get('DdSdk'); From 316225156bc4e4bcab7519fa935abcfbef6bf39c Mon Sep 17 00:00:00 2001 From: louiszawadzki Date: Wed, 25 Oct 2023 14:52:28 +0200 Subject: [PATCH 2/2] Use turbomodules for telemetry --- .../src/__utils__/formatAllowedHosts.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/react-native-webview/src/__utils__/formatAllowedHosts.ts b/packages/react-native-webview/src/__utils__/formatAllowedHosts.ts index 9c7971400..25142ec89 100644 --- a/packages/react-native-webview/src/__utils__/formatAllowedHosts.ts +++ b/packages/react-native-webview/src/__utils__/formatAllowedHosts.ts @@ -3,14 +3,14 @@ * This product includes software developed at Datadog (https://www.datadoghq.com/). * Copyright 2016-Present Datadog, Inc. */ -import { NativeModules } from 'react-native'; +import { NativeDdSdk } from '../NativeDdSdk'; export function formatAllowedHosts(allowedHosts?: string[]): string { try { return `'${JSON.stringify(allowedHosts)}'`; } catch (e: any) { - if (NativeModules.DdSdk && NativeModules.DdSdk.telemetryError) { - NativeModules.DdSdk.telemetryError( + if (NativeDdSdk) { + NativeDdSdk.telemetryError( getErrorMessage(e), getErrorStackTrace(e), 'AllowedHostsError'