Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: putContext doesn't trigger fetchAndActivate #178

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 15 additions & 53 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ 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 contextQueue = DispatchQueue(label: "com.confidence.queue.context")
private let cacheQueue = DispatchQueue(label: "com.confidence.queue.cache")
private let flagApplier: FlagApplier
private var cache = FlagResolution.EMPTY
Expand Down Expand Up @@ -41,7 +41,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 All @@ -50,26 +50,6 @@ public class Confidence: ConfidenceEventSender {
if let visitorId {
putContext(context: ["visitor_id": ConfidenceValue.init(string: visitorId)])
}

contextChanges().sink { [weak self] context in
guard let self = self else {
return
}
self.currentFetchTask?.cancel()
self.currentFetchTask = Task {
do {
let context = self.getContext()
try await self.fetchAndActivate()
self.contextReconciliatedChanges.send(context.hash())
} catch {
debugLogger?.logMessage(
message: "\(error)",
isWarning: true
)
}
}
}
.store(in: &cancellables)
}

/**
Expand Down Expand Up @@ -173,16 +153,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 @@ -232,53 +202,45 @@ public class Confidence: ConfidenceEventSender {
var reconciledCtx = parentContext.filter {
!removedContextKeys.contains($0.key)
}
self.contextSubject.value.forEach { entry in
reconciledCtx.updateValue(entry.value, forKey: entry.key)
self.context.forEach { (key: String, value: ConfidenceValue) in
reconciledCtx.updateValue(value, forKey: key)
}
return reconciledCtx
}

public func putContext(key: String, value: ConfidenceValue) {
withLock { confidence in
var map = confidence.contextSubject.value
map[key] = value
confidence.contextSubject.value = map
confidence.debugLogger?.logContext(action: "PutContext", context: confidence.contextSubject.value)
confidence.context[key] = value
confidence.debugLogger?.logContext(action: "PutContext", context: confidence.context)
}
}

public func putContext(context: ConfidenceStruct) {
withLock { confidence in
var map = confidence.contextSubject.value
for entry in context {
map.updateValue(entry.value, forKey: entry.key)
confidence.context[entry.key] = entry.value
}
confidence.contextSubject.value = map
confidence.debugLogger?.logContext(action: "PutContext", context: confidence.contextSubject.value)
confidence.debugLogger?.logContext(action: "PutContext", context: confidence.context)
}
}

public func putContext(context: ConfidenceStruct, removeKeys removedKeys: [String] = []) {
withLock { confidence in
var map = confidence.contextSubject.value
for removedKey in removedKeys {
map.removeValue(forKey: removedKey)
confidence.removedContextKeys.insert(removedKey)
}
for entry in context {
map.updateValue(entry.value, forKey: entry.key)
confidence.context[entry.key] = entry.value
}
confidence.contextSubject.value = map
confidence.debugLogger?.logContext(action: "PutContext", context: confidence.contextSubject.value)
confidence.debugLogger?.logContext(action: "PutContext", context: confidence.context)
}
}

public func removeKey(key: String) {
withLock { confidence in
var map = confidence.contextSubject.value
map.removeValue(forKey: key)
confidence.contextSubject.value = map
confidence.context.removeValue(forKey: key)
confidence.removedContextKeys.insert(key)
confidence.debugLogger?.logContext(action: "RemoveContext", context: confidence.contextSubject.value)
confidence.debugLogger?.logContext(action: "RemoveContext", context: confidence.context)
}
}

Expand All @@ -297,7 +259,7 @@ public class Confidence: ConfidenceEventSender {
}

private func withLock(callback: @escaping (Confidence) -> Void) {
contextSubjectQueue.sync { [weak self] in
contextQueue.sync { [weak self] in
guard let self = self else {
return
}
Expand Down
7 changes: 1 addition & 6 deletions Tests/ConfidenceTests/ConfidenceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,8 @@ class ConfidenceTest: XCTestCase {
resolveReason: .match)
]

let expectation = expectation(description: "context is synced")
let cancellable = confidence.contextReconciliatedChanges.sink { _ in
expectation.fulfill()
}
confidence.putContext(context: ["targeting_key": .init(string: "user2")])
await fulfillment(of: [expectation], timeout: 1)
cancellable.cancel()
try await confidence.fetchAndActivate()

let evaluation = confidence.getEvaluation(
key: "flag.size",
Expand Down
Loading