Skip to content

Commit

Permalink
Markup for Sendable
Browse files Browse the repository at this point in the history
  • Loading branch information
rustle committed Nov 16, 2023
1 parent 8385321 commit 6546e4a
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 124 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let package = Package(
from: "0.1.10"),
.package(
url: "https://github.com/rustle/TargetAction.git",
from: "0.1.0"),
from: "0.1.1"),
],
targets: [
.target(
Expand Down
4 changes: 3 additions & 1 deletion Sources/ScreenReader/ArrayObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ public enum ArrayChange<Element>: Equatable where Element: Equatable {
}
}

extension ArrayChange: Sendable where Element: Sendable {}

/// Repackage KVO updates to an observable array
public final class ArrayObserver<Root, Element> where Root: NSObject, Element: Equatable {
private let observer: NSKeyValueObservation
init(
root: Root,
keypath: KeyPath<Root, [Element]>,
changeHandler: @escaping (ArrayChange<Element>) -> Void
changeHandler: @escaping @Sendable (ArrayChange<Element>) -> Void
) {
observer = root.observe(
keypath,
Expand Down
85 changes: 0 additions & 85 deletions Sources/ScreenReader/Concurrency/AsyncSequence.swift

This file was deleted.

12 changes: 6 additions & 6 deletions Sources/ScreenReader/ControllerHierarchy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import AccessibilityElement
import Foundation
import os

public typealias ControllerFactory<ObserverType: AccessibilityElement.Observer> = (
public typealias ControllerFactory<ObserverType: AccessibilityElement.Observer> = @Sendable (
ObserverType.ObserverElement,
AsyncStream<Output.Job>.Continuation,
ApplicationObserver<ObserverType>
) async throws -> Controller where ObserverType.ObserverElement: Hashable

actor ControllerHierarchy<ObserverType: AccessibilityElement.Observer> where ObserverType.ObserverElement: Hashable {
public actor ControllerHierarchy<ObserverType: AccessibilityElement.Observer> where ObserverType.ObserverElement: Hashable {
typealias ElementType = ObserverType.ObserverElement
private struct ControllerContext {
let task: Task<Void, any Error>?
Expand All @@ -28,7 +28,7 @@ actor ControllerHierarchy<ObserverType: AccessibilityElement.Observer> where Obs
private let controllerFactory: ControllerFactory<ObserverType>
private let observer: ApplicationObserver<ObserverType>
private var observerTasks: Set<Task<Void, any Error>> = .init()
init(
public init(
application: Application<ObserverType>,
observer: ApplicationObserver<ObserverType>,
controllerFactory: @escaping ControllerFactory<ObserverType>
Expand All @@ -39,7 +39,7 @@ actor ControllerHierarchy<ObserverType: AccessibilityElement.Observer> where Obs
}
private func uiElementDestroyed(
element: ElementType,
userInfo: [String:Any]?
userInfo: [String:Sendable]?
) async {
logger.debug("\(element)")
guard let context = controllers.removeValue(forKey: element) else { return }
Expand Down Expand Up @@ -135,10 +135,10 @@ actor ControllerHierarchy<ObserverType: AccessibilityElement.Observer> where Obs
element: element,
notification: .uiElementDestroyed
)
let action = target(action: ControllerHierarchy<ObserverType>.uiElementDestroyed)
let target = target(uncheckedAction: ControllerHierarchy<ObserverType>.uiElementDestroyed)
let task: Task<Void, any Error> = Task(priority: .userInitiated) {
for try await notification in stream {
await action(
await target(
notification.element,
notification.info
)
Expand Down
11 changes: 6 additions & 5 deletions Sources/ScreenReader/Controllers/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public actor Application<ObserverType: Observer>: Controller where ObserverType.
observer: observer,
element: element,
notification: .windowCreated,
handler: target(action: Application.windowCreated)
handler: target(uncheckedAction: Application.windowCreated)
))
} catch let error as ControllerObserverError {
logger.debug("\(error.localizedDescription)")
Expand All @@ -84,7 +84,7 @@ public actor Application<ObserverType: Observer>: Controller where ObserverType.
observer: observer,
element: element,
notification: .focusedWindowChanged,
handler: target(action: Application.focusedWindowChanged)
handler: target(uncheckedAction: Application.focusedWindowChanged)
))
} catch let error as ControllerObserverError {
logger.debug("\(error.localizedDescription)")
Expand Down Expand Up @@ -130,7 +130,7 @@ public actor Application<ObserverType: Observer>: Controller where ObserverType.
}
private func windowCreated(
window: ElementType,
userInfo: [String:Any]?
userInfo: [String:Sendable]?
) async {
logger.debug("\(window)")
do {
Expand All @@ -141,7 +141,7 @@ public actor Application<ObserverType: Observer>: Controller where ObserverType.
}
private func focusedWindowChanged(
element: ElementType,
userInfo: [String:Any]?
userInfo: [String:Sendable]?
) async {
logger.debug("\(element)")
do {
Expand All @@ -152,7 +152,7 @@ public actor Application<ObserverType: Observer>: Controller where ObserverType.
}
private func focusedUIElementChanged(
element: ElementType,
userInfo: [String:Any]?
userInfo: [String:Sendable]?
) async {
logger.debug("\(element.description)")
do {
Expand Down Expand Up @@ -181,6 +181,7 @@ public actor Application<ObserverType: Observer>: Controller where ObserverType.
}

extension Application {
@Sendable
fileprivate static func controller(
element: ElementType,
output: AsyncStream<Output.Job>.Continuation,
Expand Down
4 changes: 2 additions & 2 deletions Sources/ScreenReader/Controllers/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public actor Button<ObserverType: Observer>: Controller where ObserverType.Obser
do {
observerTasks.append(try await add(
notification: .valueChanged,
handler: target(action: Button<ObserverType>.valueChanged)
handler: target(uncheckedAction: Button<ObserverType>.valueChanged)
))
} catch let error as ControllerObserverError {
logger.info("\(error.localizedDescription)")
Expand All @@ -59,7 +59,7 @@ public actor Button<ObserverType: Observer>: Controller where ObserverType.Obser
}
private func valueChanged(
element: ElementType,
userInfo: [String:Any]?
userInfo: [String:Sendable]?
) async {
logger.debug("\(element)")
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/ScreenReader/Controllers/Controller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ enum RunState {
}

extension Controller {
@Sendable
static func add<ObserverType: Observer>(
observer: ApplicationObserver<ObserverType>,
element: ObserverType.ObserverElement,
notification: NSAccessibility.Notification,
handler: @escaping (ObserverType.ObserverElement, [String:Any]?) async -> Void
handler: @escaping @Sendable (ObserverType.ObserverElement, [String:Sendable]?) async -> Void
) async throws -> Task<Void, any Error> {
do {
let stream = try await observer.stream(
Expand Down
4 changes: 2 additions & 2 deletions Sources/ScreenReader/Controllers/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public actor List<ObserverType: Observer>: Controller where ObserverType.Observe
do {
observerTasks.append(try await add(
notification: .selectedChildrenChanged,
handler: target(action: List<ObserverType>.selectedChildrenChanged)
handler: target(uncheckedAction: List<ObserverType>.selectedChildrenChanged)
))
} catch let error as ControllerObserverError {
logger.info("\(error.localizedDescription)")
Expand Down Expand Up @@ -88,7 +88,7 @@ public actor List<ObserverType: Observer>: Controller where ObserverType.Observe
}
private func selectedChildrenChanged(
element: ElementType,
userInfo: [String:Any]?
userInfo: [String:Sendable]?
) async {
logger.debug("\(self.element)")
do {
Expand Down
6 changes: 4 additions & 2 deletions Sources/ScreenReader/Controllers/ObserverHosting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ protocol ObserverHosting: Controller {
typealias ElementType = ObserverType.ObserverElement
var element: ElementType { get }
var observer: ApplicationObserver<ObserverType> { get }
@Sendable
func add(
notification: NSAccessibility.Notification,
handler: @escaping (ElementType, [String:Any]?) async -> Void
handler: @escaping @Sendable (ElementType, [String:Sendable]?) async -> Void
) async throws -> Task<Void, any Error>
}

extension ObserverHosting {
@Sendable
func add(
notification: NSAccessibility.Notification,
handler: @escaping (ElementType, [String:Any]?) async -> Void
handler: @escaping @Sendable (ElementType, [String:Sendable]?) async -> Void
) async throws -> Task<Void, any Error> {
try await Self.add(
observer: observer,
Expand Down
8 changes: 4 additions & 4 deletions Sources/ScreenReader/Controllers/Table.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public actor Table<ObserverType: Observer>: Controller where ObserverType.Observ
guard runState == .stopped else { return }
try await _add(
notification: .selectedRowsChanged,
handler: target(action: Table<ObserverType>.selectionChanged)
handler: target(uncheckedAction: Table<ObserverType>.selectionChanged)
)
try await _add(
notification: .selectedColumnsChanged,
handler: target(action: Table<ObserverType>.selectionChanged)
handler: target(uncheckedAction: Table<ObserverType>.selectionChanged)
)
runState = .running
await selectionChanged(
Expand All @@ -53,7 +53,7 @@ public actor Table<ObserverType: Observer>: Controller where ObserverType.Observ
}
private func _add(
notification: NSAccessibility.Notification,
handler: @escaping (ObserverType.ObserverElement, [String : Any]?) async -> Void
handler: @escaping @Sendable (ObserverType.ObserverElement, [String:Sendable]?) async -> Void
) async throws {
do {
observerTasks.append(try await add(
Expand All @@ -77,7 +77,7 @@ public actor Table<ObserverType: Observer>: Controller where ObserverType.Observ
}
private func selectionChanged(
element: ElementType,
userInfo: [String:Any]?
userInfo: [String:Sendable]?
) async {
logger.debug("\(self.element)")
do {
Expand Down
8 changes: 4 additions & 4 deletions Sources/ScreenReader/Controllers/TextArea.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public actor TextArea<ObserverType: Observer>: Controller where ObserverType.Obs
do {
observerTasks.append(try await add(
notification: .valueChanged,
handler: target(action: TextArea<ObserverType>.valueChanged)
handler: target(uncheckedAction: TextArea<ObserverType>.valueChanged)
))
} catch let error as ControllerObserverError {
logger.info("\(error.localizedDescription)")
Expand All @@ -50,7 +50,7 @@ public actor TextArea<ObserverType: Observer>: Controller where ObserverType.Obs
do {
observerTasks.append(try await add(
notification: .selectedTextChanged,
handler: target(action: TextArea<ObserverType>.selectedTextChanged)
handler: target(uncheckedAction: TextArea<ObserverType>.selectedTextChanged)
))
} catch let error as ControllerObserverError {
logger.info("\(error.localizedDescription)")
Expand All @@ -70,13 +70,13 @@ public actor TextArea<ObserverType: Observer>: Controller where ObserverType.Obs
}
private func valueChanged(
element: ElementType,
userInfo: [String:Any]?
userInfo: [String:Sendable]?
) async {
//logger.debug("\(self.element)")
}
private func selectedTextChanged(
element: ElementType,
userInfo: [String:Any]?
userInfo: [String:Sendable]?
) async {
//logger.debug("\(self.element)")
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/ScreenReader/Controllers/TextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public actor TextField<ObserverType: Observer>: Controller where ObserverType.Ob
do {
observerTasks.append(try await add(
notification: .valueChanged,
handler: target(action: TextField<ObserverType>.valueChanged)
handler: target(uncheckedAction: TextField<ObserverType>.valueChanged)
))
} catch let error as ControllerObserverError {
logger.info("\(error.localizedDescription)")
Expand All @@ -50,7 +50,7 @@ public actor TextField<ObserverType: Observer>: Controller where ObserverType.Ob
do {
observerTasks.append(try await add(
notification: .selectedTextChanged,
handler: target(action: TextField<ObserverType>.selectedTextChanged)
handler: target(uncheckedAction: TextField<ObserverType>.selectedTextChanged)
))
} catch let error as ControllerObserverError {
logger.info("\(error.localizedDescription)")
Expand Down Expand Up @@ -96,13 +96,13 @@ public actor TextField<ObserverType: Observer>: Controller where ObserverType.Ob
}
private func valueChanged(
element: ElementType,
userInfo: [String:Any]?
userInfo: [String:Sendable]?
) async {
logger.debug("\(self.element)")
}
private func selectedTextChanged(
element: ElementType,
userInfo: [String:Any]?
userInfo: [String:Sendable]?
) async {
logger.debug("\(self.element)")
}
Expand Down
Loading

0 comments on commit 6546e4a

Please sign in to comment.