fix: choose the insertion route on the main actor (crash on background thread)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-02 10:53:36 +02:00
co-authored by Claude Sonnet 5.5
parent 876ca92ae1
commit 951e23ba22
2 changed files with 8 additions and 9 deletions
+2 -3
View File
@@ -12,13 +12,12 @@ import SwiftUI
final class InsertionPolicy { final class InsertionPolicy {
var windowSession = false var windowSession = false
@MainActor
func route() -> InsertionRoute { func route() -> InsertionRoute {
MainActor.assumeIsolated {
let sstInFront = NSWorkspace.shared.frontmostApplication?.bundleIdentifier == Bundle.main.bundleIdentifier let sstInFront = NSWorkspace.shared.frontmostApplication?.bundleIdentifier == Bundle.main.bundleIdentifier
if sstInFront { return NoteEditorRegistry.shared.active != nil ? .editor : .none } if sstInFront { return NoteEditorRegistry.shared.active != nil ? .editor : .none }
return windowSession ? .none : .system return windowSession ? .none : .system
} }
}
} }
@MainActor @MainActor
@@ -103,7 +102,7 @@ final class AppModel: ObservableObject {
inserter: RoutingInserter( inserter: RoutingInserter(
system: TextInserter(pasteboard: SystemPasteboard(), paster: CGEventPaster()), system: TextInserter(pasteboard: SystemPasteboard(), paster: CGEventPaster()),
editor: EditorInserter(), editor: EditorInserter(),
route: { policy.route() }), route: { await policy.route() }),
corrector: corrector, corrector: corrector,
vocabularyProvider: { settings.usePriming ? VocabularyBuilder.build(from: dictionary.entries) : [] }) vocabularyProvider: { settings.usePriming ? VocabularyBuilder.build(from: dictionary.entries) : [] })
+3 -3
View File
@@ -14,16 +14,16 @@ public enum InsertionRoute: Equatable, Sendable {
public struct RoutingInserter: TextInserting { public struct RoutingInserter: TextInserting {
private let system: TextInserting private let system: TextInserting
private let editor: TextInserting private let editor: TextInserting
private let route: () -> InsertionRoute private let route: () async -> InsertionRoute
public init(system: TextInserting, editor: TextInserting, route: @escaping () -> InsertionRoute) { public init(system: TextInserting, editor: TextInserting, route: @escaping () async -> InsertionRoute) {
self.system = system self.system = system
self.editor = editor self.editor = editor
self.route = route self.route = route
} }
public func insert(_ text: String) async throws { public func insert(_ text: String) async throws {
switch route() { switch await route() {
case .system: try await system.insert(text) case .system: try await system.insert(text)
case .editor: try await editor.insert(text) case .editor: try await editor.insert(text)
case .none: break case .none: break