Skip to content

Commit

Permalink
refactor: remove ConcurrencyExtras package dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Akazm committed Dec 31, 2024
1 parent 4886cb7 commit 472fe9d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ let package = Package(
.package(url: "https://github.com/Akazm/allocated-unfair-lock", .upToNextMajor(from: "1.2.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/nicklockwood/SwiftFormat", .upToNextMajor(from: "0.55.0")),
],
targets: [
Expand All @@ -26,7 +25,6 @@ let package = Package(
.product(name: "AllocatedUnfairLockShim", package: "allocated-unfair-lock"),
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras")
],
path: "Sources/TapGuard"
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
@preconcurrency import AppKit
import AsyncAlgorithms
import ConcurrencyExtras

private struct ImmutableNotification: Sendable {
struct ImmutableNotification: Sendable {
let name: Notification.Name

init(_ notification: Notification) {
Expand Down Expand Up @@ -33,13 +32,23 @@ private extension NSAccessibility.Notification {
static let isProcessTrusted = NSNotification.Name("com.apple.accessibility.api")
}

typealias NotificationToggleSequence = AsyncMerge2Sequence<
AsyncMapSequence<AsyncStream<ImmutableNotification>, HIDEventDispatcherEnabledPrerequisite.Change>,
AsyncMapSequence<AsyncStream<ImmutableNotification>, HIDEventDispatcherEnabledPrerequisite.Change>
>

typealias IsProcessTrustedNotifications = AsyncMapSequence<
AsyncFlatMapSequence<AsyncSyncSequence<[Bool]>, AsyncScanSequence<AsyncStream<ImmutableNotification>, ()>>,
HIDEventDispatcherEnabledPrerequisite.Change
>

extension HIDEventDispatcherEnabledPrerequisite {
public enum Change: Sendable, Equatable {
case add(HIDEventDispatcherEnabledPrerequisite)
case remove(HIDEventDispatcherEnabledPrerequisite)
}

static var screensNotification: AsyncStream<HIDEventDispatcherEnabledPrerequisite.Change> {
static var screensNotification: NotificationToggleSequence {
merge(
notifications(
named: NSWorkspace.screensDidSleepNotification,
Expand All @@ -51,10 +60,10 @@ extension HIDEventDispatcherEnabledPrerequisite {
notificationCenter: NSWorkspace.shared.notificationCenter
)
.map { _ in HIDEventDispatcherEnabledPrerequisite.Change.add(.screensAwake) }
).eraseToStream()
)
}

static var workspaceNotifications: AsyncStream<HIDEventDispatcherEnabledPrerequisite.Change> {
static var workspaceNotifications: NotificationToggleSequence {
merge(
notifications(
named: NSWorkspace.willSleepNotification, notificationCenter: DistributedNotificationCenter.default()
Expand All @@ -64,10 +73,10 @@ extension HIDEventDispatcherEnabledPrerequisite {
named: NSWorkspace.didWakeNotification, notificationCenter: DistributedNotificationCenter.default()
)
.map { _ in HIDEventDispatcherEnabledPrerequisite.Change.add(.deviceAwake) }
).eraseToStream()
)
}

static var isProcessTrustedNotifications: AsyncStream<HIDEventDispatcherEnabledPrerequisite.Change> {
static var isProcessTrustedNotifications: IsProcessTrustedNotifications {
[AXIsProcessTrusted()]
.async
.flatMap { _ in
Expand All @@ -83,6 +92,6 @@ extension HIDEventDispatcherEnabledPrerequisite {
return AXIsProcessTrusted()
? HIDEventDispatcherEnabledPrerequisite.Change.add(.axGranted)
: HIDEventDispatcherEnabledPrerequisite.Change.remove(.axGranted)
}.eraseToStream()
}
}
}

0 comments on commit 472fe9d

Please sign in to comment.