Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saroar/1aa43f8cb53b554b060ee9066eabaab1 to your computer and use it in GitHub Desktop.
Save saroar/1aa43f8cb53b554b060ee9066eabaab1 to your computer and use it in GitHub Desktop.
import Combine
import ComposableArchitecture
import KeyboardShortcuts
public struct KeyboardShortcutManager {
public enum Action: Equatable {
case onKeyDown(UUID)
case onKeyUp(UUID)
}
var bind: ([UUID]) -> Effect<Action, Never> = { _ in _unimplemented("bind") }
public func bind(uuids: [UUID]) -> Effect<Action, Never> {
self.bind(uuids)
}
}
extension KeyboardShortcutManager {
public struct Properties: Equatable {
var uuid: UUID? = nil
public init(uuid: UUID? = nil) {
self.uuid = uuid
}
}
}
extension KeyboardShortcutManager {
private static var subscriber: Effect<KeyboardShortcutManager.Action, Never>.Subscriber? = nil
public static let live: () -> KeyboardShortcutManager = {
var manager = KeyboardShortcutManager()
manager.bind = { uuids in
Effect.run { subscriber in
KeyboardShortcutManager.subscriber = subscriber
uuids.forEach{ uuid in
KeyboardShortcuts.onKeyDown(for: KeyboardShortcuts.Name(uuid.uuidString)) {
subscriber.send(.onKeyDown(uuid))
}
KeyboardShortcuts.onKeyUp(for: KeyboardShortcuts.Name(uuid.uuidString)) {
subscriber.send(.onKeyUp(uuid))
}
}
return AnyCancellable {
KeyboardShortcutManager.subscriber = nil
}
}
}
return manager
}
}
public func _unimplemented(
_ function: StaticString, file: StaticString = #file, line: UInt = #line
) -> Never {
fatalError(
"""
`\(function)` was called but is not implemented. Be sure to provide an implementation for
this endpoint when creating the mock.
""",
file: file,
line: line
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment