Skip to content

Commit

Permalink
RUMM-2334 reuse existing publisher pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
maxep committed Oct 10, 2022
1 parent 5ae5a25 commit fe152c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
32 changes: 12 additions & 20 deletions Sources/Datadog/DatadogCore/Context/DatadogContextProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,6 @@

import Foundation

/// Comply to `DatadogContextObserver` protocol if you want to listen for new
/// value of the Datadog Context.
internal protocol DatadogContextObserver {
/// Receives notification for new Context.
///
/// This method will be called each time the context changed in the observed
/// provider. There is no garantee on which thread this method will be invoked.
///
/// - Parameter context: The new context value.
///
func notify(context: DatadogContext)
}

/// Provides thread-safe access to Datadog Context.
///
/// The context can be accessed asynchronously for reads and writes.
Expand Down Expand Up @@ -53,7 +40,7 @@ internal final class DatadogContextProvider {
///
/// The value must be accessed from the `queue` only.
private var context: DatadogContext {
didSet { observers.forEach { $0.notify(context: context) } }
didSet { receivers.forEach { $0(context) } }
}

/// The queue used to synchronize the access to the `DatadogContext`.
Expand All @@ -65,8 +52,13 @@ internal final class DatadogContextProvider {
qos: .utility
)

private var observers: [DatadogContextObserver]
/// List of receivers to invoke when the context changes.
private var receivers: [ContextValueReceiver<DatadogContext>]

/// List of subscription of context values.
private var subscriptions: [ContextValueSubscription]

/// A reader for key-path values of the context.
private var reader: KeyPathContextValueReader<DatadogContext>

/// Creates a context provider to perform reads and writes on the
Expand All @@ -75,7 +67,7 @@ internal final class DatadogContextProvider {
/// - Parameter context: The inital context value.
init(context: DatadogContext) {
self.context = context
self.observers = []
self.receivers = []
self.subscriptions = []
self.reader = KeyPathContextValueReader()
}
Expand All @@ -92,11 +84,11 @@ internal final class DatadogContextProvider {
return context
}

/// Adds an observer to notify when the context changes.
/// Publishes context changes to tge given receiver.
///
/// - Parameter observer: The observer to add.
func add(observer: DatadogContextObserver) {
queue.async(flags: .barrier) { self.observers.append(observer) }
/// - Parameter receiver: The receiver closure.
func publish(to receiver: @escaping ContextValueReceiver<DatadogContext>) {
queue.async(flags: .barrier) { self.receivers.append(receiver) }
}

/// Reads to the `context` synchronously, by blocking the caller thread.
Expand Down
12 changes: 6 additions & 6 deletions Sources/Datadog/DatadogCore/DatadogCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ internal final class DatadogCore {
self.encryption = encryption
self.v1Context = v1Context
self.contextProvider = contextProvider

self.contextProvider.subscribe(\.userInfo, to: userInfoPublisher)

// forward any context change on the message-bus
self.contextProvider.publish(to: { [weak self] context in
self?.send(message: .context(context))
})
}

/// Sets current user information.
Expand Down Expand Up @@ -237,12 +243,6 @@ extension DatadogCore: DatadogV1CoreProtocol {
}
}

extension DatadogCore: DatadogContextObserver {
func notify(context: DatadogContext) {
send(message: .context(context))
}
}

/// A v1 Feature with an associated stroage.
internal protocol V1Feature {
/// The feature's storage.
Expand Down

0 comments on commit fe152c2

Please sign in to comment.