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 texts: [String: String] = [:]
var comments: [String: [LineComment]]? = nil
var visits: [String: Double]? = nil
}
struct LineAnchor: Codable, Hashable {
@@ -41,8 +42,8 @@ final class DraftSend {
@MainActor
func migrateRecordIDs() {
var changed = false
for v in fetchRecords(FetchDescriptor<VisitRecord>()) where migratedID("tsk", v.taskId) != v.taskId {
v.taskId = migratedID("tsk", v.taskId)
for v in fetchRecords(FetchDescriptor<VisitRecord>()) {
persistence.delete(v)
changed = true
}
let composerKey = { (key: String) in
@@ -93,6 +94,7 @@ final class Drafts {
private var checked: [String: [String]] = [:]
private var texts: [String: String] = [:]
private var comments: [String: [LineComment]] = [:]
@ObservationIgnored private(set) var visits: [String: Double] = [:]
@ObservationIgnored private let scope: String
@ObservationIgnored private var timer: Task<Void, Never>?
@ObservationIgnored private var dirty = false
@@ -113,6 +115,12 @@ final class Drafts {
checked = saved.checked
texts = saved.texts
comments = saved.comments ?? [:]
visits = saved.visits ?? [:]
}
func visit(_ id: String, _ at: Double) {
visits[id] = at
save()
}
func reload() {
@@ -148,6 +156,10 @@ final class Drafts {
texts[id] = nil
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 {
save()
}
@@ -179,7 +191,7 @@ final class Drafts {
return
}
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)
for key in keys {
let inFlight = sending.filter { $0.key == key }
+2 -14
View File
@@ -680,26 +680,14 @@ final class Store {
}
func lastVisits() -> [String: Double] {
fetchRecords(FetchDescriptor<VisitRecord>()).reduce(into: [:]) { visits, v in
visits[v.taskId] = max(visits[v.taskId] ?? 0, v.at)
}
drafts?.visits ?? [:]
}
private func markVisited(_ id: String?) {
guard let id else {
return
}
let at = 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()
drafts?.visit(id, Date().timeIntervalSince1970 * 1000)
}
func loadSummaryReads() async throws {