Skip to content

Commit

Permalink
refactor: Simplify context object
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Dec 4, 2024
1 parent ef5343d commit fcae169
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ public class Confidence: ConfidenceEventSender {
private var region: ConfidenceRegion
private let parent: ConfidenceContextProvider?
private let eventSenderEngine: EventSenderEngine
private let contextSubject = CurrentValueSubject<ConfidenceStruct, Never>([:])
private var context: ConfidenceStruct = [:]
private var removedContextKeys: Set<String> = 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
Expand All @@ -22,7 +21,6 @@ public class Confidence: ConfidenceEventSender {

// Internal for testing
internal let remoteFlagResolver: ConfidenceResolveClient
internal let contextReconciliatedChanges = PassthroughSubject<String, Never>()

public static let sdkId: String = "SDK_ID_SWIFT_CONFIDENCE"

Expand All @@ -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
Expand Down Expand Up @@ -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<ConfidenceStruct, Never> {
return contextSubject
.dropFirst()
.removeDuplicates()
.eraseToAnyPublisher()
}

public func track(eventName: String, data: ConfidenceStruct) throws {
try eventSenderEngine.emit(
eventName: eventName,
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}
}

Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand Down

0 comments on commit fcae169

Please sign in to comment.