Fix performance issues found by audit of fa73b74

This commit is contained in:
Ian Gulliver
2026-10-23 14:06:22 -07:00
parent 8b8551a358
commit dc1769aaeb
2 changed files with 17 additions and 17 deletions
+15 -3
View File
@@ -12,6 +12,7 @@ private struct Saved: Codable {
var checked: [String: [String]] = [:] var checked: [String: [String]] = [:]
var texts: [String: String] = [:] var texts: [String: String] = [:]
var comments: [String: [LineComment]]? = nil var comments: [String: [LineComment]]? = nil
var visits: [String: Double]? = nil
} }
struct LineAnchor: Codable, Hashable { struct LineAnchor: Codable, Hashable {
@@ -41,8 +42,8 @@ final class DraftSend {
@MainActor @MainActor
func migrateRecordIDs() { func migrateRecordIDs() {
var changed = false var changed = false
for v in fetchRecords(FetchDescriptor<VisitRecord>()) where migratedID("tsk", v.taskId) != v.taskId { for v in fetchRecords(FetchDescriptor<VisitRecord>()) {
v.taskId = migratedID("tsk", v.taskId) persistence.delete(v)
changed = true changed = true
} }
let composerKey = { (key: String) in let composerKey = { (key: String) in
@@ -93,6 +94,7 @@ final class Drafts {
private var checked: [String: [String]] = [:] private var checked: [String: [String]] = [:]
private var texts: [String: String] = [:] private var texts: [String: String] = [:]
private var comments: [String: [LineComment]] = [:] private var comments: [String: [LineComment]] = [:]
@ObservationIgnored private(set) var visits: [String: Double] = [:]
@ObservationIgnored private let scope: String @ObservationIgnored private let scope: String
@ObservationIgnored private var timer: Task<Void, Never>? @ObservationIgnored private var timer: Task<Void, Never>?
@ObservationIgnored private var dirty = false @ObservationIgnored private var dirty = false
@@ -113,6 +115,12 @@ final class Drafts {
checked = saved.checked checked = saved.checked
texts = saved.texts texts = saved.texts
comments = saved.comments ?? [:] comments = saved.comments ?? [:]
visits = saved.visits ?? [:]
}
func visit(_ id: String, _ at: Double) {
visits[id] = at
save()
} }
func reload() { func reload() {
@@ -148,6 +156,10 @@ final class Drafts {
texts[id] = nil texts[id] = nil
changed = true changed = true
} }
for id in visits.keys where store.findTask(id)?.status != .active && !store.projects.contains(where: { $0.id == id }) {
visits[id] = nil
changed = true
}
if changed { if changed {
save() save()
} }
@@ -179,7 +191,7 @@ final class Drafts {
return return
} }
dirty = false dirty = false
var out = Saved(checked: checked, texts: texts, comments: comments) var out = Saved(checked: checked, texts: texts, comments: comments, visits: visits)
let keys = Set(composerTexts.keys).union(composerImages.keys).union(sending.map(\.key)).union(lostImages.keys) let keys = Set(composerTexts.keys).union(composerImages.keys).union(sending.map(\.key)).union(lostImages.keys)
for key in keys { for key in keys {
let inFlight = sending.filter { $0.key == key } let inFlight = sending.filter { $0.key == key }
+2 -14
View File
@@ -680,26 +680,14 @@ final class Store {
} }
func lastVisits() -> [String: Double] { func lastVisits() -> [String: Double] {
fetchRecords(FetchDescriptor<VisitRecord>()).reduce(into: [:]) { visits, v in drafts?.visits ?? [:]
visits[v.taskId] = max(visits[v.taskId] ?? 0, v.at)
}
} }
private func markVisited(_ id: String?) { private func markVisited(_ id: String?) {
guard let id else { guard let id else {
return return
} }
let at = Date().timeIntervalSince1970 * 1000 drafts?.visit(id, Date().timeIntervalSince1970 * 1000)
let records = fetchRecords(FetchDescriptor<VisitRecord>(predicate: #Predicate { $0.taskId == id }))
if records.isEmpty {
persistence.insert(VisitRecord(taskId: id, at: at))
} else {
records[0].at = at
for record in records.dropFirst() {
persistence.delete(record)
}
}
saveRecords()
} }
func loadSummaryReads() async throws { func loadSummaryReads() async throws {