Skip to content

Commit

Permalink
refactor: reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
Akazm committed Dec 18, 2024
1 parent c597d01 commit 3f15635
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 52 deletions.
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PackageDescription
let package = Package(
name: "tap-guard",
platforms: [
.macOS(.v10_15),
.macOS(.v10_15)
],
products: [
.library(
Expand All @@ -14,19 +14,19 @@ 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(
name: "TapGuard",
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"
),
Expand Down
90 changes: 45 additions & 45 deletions Sources/TapGuard/Internal Extensions/AsyncScanSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,62 @@
// Original: https://github.com/sideeffect-io/AsyncExtensions

extension AsyncSequence {
func scan<Output>(
_ initialResult: Output,
_ nextPartialResult: @Sendable @escaping (Output, Element) async -> Output
) -> AsyncScanSequence<Self, Output> {
AsyncScanSequence(self, initialResult: initialResult, nextPartialResult: nextPartialResult)
}
func scan<Output>(
_ initialResult: Output,
_ nextPartialResult: @Sendable @escaping (Output, Element) async -> Output
) -> AsyncScanSequence<Self, Output> {
AsyncScanSequence(self, initialResult: initialResult, nextPartialResult: nextPartialResult)
}
}

struct AsyncScanSequence<Base: AsyncSequence, Output>: 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 {}
Expand Down
3 changes: 1 addition & 2 deletions Sources/Tests/HIDEventDispatcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]()
Expand Down Expand Up @@ -43,7 +42,7 @@ import Testing
receiver.remove()
observationTask.cancel()
}

@Test("HIDEventReceiver proxy") func hidEventReceiverProxy() async throws {
let (dispatcher, _, _) = makeTestDispatcher()
final class Receiver: HIDEventReceiver {
Expand Down

0 comments on commit 3f15635

Please sign in to comment.