Skip to content

Commit

Permalink
Threading fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ptsochantaris committed Feb 26, 2024
1 parent 15b3d76 commit 080df6e
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 40 deletions.
16 changes: 8 additions & 8 deletions Sources/trailer/API/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ enum Network {
networkGate.returnTicket()
}
#if canImport(FoundationNetworking)
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Data, Error>) in
_ = urlSession.dataTask(with: req) { data, _, error in
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: data ?? Data())
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Data, Error>) in
_ = urlSession.dataTask(with: req) { data, _, error in
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: data ?? Data())
}
}
}
}
#else
return try await urlSession.data(for: req).0
return try await urlSession.data(for: req).0
#endif
}
}
24 changes: 12 additions & 12 deletions Sources/trailer/Actions/Actions-Update.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ extension Actions {
Group("repositories", paging: .first(count: 100, paging: true)) { Repo.fragment }
Group("watching", paging: .first(count: 100, paging: true)) { Repo.fragment }
}
let repositoryListQuery = Query(name: "Repos", rootElement: root, perNode: parse)
let repositoryListQuery = Query(name: "Repos", rootElement: root, perNode: { await parse(output: $0) })
try await run(repositoryListQuery)
} else {
log(level: .info, "[*Repos*] (Skipped)")
Expand Down Expand Up @@ -244,7 +244,7 @@ extension Actions {
}

if userWantsPrs || userWantsIssues, !filtersRequested { // detect new items
let itemIdParser: Query.PerNodeBlock = { output in
func itemIdParser(output: ParseOutput) {
guard case let .node(node) = output,
let parent = node.parent, parent.elementType == "Repository" else {
return
Expand Down Expand Up @@ -285,7 +285,7 @@ extension Actions {
userWantsIssues ? [Repo.issueIdsFragment] :
[]
if !fields.isEmpty {
let queries = Query.batching("Item IDs", groupName: "nodes", idList: repoIds, maxCost: config.maxNodeCost, perNode: itemIdParser) { fields }
let queries = Query.batching("Item IDs", groupName: "nodes", idList: repoIds, maxCost: config.maxNodeCost, perNode: { await itemIdParser(output: $0) }) { fields }
try await run(queries)
}
} else {
Expand All @@ -307,7 +307,7 @@ extension Actions {
///////////////////////////////////////////////////////////////////////////////////////////////////////////

if !prIdList.isEmpty {
try await run(Query.batching("PRs", groupName: "nodes", idList: Array(prIdList.keys), maxCost: config.maxNodeCost, perNode: parse) {
try await run(Query.batching("PRs", groupName: "nodes", idList: Array(prIdList.keys), maxCost: config.maxNodeCost, perNode: { await parse(output: $0) }) {
userWantsComments ? PullRequest.fragmentWithComments : PullRequest.fragment
})

Expand Down Expand Up @@ -345,7 +345,7 @@ extension Actions {
let reviewIdsWithComments = Review.allItems.values.compactMap { $0.syncState == .none || !$0.syncNeedsComments ? nil : $0.id }

if !reviewIdsWithComments.isEmpty {
try await run(Query.batching("PR Review Comments", groupName: "nodes", idList: reviewIdsWithComments, maxCost: config.maxNodeCost, perNode: parse) {
try await run(Query.batching("PR Review Comments", groupName: "nodes", idList: reviewIdsWithComments, maxCost: config.maxNodeCost, perNode: { await parse(output: $0) }) {
Review.commentsFragment
PullRequest.commentsFragment
Issue.commentsFragment
Expand All @@ -367,7 +367,7 @@ extension Actions {
///////////////////////////////////////////////////////////////////////////////////////////////////////////

if !issueIdList.isEmpty {
try await run(Query.batching("Issues", groupName: "nodes", idList: Array(issueIdList.keys), maxCost: config.maxNodeCost, perNode: parse) {
try await run(Query.batching("Issues", groupName: "nodes", idList: Array(issueIdList.keys), maxCost: config.maxNodeCost, perNode: { await parse(output: $0) }) {
userWantsComments ? Issue.fragmentWithComments : Issue.fragment
})

Expand Down Expand Up @@ -440,7 +440,7 @@ extension Actions {
itemIdsWithReactions += Issue.allItems.keys
}

try await run(Query.batching("Reactions", groupName: "nodes", idList: itemIdsWithReactions, maxCost: config.maxNodeCost, perNode: parse) {
try await run(Query.batching("Reactions", groupName: "nodes", idList: itemIdsWithReactions, maxCost: config.maxNodeCost, perNode: { await parse(output: $0) }) {
Comment.pullRequestReviewCommentReactionFragment
Comment.issueCommentReactionFragment
PullRequest.reactionsFragment
Expand Down Expand Up @@ -479,13 +479,13 @@ extension Actions {
let userWantsComments = CommandLine.argument(exists: "-comments")
if let pr = item.pullRequest {
let fragment = userWantsComments ? PullRequest.fragmentWithComments : PullRequest.fragment
let queries = Query.batching("PR", groupName: "nodes", idList: [pr.id], maxCost: config.maxNodeCost, perNode: parse) { fragment }
let queries = Query.batching("PR", groupName: "nodes", idList: [pr.id], maxCost: config.maxNodeCost, perNode: { await parse(output: $0) }) { fragment }
try await run(queries)

if userWantsComments {
let reviewIdsWithComments = pr.reviews.compactMap { $0.syncState == .none || !$0.syncNeedsComments ? nil : $0.id }
if !reviewIdsWithComments.isEmpty {
try await run(Query.batching("PR Review Comments", groupName: "nodes", idList: reviewIdsWithComments, maxCost: config.maxNodeCost, perNode: parse) {
try await run(Query.batching("PR Review Comments", groupName: "nodes", idList: reviewIdsWithComments, maxCost: config.maxNodeCost, perNode: { await parse(output: $0) }) {
Review.commentsFragment
PullRequest.commentsFragment
Issue.commentsFragment
Expand All @@ -500,7 +500,7 @@ extension Actions {
itemIdsWithReactions.append(contentsOf: review.comments.compactMap { ($0.syncState == .none || !$0.syncNeedsReactions) ? nil : $0.id })
}
}
try await run(Query.batching("Reactions", groupName: "nodes", idList: itemIdsWithReactions, maxCost: config.maxNodeCost, perNode: parse) {
try await run(Query.batching("Reactions", groupName: "nodes", idList: itemIdsWithReactions, maxCost: config.maxNodeCost, perNode: { await parse(output: $0) }) {
Comment.pullRequestReviewCommentReactionFragment
PullRequest.reactionsFragment
})
Expand All @@ -510,15 +510,15 @@ extension Actions {

} else if let issue = item.issue {
let fragment = userWantsComments ? Issue.fragmentWithComments : Issue.fragment
let queries = Query.batching("Issue", groupName: "nodes", idList: [issue.id], maxCost: config.maxNodeCost, perNode: parse) { fragment }
let queries = Query.batching("Issue", groupName: "nodes", idList: [issue.id], maxCost: config.maxNodeCost, perNode: { await parse(output: $0) }) { fragment }
try await run(queries)

var itemIdsWithReactions = [issue.id]
if userWantsComments {
itemIdsWithReactions += issue.comments.compactMap { ($0.syncState == .none || !$0.syncNeedsReactions) ? nil : $0.id }
}

try await run(Query.batching("Reactions", groupName: "nodes", idList: itemIdsWithReactions, maxCost: config.maxNodeCost, perNode: parse) {
try await run(Query.batching("Reactions", groupName: "nodes", idList: itemIdsWithReactions, maxCost: config.maxNodeCost, perNode: { await parse(output: $0) }) {
Comment.pullRequestReviewCommentReactionFragment
PullRequest.reactionsFragment
})
Expand Down
1 change: 1 addition & 0 deletions Sources/trailer/Actions/Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum Action: String {
case stats
}

@MainActor
enum Actions {
private static func checkArguments() -> [String]? {
// Very rough sanity check to catch typos, should be more fine-grained per action
Expand Down
2 changes: 1 addition & 1 deletion Sources/trailer/Actions/Notifications.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import Lista

struct Notifications {
enum Notifications {
struct Notification {
let title: String?
let subtitle: String?
Expand Down
26 changes: 13 additions & 13 deletions Sources/trailer/Core/DB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@ enum DB {

static func lookup(type: String, id: String) -> (any Item)? {
switch type {
case "Org": return Org.allItems[id]
case "Repo": return Repo.allItems[id]
case "Comment": return Comment.allItems[id]
case "User": return User.allItems[id]
case "Reaction": return Reaction.allItems[id]
case "PullRequest": return PullRequest.allItems[id]
case "Issue": return Issue.allItems[id]
case "Label": return Label.allItems[id]
case "Review": return Review.allItems[id]
case "ReviewRequest": return ReviewRequest.allItems[id]
case "Status": return Status.allItems[id]
case "Milestone": return Milestone.allItems[id]
default: return nil
case "Org": Org.allItems[id]
case "Repo": Repo.allItems[id]
case "Comment": Comment.allItems[id]
case "User": User.allItems[id]
case "Reaction": Reaction.allItems[id]
case "PullRequest": PullRequest.allItems[id]
case "Issue": Issue.allItems[id]
case "Label": Label.allItems[id]
case "Review": Review.allItems[id]
case "ReviewRequest": ReviewRequest.allItems[id]
case "Status": Status.allItems[id]
case "Milestone": Milestone.allItems[id]
default: nil
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/trailer/Core/Dates.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct GHDateFormatter {
enum GHDateFormatter {
#if os(macOS)
private static var timeData = tm()
private static var dateParserHolder = " +0000".cString(using: String.Encoding.ascii)!
Expand Down
2 changes: 1 addition & 1 deletion Sources/trailer/Core/TTY.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct TTY {
enum TTY {
static func rightAlign(_ message: String) -> String {
let c = max(0, 15 - message.count)
let spaces = String(repeating: " ", count: c)
Expand Down
1 change: 1 addition & 0 deletions Sources/trailer/Core/mainasync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import TrailerQL
#endif

@main
@MainActor
struct MainApp {
static func main() async {
TQL.debugLog = { log(level: .debug, indent: 0, $0) }
Expand Down
8 changes: 4 additions & 4 deletions Sources/trailer/Data/Protocols/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ extension Item {
}

static func processAnnouncements(notificationMode: NotificationMode) {
allItems.values.forEach {
if let i = $0 as? Announceable {
for value in allItems.values {
if let i = value as? Announceable {
i.announceIfNeeded(notificationMode: notificationMode)
}
}
Expand Down Expand Up @@ -203,9 +203,9 @@ extension Item {

func children<T: Item>(field: String) -> [T] {
if let childrenIds = DB.idsForChildren(of: id, field: field) {
return childrenIds.compactMap { T.allItems[$0] }
childrenIds.compactMap { T.allItems[$0] }
} else {
return []
[]
}
}

Expand Down

0 comments on commit 080df6e

Please sign in to comment.