Repository navigation
Make adding and removing projects explicit across devices #295
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
9 commits
Select commit
Hold shift + click to select a range
785e4c2
feat(projects): make adding and removing projects explicit across dev…
2witstudios ae3f7d7
Merge origin/main into pu/deleted-projects
2witstudios c4aae83
fix(projects): address review of explicit project membership
2witstudios c92d33e
docs(ipc): document killed sessions and the deferred registry prune
2witstudios d3ab290
fix(projects): address CodeRabbit and second review findings
2witstudios 03b5235
Merge remote-tracking branch 'origin/main' into pu/deleted-projects
2witstudios 2f1cc2a
refactor(projects): simplify registry membership after cleanup review
2witstudios d7c3e5e
fix(projects): keep unreadable projects and retry unresolved paths
2witstudios 7692d04
fix(projects): address CodeRabbit review of the latest commits
2witstudios 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
69 changes: 69 additions & 0 deletions
69
apps/purepoint-macos/purepoint-macos/Services/DaemonProjectsSubscription.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,69 @@ | ||
| import Foundation | ||
|
|
||
| /// Follows a daemon's registered projects (protocol 13). The daemon sends the full list of | ||
| /// roots when the stream opens and again after every add or remove, so a project added or | ||
| /// removed from any device, or with `pu projects`, shows up here without polling. | ||
| /// Modeled after DaemonStatusSubscription; unlike it, it never gives up while running: a | ||
| /// restarted daemon is simply subscribed to again. | ||
| actor DaemonProjectsSubscription { | ||
| private var connection: (any DaemonByteStream)? | ||
| private var stopped = false | ||
| private let client: DaemonClient | ||
|
|
||
| private static let initialBackoff: UInt64 = 500_000_000 // 0.5s | ||
|
|
||
| init(client: DaemonClient) { | ||
| self.client = client | ||
| } | ||
|
|
||
| /// Delivers each list of roots until `stop()`. Reconnects with backoff after a failure. | ||
| func start(onChange: @escaping @MainActor ([String]) -> Void) async { | ||
| var backoff = Self.initialBackoff | ||
| let maxBackoff: UInt64 = 10_000_000_000 // 10s | ||
| while !stopped { | ||
| do { | ||
| try await run(onChange: onChange) | ||
| backoff = Self.initialBackoff | ||
| } catch is CancellationError { | ||
| return | ||
| } catch { | ||
| #if os(iOS) | ||
| if await AppActivity.shared.isInBackground { | ||
| await AppActivity.shared.untilForeground() | ||
| backoff = Self.initialBackoff | ||
| continue | ||
| } | ||
| #endif | ||
| } | ||
| guard !stopped else { return } | ||
| do { try await Task.sleep(nanoseconds: backoff) } catch { return } | ||
| backoff = min(backoff * 2, maxBackoff) | ||
| } | ||
| } | ||
|
|
||
| func stop() { | ||
| stopped = true | ||
| connection?.close() | ||
| connection = nil | ||
| } | ||
|
|
||
| private func run(onChange: @escaping @MainActor ([String]) -> Void) async throws { | ||
| let (conn, reader) = try await client.connect() | ||
| // Stopped while connecting: nothing else would ever close this connection. | ||
| guard !stopped else { | ||
| conn.close() | ||
| return | ||
| } | ||
| connection?.close() | ||
| connection = conn | ||
| try await DaemonClient.write(.subscribeProjects, to: conn) | ||
| guard case .projectsSubscribed = DaemonClient.parse(try await reader.readLine()) else { | ||
| throw DaemonStatusError.unexpectedResponse | ||
| } | ||
| while !stopped { | ||
| if case .projectsChanged(let projects) = DaemonClient.parse(try await reader.readLine()) { | ||
| await onChange(projects) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.