-
Notifications
You must be signed in to change notification settings - Fork 7
fix: Prevent race condition on updateContext #84
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e9d776
fix: Prevent race condition on updateContext
fabriziodemaria f2a077a
feat: Performance in bursts of context updates (second attempt) (#86)
fabriziodemaria 3493d21
fix: streamline all possible operations
fabriziodemaria 5223c98
feat: Add clearProviderAndWait and fix tests
fabriziodemaria 3f49bbe
docs: Add docs about the ProviderOperationsQueue
fabriziodemaria 97925df
fix: Dont send ready if no provider is set
fabriziodemaria File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import Foundation | ||
|
|
||
| /// Unified serial async task queue with operation-type-aware last-wins semantics. | ||
| /// - Non-last-wins operations always execute in order | ||
| /// - Consecutive last-wins operations: only the last one executes | ||
| /// - Order is always preserved | ||
| internal actor AsyncProviderOperationsQueue { | ||
| private var currentTask: Task<Void, Never>? | ||
|
|
||
| private struct QueuedOperation { | ||
| let operation: () async -> Void | ||
| let continuation: CheckedContinuation<Void, Never> | ||
| let lastWins: Bool | ||
| } | ||
|
|
||
| private var queue: [QueuedOperation] = [] | ||
|
|
||
| /// Runs the given operation serially. | ||
| /// - If lastWins is false: operation always executes | ||
| /// - If lastWins is true: may be skipped if superseded by a later last-wins operation | ||
| func run(lastWins: Bool, operation: @Sendable @escaping () async -> Void) async { | ||
| await withCheckedContinuation { continuation in | ||
| queue.append(QueuedOperation(operation: operation, continuation: continuation, lastWins: lastWins)) | ||
|
|
||
| if currentTask == nil { | ||
| processNext() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func processNext() { | ||
| guard !queue.isEmpty else { | ||
| currentTask = nil | ||
| return | ||
| } | ||
|
|
||
| // Find the next batch to execute | ||
| // A batch is either: | ||
| // 1. A single non-last-wins operation, OR | ||
| // 2. Consecutive last-wins operations (we execute only the last one) | ||
|
|
||
| let firstOp = queue[0] | ||
|
|
||
| if !firstOp.lastWins { | ||
| // Non-last-wins operation: execute it immediately | ||
| let op = queue.removeFirst() | ||
| currentTask = Task { [weak self] in | ||
| await op.operation() | ||
| op.continuation.resume() | ||
| await self?.processNext() | ||
| } | ||
| } else { | ||
| // Last-wins operation: find all consecutive last-wins ops | ||
| var lastWinsCount = 0 | ||
| for op in queue { | ||
| if op.lastWins { | ||
| lastWinsCount += 1 | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| // Execute only the last one in the last-wins batch | ||
| let toSkip = Array(queue.prefix(lastWinsCount - 1)) | ||
| let toExecute = queue[lastWinsCount - 1] | ||
| queue.removeFirst(lastWinsCount) | ||
|
|
||
| currentTask = Task { [weak self] in | ||
| await toExecute.operation() | ||
|
|
||
| // Resume all continuations (both skipped and executed) | ||
| for op in toSkip { | ||
| op.continuation.resume() | ||
| } | ||
| toExecute.continuation.resume() | ||
|
|
||
| await self?.processNext() | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.