diff --git a/Sources/Confidence/Confidence.swift b/Sources/Confidence/Confidence.swift index d60b762c..4598e2c9 100644 --- a/Sources/Confidence/Confidence.swift +++ b/Sources/Confidence/Confidence.swift @@ -9,9 +9,8 @@ public class Confidence: ConfidenceEventSender { private var region: ConfidenceRegion private let parent: ConfidenceContextProvider? private let eventSenderEngine: EventSenderEngine - private let contextSubject = CurrentValueSubject([:]) + private var context: ConfidenceStruct = [:] private var removedContextKeys: Set = Set() - private let contextSubjectQueue = DispatchQueue(label: "com.confidence.queue.contextsubject") private let cacheQueue = DispatchQueue(label: "com.confidence.queue.cache") private let flagApplier: FlagApplier private var cache = FlagResolution.EMPTY @@ -22,7 +21,6 @@ public class Confidence: ConfidenceEventSender { // Internal for testing internal let remoteFlagResolver: ConfidenceResolveClient - internal let contextReconciliatedChanges = PassthroughSubject() public static let sdkId: String = "SDK_ID_SWIFT_CONFIDENCE" @@ -42,7 +40,7 @@ public class Confidence: ConfidenceEventSender { self.clientSecret = clientSecret self.region = region self.storage = storage - self.contextSubject.value = context + self.context = context self.parent = parent self.storage = storage self.flagApplier = flagApplier @@ -152,16 +150,6 @@ public class Confidence: ConfidenceEventSender { return storage.isEmpty() } - /** - Listen to changes in the context that is local to this Confidence instance. - */ - public func contextChanges() -> AnyPublisher { - return contextSubject - .dropFirst() - .removeDuplicates() - .eraseToAnyPublisher() - } - public func track(eventName: String, data: ConfidenceStruct) throws { try eventSenderEngine.emit( eventName: eventName, @@ -211,7 +199,7 @@ public class Confidence: ConfidenceEventSender { var reconciledCtx = parentContext.filter { !removedContextKeys.contains($0.key) } - self.contextSubject.value.forEach { entry in + self.context.forEach { entry in reconciledCtx.updateValue(entry.value, forKey: entry.key) } return reconciledCtx @@ -222,12 +210,12 @@ public class Confidence: ConfidenceEventSender { guard let self = self else { return } - var map = contextSubject.value + var map = self.context map[key] = value - contextSubject.value = map + context = map do { try await self.fetchAndActivate() - debugLogger?.logContext(action: "PutContext", context: contextSubject.value) + debugLogger?.logContext(action: "PutContext", context: context) } catch { debugLogger?.logMessage(message: "Error when putting context: \(error)", isWarning: true) } @@ -236,22 +224,21 @@ public class Confidence: ConfidenceEventSender { public func putContextLocal(context: ConfidenceStruct, removeKeys removedKeys: [String] = []) { - // Maybe use the semaphore instead withSemaphore { [weak self] in guard let self = self else { return } - var map = contextSubject.value + var map = self.context for removedKey in removedKeys { map.removeValue(forKey: removedKey) } for entry in context { map.updateValue(entry.value, forKey: entry.key) } - contextSubject.value = map + self.context = map debugLogger?.logContext( action: "PutContextLocal", - context: contextSubject.value) + context: context) } } @@ -260,16 +247,16 @@ public class Confidence: ConfidenceEventSender { guard let self = self else { return } - var map = contextSubject.value + var map = self.context for entry in context { map.updateValue(entry.value, forKey: entry.key) } - contextSubject.value = map + self.context = map do { try await fetchAndActivate() debugLogger?.logContext( action: "PutContext - Done with FetchAndActivate", - context: contextSubject.value) + context: context) } catch { debugLogger?.logMessage( message: "Error when putting context: \(error)", @@ -283,19 +270,19 @@ public class Confidence: ConfidenceEventSender { guard let self = self else { return } - var map = contextSubject.value + var map = self.context for removedKey in removedKeys { map.removeValue(forKey: removedKey) } for entry in context { map.updateValue(entry.value, forKey: entry.key) } - contextSubject.value = map + self.context = map do { try await self.fetchAndActivate() debugLogger?.logContext( action: "PutContext - Done with FetchAndActivate", - context: contextSubject.value) + context: context) } catch { debugLogger?.logMessage( message: "Error when putting context: \(error)", @@ -309,15 +296,15 @@ public class Confidence: ConfidenceEventSender { guard let self = self else { return } - var map = contextSubject.value + var map = self.context map.removeValue(forKey: key) - contextSubject.value = map + context = map removedContextKeys.insert(key) do { try await self.fetchAndActivate() debugLogger?.logContext( action: "RemoveContext - Done with FetchAndActivate", - context: contextSubject.value) + context: context) } catch { debugLogger?.logMessage( message: "Error when removing context key: \(error)",