-
Notifications
You must be signed in to change notification settings - Fork 3
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!: Context APIs changes and documentation/onboarding #180
Changes from 1 commit
2b4dc68
b3c44bc
6c6a583
bc7473f
59441f0
be27423
58c06d7
534d2b9
a4e3dd1
d4c6980
0c38b46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,13 @@ public class Confidence: ConfidenceEventSender { | |
// Synchronization and task management resources | ||
private var cancellables = Set<AnyCancellable>() | ||
private let cacheQueue = DispatchQueue(label: "com.confidence.queue.cache") | ||
private var currentFetchTask: Task<(), Never>? | ||
private var currentFetchTask: Task<(), Never>? { | ||
didSet { | ||
if let oldTask = oldValue { | ||
oldTask.cancel() | ||
} | ||
} | ||
} | ||
|
||
// Internal for testing | ||
internal let remoteFlagResolver: ConfidenceResolveClient | ||
|
@@ -155,7 +161,6 @@ public class Confidence: ConfidenceEventSender { | |
} | ||
|
||
public func putContextAndWait(key: String, value: ConfidenceValue) async { | ||
self.currentFetchTask?.cancel() | ||
self.currentFetchTask = Task { | ||
let newContext = contextManager.updateContext(withValues: [key: value], removedKeys: []) | ||
do { | ||
|
@@ -169,7 +174,6 @@ public class Confidence: ConfidenceEventSender { | |
} | ||
|
||
public func putContextAndWait(context: ConfidenceStruct, removedKeys: [String] = []) async { | ||
self.currentFetchTask?.cancel() | ||
self.currentFetchTask = Task { | ||
let newContext = contextManager.updateContext(withValues: context, removedKeys: removedKeys) | ||
do { | ||
|
@@ -183,7 +187,6 @@ public class Confidence: ConfidenceEventSender { | |
} | ||
|
||
public func putContextAndWait(context: ConfidenceStruct) async { | ||
self.currentFetchTask?.cancel() | ||
self.currentFetchTask = Task { | ||
let newContext = contextManager.updateContext(withValues: context, removedKeys: []) | ||
do { | ||
|
@@ -201,7 +204,6 @@ public class Confidence: ConfidenceEventSender { | |
} | ||
|
||
public func removeContextAndWait(key: String) async { | ||
self.currentFetchTask?.cancel() | ||
self.currentFetchTask = Task { | ||
let newContext = contextManager.updateContext(withValues: [:], removedKeys: [key]) | ||
do { | ||
|
@@ -229,35 +231,30 @@ public class Confidence: ConfidenceEventSender { | |
} | ||
|
||
public func putContext(key: String, value: ConfidenceValue) { | ||
self.currentFetchTask?.cancel() | ||
self.currentFetchTask = Task { | ||
await putContextAndWait(key: key, value: value) | ||
} | ||
} | ||
|
||
public func putContext(context: ConfidenceStruct) { | ||
self.currentFetchTask?.cancel() | ||
self.currentFetchTask = Task { | ||
await putContextAndWait(context: context) | ||
} | ||
} | ||
|
||
public func putContext(context: ConfidenceStruct, removeKeys removedKeys: [String] = []) { | ||
self.currentFetchTask?.cancel() | ||
self.currentFetchTask = Task { | ||
await putContextAndWait(context: context, removedKeys: removedKeys) | ||
} | ||
} | ||
|
||
public func removeContext(key: String) { | ||
self.currentFetchTask?.cancel() | ||
self.currentFetchTask = Task { | ||
await removeContextAndWait(key: key) | ||
} | ||
} | ||
|
||
public func putContext(context: ConfidenceStruct, removedKeys: [String]) { | ||
self.currentFetchTask?.cancel() | ||
self.currentFetchTask = Task { | ||
let newContext = contextManager.updateContext(withValues: context, removedKeys: removedKeys) | ||
do { | ||
|
@@ -277,8 +274,22 @@ public class Confidence: ConfidenceEventSender { | |
Ensures all the already-started context changes prior to this function have been reconciliated | ||
*/ | ||
public func awaitReconciliation() async { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should keep the same name used in Kotlin, i.e. I'd be happy to create a pair of "renaming PRs" for both repos after this though, if we want to reach proper alignment (this would make sense especially now that the APIs between platforms are much more similar!) |
||
if let task = self.currentFetchTask { | ||
while let task = self.currentFetchTask { | ||
// If current task is cancelled, return | ||
if task.isCancelled { | ||
return | ||
} | ||
// Wait for result of current task | ||
await task.value | ||
// If current task gets cancelled, check again if a new task was set | ||
if task.isCancelled { | ||
continue | ||
} | ||
// If current task finished successfully | ||
// and the set task has not changed, we are done waiting | ||
if self.currentFetchTask == task { | ||
return | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice