Skip to content

Commit

Permalink
Memoize webview message emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
louiszawadzki committed Sep 14, 2023
1 parent e019150 commit 2766f66
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/core/ios/Sources/DdSdkImplementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class DdSdkImplementation: NSObject {
let mainDispatchQueue: DispatchQueueType
let RUMMonitorProvider: () -> RUMMonitorProtocol
let RUMMonitorInternalProvider: () -> RUMMonitorInternalProtocol?

var webviewMessageEmitter: InternalExtension<WebViewTracking>.AbstractMessageEmitter?

private let jsLongTaskThresholdInSeconds: TimeInterval = 0.1;

@objc
Expand Down Expand Up @@ -101,6 +102,8 @@ public class DdSdkImplementation: NSObject {
if sdkConfiguration.nativeCrashReportEnabled ?? false {
CrashReporting.enable(in: core)
}

self.webviewMessageEmitter = WebViewTracking._internal.messageEmitter(in: core)

overrideReactNativeTelemetry(rnConfiguration: sdkConfiguration, core: core)
}
Expand Down Expand Up @@ -149,9 +152,7 @@ public class DdSdkImplementation: NSObject {
@objc
public func consumeWebviewEvent(message: NSString, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
do{
// TODO: memoize message emitter once core is initialized
let messageEmitter = WebViewTracking._internal.messageEmitter(in: CoreRegistry.default)
try messageEmitter.send(body: message)
try self.webviewMessageEmitter?.send(body: message)
} catch {
Datadog._internal.telemetry.error(id: "datadog_react_native:\(error.localizedDescription)", message: "The message being sent was:\(message)" as String, kind: "WebViewEventBridgeError" as String, stack: String(describing: error) as String)
}
Expand Down
21 changes: 21 additions & 0 deletions packages/core/ios/Tests/DdSdkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,22 @@ internal class DdSdkTests: XCTestCase {

Datadog.internalFlushAndDeinitialize()
}

func testConsumeWebviewEventBeforeInitialization() throws {
XCTAssertNoThrow(try DdSdkImplementation().consumeWebviewEvent(message: "TestMessage", resolve: mockResolve, reject: mockReject))
}

func testConsumeWebviewEvent() throws {
let sdk = DdSdkImplementation()
let configuration: DdSdkConfiguration = .mockAny()
let core = MockDatadogCore()

sdk.enableFeatures(sdkConfiguration: configuration, core: core)

sdk.consumeWebviewEvent(message: "{\"eventType\":\"RUM\",\"event\":{\"blabla\":\"custom message\"}}", resolve: mockResolve, reject: mockReject)

XCTAssertNotNil(core.baggages["browser-rum-event"])
}
}

private final class MockJSRefreshRateMonitor: RefreshRateMonitor {
Expand Down Expand Up @@ -998,10 +1014,15 @@ internal class MockDatadogCore: DatadogCoreProtocol {
case .configuration(let configuration) = telemetry {
self.configuration = configuration
}

if case .baggage(let key, let baggage) = message {
self.baggages[key] = baggage
}
}

private(set) var configuration: ConfigurationTelemetry?
private(set) var features: [String: DatadogFeature] = [:]
private(set) var baggages: [String: Any] = [:]

func register<T>(feature: T) throws where T : DatadogFeature {
features[T.name] = feature
Expand Down

0 comments on commit 2766f66

Please sign in to comment.