From d77f4bbc2cc1e260c79c003bf47e581cf5a4bfec Mon Sep 17 00:00:00 2001 From: louiszawadzki Date: Tue, 19 Sep 2023 17:18:12 +0200 Subject: [PATCH] Use backed fields for android proxies --- .../datadog/reactnative/DatadogSDKWrapper.kt | 53 ++++++++++--------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/packages/core/android/src/main/kotlin/com/datadog/reactnative/DatadogSDKWrapper.kt b/packages/core/android/src/main/kotlin/com/datadog/reactnative/DatadogSDKWrapper.kt index e2b5b89cc..aecae207e 100644 --- a/packages/core/android/src/main/kotlin/com/datadog/reactnative/DatadogSDKWrapper.kt +++ b/packages/core/android/src/main/kotlin/com/datadog/reactnative/DatadogSDKWrapper.kt @@ -8,6 +8,7 @@ package com.datadog.reactnative import android.content.Context import com.datadog.android.Datadog +import com.datadog.android._InternalProxy import com.datadog.android.core.configuration.Configuration import com.datadog.android.log.Logs import com.datadog.android.log.LogsConfiguration @@ -22,15 +23,31 @@ import com.datadog.android.webview.WebViewTracking internal class DatadogSDKWrapper : DatadogWrapper { - // lazy here is on purpose. The thing is that this class will be instantiated even before - // Sdk.initialize is called, but telemetry proxy can be created only after SDK is initialized. - private val telemetryProxy by lazy { Datadog._internalProxy() } + // We use Kotlin backing field here to initialize once the telemetry proxy + // and make sure it is only after SDK is initialized. + private var telemetryProxy: _InternalProxy._TelemetryProxy? = null + get() { + if (field == null) { + if (isInitialized()) { + field = Datadog._internalProxy()._telemetry + } + } + + return field + } - // lazy here is on purpose. The thing is that this class will be instantiated even before - // Sdk.initialize is called, but webview proxy can be created only after SDK is initialized. - private val webViewProxy by lazy { - WebViewTracking._InternalWebViewProxy(Datadog.getInstance()) - } + // We use Kotlin backing field here to initialize once the telemetry proxy + // and make sure it is only after SDK is initialized. + private var webViewProxy: WebViewTracking._InternalWebViewProxy? = null + get() { + if (field == null) { + if (isInitialized()) { + field = WebViewTracking._InternalWebViewProxy(Datadog.getInstance()) + } + } + + return field + } override fun setVerbosity(level: Int) { Datadog.setVerbosity(level) @@ -77,31 +94,19 @@ internal class DatadogSDKWrapper : DatadogWrapper { } override fun telemetryDebug(message: String) { - // Do not initialize the telemetry proxy before SDK is initialized - if (isInitialized()) { - telemetryProxy._telemetry.debug(message) - } + telemetryProxy?.debug(message) } override fun telemetryError(message: String, stack: String?, kind: String?) { - // Do not initialize the telemetry proxy before SDK is initialized - if (isInitialized()) { - telemetryProxy._telemetry.error(message, stack, kind) - } + telemetryProxy?.error(message, stack, kind) } override fun telemetryError(message: String, throwable: Throwable?) { - // Do not initialize the telemetry proxy before SDK is initialized - if (isInitialized()) { - telemetryProxy._telemetry.error(message, throwable) - } + telemetryProxy?.error(message, throwable) } override fun consumeWebviewEvent(message: String) { - // Do not initialize the webview proxy before SDK is initialized - if (isInitialized()) { - webViewProxy.consumeWebviewEvent(message) - } + webViewProxy?.consumeWebviewEvent(message) } override fun isInitialized(): Boolean {