-
Notifications
You must be signed in to change notification settings - Fork 3
Add DittoSyncStatusHelper tool #162
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
Draft
bplattenburg
wants to merge
14
commits into
main
Choose a base branch
from
BP/sync_status
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b0585e7
Rough draft of sync status helper tool
bplattenburg ec79ffb
Add handler
bplattenburg c2d5adb
Naming is hard
bplattenburg 6519c27
Poll for updates
bplattenburg d7378ba
doc comments
bplattenburg d7cfc8b
Fix naming to align
bplattenburg 65a50b8
Schedule timer on update instead of polling repeatedly
bplattenburg 40fec1f
Always send an initial handler callback for the default .idle status
bplattenburg ff956e4
Update default to 1s
bplattenburg bee0cba
Cleanup
bplattenburg 4a64224
Make sure to use arguments from the subscription in the live query
bplattenburg 0ea744f
Doc comment cleanup, updated init paths to allow users to set this up…
bplattenburg fed929a
Fix comment whitespace
bplattenburg d75a90d
Make status publish via Combine, cleanup duplicated logic, more doc c…
bplattenburg 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
87 changes: 87 additions & 0 deletions
87
Sources/DittoSyncStatusHelper/DittoSubscriptionsStatusHelper.swift
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,87 @@ | ||
// | ||
// DittoSyncStatusHelper.swift | ||
// DittoSwiftTools | ||
// | ||
// Created by Brian Plattenburg on 11/16/24. | ||
// | ||
|
||
import Combine | ||
import DittoSwift | ||
|
||
public typealias DittoSyncSubscriptionStatusHandler = (_ result: DittoSyncSubscriptionsStatus) -> Void | ||
|
||
/// A status that describes whether a set of `DittoSyncSubscription`s is syncing or idle. | ||
/// This can be combined with an online / offline check to provide an approximation of whether this subscription is up to date | ||
public enum DittoSyncSubscriptionsStatus: String { | ||
case idle | ||
case syncing | ||
} | ||
|
||
/** | ||
A helper which provide the sync status of a set of DittoSyncSubscriptions, either idle or syncing. | ||
This tells you if this peer is actively receiving data about this subscription from connected peers or idling | ||
It can be used to provide an approximation of whether this peer is up to date with other connected peers. | ||
It works by creating local store observers for each passed in subscription, then tracking when they fire and comparing against the `idleTimeoutInterval` | ||
*/ | ||
public class DittoSubscriptionsStatusHelper { | ||
/// The interval after which a subscription is considered to be idle. Defaults to 1 second. | ||
public var idleTimeoutInterval: TimeInterval = 1 | ||
|
||
/// The current status for the total set of subscriptions monitored by this helper. This is both `@Published` | ||
/// fired to `handler` via `didSet` when the value changes. | ||
@Published public private(set) var status: DittoSyncSubscriptionsStatus = .idle { | ||
didSet { | ||
guard oldValue != status else { return } | ||
handler?(status) | ||
} | ||
} | ||
|
||
private let subscriptions: Set<DittoSyncSubscription> | ||
private let handler: DittoSyncSubscriptionStatusHandler? | ||
|
||
private var timer: Timer? = nil | ||
private var observers: [DittoStoreObserver] = [] | ||
private var lastUpdated: Date = .distantPast | ||
|
||
/** | ||
Creates a new `DittoSyncStatusHelper` for a given set of `DittoSyncSubscription`s | ||
- Parameters: | ||
- ditto: A Ditto instance for which sync status is being checked. Used internally to create `DittoStoreObserver`s tracking each query. | ||
- subscriptions: Which subscriptions to include for this status helper. The aggregate status for all of them will be tracked here, such that it is only considered `idle` if all subscriptions are `idle`. | ||
- handler: An closure called each time the `status` changes. Defaults to `nil` | ||
*/ | ||
init(ditto: Ditto, subscriptions: Set<DittoSyncSubscription>, handler: DittoSyncSubscriptionStatusHandler? = nil) throws { | ||
self.subscriptions = subscriptions | ||
self.handler = handler | ||
handler?(.idle) | ||
self.observers = try subscriptions.map { subscription in | ||
try ditto.store.registerObserver(query: subscription.queryString, arguments: subscription.queryArguments, handler: handleObserver) | ||
} | ||
} | ||
|
||
/** | ||
Creates a new `DittoSyncStatusHelper` for all of the currently active subscriptions on this Ditto instance *at the time this is created*. It will not update if those subscriptions change | ||
- Parameters: | ||
- ditto: A Ditto instance for which sync status is being checked. Used internally to create `DittoStoreObserver`s tracking each query. | ||
- handler: A closure called each time the `status` changes. | ||
*/ | ||
convenience init(ditto: Ditto, handler: DittoSyncSubscriptionStatusHandler?) throws { | ||
try self.init(ditto: ditto, subscriptions: ditto.sync.subscriptions, handler: handler) | ||
} | ||
|
||
deinit { | ||
timer?.invalidate() | ||
observers.forEach { observer in | ||
observer.cancel() | ||
} | ||
} | ||
|
||
private func handleObserver(_ result: DittoSwift.DittoQueryResult) { | ||
status = .syncing | ||
lastUpdated = Date() | ||
timer?.invalidate() | ||
timer = Timer.scheduledTimer(withTimeInterval: idleTimeoutInterval, repeats: false, block: { [weak self] _ in | ||
self?.status = .idle | ||
}) | ||
} | ||
} |
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.
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.
Maybe annotate this to indicate the other way to get status updates.