Repository navigation
Add async streams and awaitable perform to SyncSession - #15
Merged
Merged
Conversation
An internal ID that a perform call will put on its operation. Every piece the operation is split into carries it, and the state stamps it on whatever takes a piece's place in the queue, so a retry a backend builds cannot lose it. The state can report whether any of a call's work is still queued and take that work out. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Streams for events, fetch completions, modify completions, halts and state, and perform for a fetch and for a modify. The Combine subjects are unchanged. A perform call owns its operation: when the call throws on a halt, a stop, a reset or cancellation, the part of the operation that has not been sent leaves the queue. A modify returns one response for all of its pieces. Subscribers and waiting calls live behind the session's lock and are told about each change inside update. A waiting call is woken only after the lock is released, because its cancellation handler takes the same lock while the runtime holds the task's own. The stored perform closure is renamed performWork, and CloudSyncSession's private perform function carryOut, now that the session has methods by that name. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…withdrawn work A fetch(_:) for a change token that a perform call already has queued is served by that one queued fetch. It now stays queued, without the call, when the call throws. Before, it left the queue with the call and the fetch the app asked for never ran. Taking a call's work out of the queue while the run loop is waiting to send or retry it now ends that wait, so the work behind it is not held up. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
SyncBackend's associated types now require Sendable, and the session's operations, responses, events and state conform. The compiler checks what the Unchecked wrapper used to promise, in the run loop as well as the async streams and perform. CKRecord is Sendable from iOS 17, macOS 14 and watchOS 10, so those are the package's new minimums. A modify operation's userInfo values are any Sendable. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The helper that waits for one CloudKit operation resumed its continuation with a value the compiler could not prove safe to hand over. Every value it carries is Sendable now that the backend's types are, so it says so. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds an async surface to
SyncSessioninSyncSessionCore, beside the Combine subjects. Nothing existing changes or goes away. CloudSyncSession and SupabaseSyncSession both inherit it.What is new
eventsand the two completion streams drop nothing from the moment they are made.haltsstarts with the current value.statesstarts with the current state and keeps only the newest unread one.performcall owns its operation. When it throws on a halt, a stop, a reset or task cancellation, the part of the operation that has not been sent leaves the queue. Work queued withmodify(_:)andfetch(_:)behaves as before.performfor a modify returns one response for all of its pieces. Aperformfor a fetch returns its own page.The README section "Async Streams and perform" has the full contract.
How it works
SessionObservers, behind the session's existing lock, and are told about each change insideupdate.workInFlightis cleared by the same change that records how the request ended, so a request ending and what follows from it are one step.Things a reviewer should know
performclosure is renamedperformWork(internal). The private free functionperforminCloudSyncSession.swiftis renamedcarryOut, because the new methods shadow it inside the subclass. A subclass elsewhere with a free function of that name needs the same rename.SyncBackend's associated types now requireSendable, and the operations, responses, events and state conform, so theUncheckedwrapper is deleted, from the run loop as well. This is the one change here that is not additive: a backend with a non-Sendablerecord type stops compiling,userInfovalues areany Sendable, and the package minimums rise to iOS 17, macOS 14 and watchOS 10, whereCKRecordbecomesSendable.performwaiting on that work would then wait for good. No backend in either library does this.Tests
SyncSessionCoreTests: each stream,performcompleting through split, retry and resend, andperformthrowing on each of halt, stop and reset, both queued and with its request in flight. No sleeps.swift test: 99 and 69 pass.swift test --sanitize=thread: clean.Foodnoms does not call any of this yet. Its clients move over in later stages of "Logistics off Combine".
🤖 Generated with Claude Code