mirror of
https://github.com/meshtastic/Meshtastic-Apple.git
synced 2026-04-20 22:13:56 +00:00
Address code review: add debounce constant and thread-safety comment
Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Apple/sessions/9bfe91f2-8ed7-4d2c-bb2e-4ed3dfd3a16c Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com>
This commit is contained in:
parent
a8f4cf9631
commit
b6d46ade94
2 changed files with 6 additions and 2 deletions
|
|
@ -45,6 +45,7 @@ class Router: ObservableObject {
|
|||
// MARK: Node Object ID Cache
|
||||
|
||||
/// In-memory cache mapping node numbers to their Core Data `NSManagedObjectID` for O(1) lookups.
|
||||
/// Thread-safe by virtue of Router's @MainActor isolation — all access is on the main thread.
|
||||
private var nodeObjectIDCache: [Int64: NSManagedObjectID] = [:]
|
||||
|
||||
/// Updates the node cache from a set of fetched nodes. Call this when the node list changes.
|
||||
|
|
|
|||
|
|
@ -11,6 +11,9 @@ import CoreData
|
|||
import Foundation
|
||||
|
||||
struct NodeList: View {
|
||||
/// Debounce delay for node selection changes (100ms)
|
||||
private static let nodeSelectionDebounceNs: UInt64 = 100_000_000
|
||||
|
||||
@Environment(\.managedObjectContext) var context
|
||||
@EnvironmentObject var accessoryManager: AccessoryManager
|
||||
@StateObject var router: Router
|
||||
|
|
@ -125,11 +128,11 @@ struct NodeList: View {
|
|||
}
|
||||
}
|
||||
.onChange(of: router.nodeListSelectedNodeNum) { _, newNum in
|
||||
// Debounce rapid route changes — only process the last selection after 100ms
|
||||
// Debounce rapid route changes — only process the last selection after a short delay
|
||||
nodeSelectionTask?.cancel()
|
||||
nodeSelectionTask = Task {
|
||||
do {
|
||||
try await Task.sleep(nanoseconds: 100_000_000)
|
||||
try await Task.sleep(nanoseconds: Self.nodeSelectionDebounceNs)
|
||||
} catch {
|
||||
return // Cancelled by a newer selection
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue