diff --git a/Package.swift b/Package.swift index 120cf0a..9681d0a 100644 --- a/Package.swift +++ b/Package.swift @@ -4,7 +4,7 @@ import PackageDescription let package = Package( name: "tap-guard", platforms: [ - .macOS(.v10_15), + .macOS(.v10_15) ], products: [ .library( @@ -14,10 +14,10 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/Akazm/allocated-unfair-lock", .upToNextMajor(from: "1.2.0")), - .package(url: "https://github.com/nicklockwood/SwiftFormat", .upToNextMajor(from: "0.55.0")), .package(url: "https://github.com/apple/swift-async-algorithms", .upToNextMajor(from: "1.0.0")), .package(url: "https://github.com/apple/swift-atomics.git", .upToNextMajor(from: "1.2.0")), - .package(url: "https://github.com/pointfreeco/swift-concurrency-extras", .upToNextMajor(from: "1.3.0")) + .package(url: "https://github.com/pointfreeco/swift-concurrency-extras", .upToNextMajor(from: "1.3.0")), + .package(url: "https://github.com/nicklockwood/SwiftFormat", .upToNextMajor(from: "0.55.0")), ], targets: [ .target( @@ -25,8 +25,8 @@ let package = Package( dependencies: [ .product(name: "AllocatedUnfairLockShim", package: "allocated-unfair-lock"), .product(name: "AsyncAlgorithms", package: "swift-async-algorithms"), - .product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"), - .product(name: "Atomics", package: "swift-atomics") + .product(name: "Atomics", package: "swift-atomics"), + .product(name: "ConcurrencyExtras", package: "swift-concurrency-extras") ], path: "Sources/TapGuard" ), diff --git a/Sources/TapGuard/Internal Extensions/AsyncScanSequence.swift b/Sources/TapGuard/Internal Extensions/AsyncScanSequence.swift index 84b8de8..e958db7 100644 --- a/Sources/TapGuard/Internal Extensions/AsyncScanSequence.swift +++ b/Sources/TapGuard/Internal Extensions/AsyncScanSequence.swift @@ -2,62 +2,62 @@ // Original: https://github.com/sideeffect-io/AsyncExtensions extension AsyncSequence { - func scan( - _ initialResult: Output, - _ nextPartialResult: @Sendable @escaping (Output, Element) async -> Output - ) -> AsyncScanSequence { - AsyncScanSequence(self, initialResult: initialResult, nextPartialResult: nextPartialResult) - } + func scan( + _ initialResult: Output, + _ nextPartialResult: @Sendable @escaping (Output, Element) async -> Output + ) -> AsyncScanSequence { + AsyncScanSequence(self, initialResult: initialResult, nextPartialResult: nextPartialResult) + } } struct AsyncScanSequence: AsyncSequence { - typealias Element = Output - typealias AsyncIterator = Iterator - - var base: Base - var initialResult: Output - let nextPartialResult: @Sendable (Output, Base.Element) async -> Output - - init( - _ base: Base, - initialResult: Output, - nextPartialResult: @Sendable @escaping (Output, Base.Element) async -> Output - ) { - self.base = base - self.initialResult = initialResult - self.nextPartialResult = nextPartialResult - } - - func makeAsyncIterator() -> AsyncIterator { - Iterator( - base: self.base.makeAsyncIterator(), - initialResult: self.initialResult, - nextPartialResult: self.nextPartialResult - ) - } + typealias Element = Output + typealias AsyncIterator = Iterator - struct Iterator: AsyncIteratorProtocol { - var base: Base.AsyncIterator - var currentValue: Output + var base: Base + var initialResult: Output let nextPartialResult: @Sendable (Output, Base.Element) async -> Output init( - base: Base.AsyncIterator, - initialResult: Output, - nextPartialResult: @Sendable @escaping (Output, Base.Element) async -> Output + _ base: Base, + initialResult: Output, + nextPartialResult: @Sendable @escaping (Output, Base.Element) async -> Output ) { - self.base = base - self.currentValue = initialResult - self.nextPartialResult = nextPartialResult + self.base = base + self.initialResult = initialResult + self.nextPartialResult = nextPartialResult } - mutating func next() async rethrows -> Output? { - let nextUpstreamValue = try await self.base.next() - guard let nonNilNextUpstreamValue = nextUpstreamValue else { return nil } - self.currentValue = await self.nextPartialResult(self.currentValue, nonNilNextUpstreamValue) - return self.currentValue + func makeAsyncIterator() -> AsyncIterator { + Iterator( + base: base.makeAsyncIterator(), + initialResult: initialResult, + nextPartialResult: nextPartialResult + ) + } + + struct Iterator: AsyncIteratorProtocol { + var base: Base.AsyncIterator + var currentValue: Output + let nextPartialResult: @Sendable (Output, Base.Element) async -> Output + + init( + base: Base.AsyncIterator, + initialResult: Output, + nextPartialResult: @Sendable @escaping (Output, Base.Element) async -> Output + ) { + self.base = base + currentValue = initialResult + self.nextPartialResult = nextPartialResult + } + + mutating func next() async rethrows -> Output? { + let nextUpstreamValue = try await base.next() + guard let nonNilNextUpstreamValue = nextUpstreamValue else { return nil } + currentValue = await nextPartialResult(currentValue, nonNilNextUpstreamValue) + return currentValue + } } - } } extension AsyncScanSequence: Sendable where Base: Sendable, Output: Sendable {} diff --git a/Sources/Tests/HIDEventDispatcherTests.swift b/Sources/Tests/HIDEventDispatcherTests.swift index a191c2c..10f02e1 100644 --- a/Sources/Tests/HIDEventDispatcherTests.swift +++ b/Sources/Tests/HIDEventDispatcherTests.swift @@ -5,7 +5,6 @@ import Foundation import Testing @Suite("HIDEventDispatcher Tests") struct HIDEventDispatcherTests { - @Test("HIDEventDispatcher will be automatically re-enabled") func reenableDispatcher() async throws { let (dispatcher, eventSource, _) = makeTestDispatcher() var observedPrequisites = [HIDEventDispatcherEnabledPrerequisite]() @@ -43,7 +42,7 @@ import Testing receiver.remove() observationTask.cancel() } - + @Test("HIDEventReceiver proxy") func hidEventReceiverProxy() async throws { let (dispatcher, _, _) = makeTestDispatcher() final class Receiver: HIDEventReceiver {