Skip to content

Commit a326507

Browse files
committed
feat: Default interception handler key improvements
1 parent 6c52750 commit a326507

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ If you use SwiftPM for your project, you can add CombineInterception to your pac
8080
```swift
8181
.package(
8282
url: "https://github.com/capturecontext/swift-interception.git",
83-
.upToNextMinor(from: "0.0.1")
83+
.upToNextMinor(from: "0.1.0")
8484
)
8585
```
8686

Sources/Interception/NSObject+Interception.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ extension NSObject {
2727
/// - action: Action to perform when `selector` was triggered
2828
public func setInterceptionHandler(
2929
for selector: Selector,
30-
key: AnyHashable = "__default_handler__",
30+
key: AnyHashable? = nil,
3131
action: ((InterceptionResult<Any, Any>) -> Void)?
3232
) {
3333
let handler = _intercept(selector)
34-
handler.register(action.map(SimpleInterceptionHandler.init), for: key)
34+
handler.register(
35+
action.map(SimpleInterceptionHandler.init),
36+
for: SwiftInterceptionDefaultInterceptionHandlerKey.unwrap(key)
37+
)
3538
}
3639

3740
/// Sets interception handler, which accepts ``InterceptionResult`` containing a tuple
@@ -45,13 +48,13 @@ extension NSObject {
4548
/// - action: Action to perform when `selector` was triggered
4649
public func setInterceptionHandler<Args, Output>(
4750
for selector: _MethodSelector<Args, Output>,
48-
key: AnyHashable = "__default",
51+
key: AnyHashable? = nil,
4952
action: ((InterceptionResult<Args, Output>) -> Void)?
5053
) {
5154
let handler = _intercept(selector.wrappedValue)
5255

5356
guard let action else {
54-
handler.register(nil, for: key)
57+
handler.register(nil, for: SwiftInterceptionDefaultInterceptionHandlerKey.unwrap(key))
5558
return
5659
}
5760

@@ -62,7 +65,7 @@ extension NSObject {
6265
output: Output.self
6366
))
6467
},
65-
for: key
68+
for: SwiftInterceptionDefaultInterceptionHandlerKey.unwrap(key)
6669
)
6770
}
6871

@@ -322,6 +325,17 @@ private func setupMethodSignatureCaching(_ realClass: AnyClass, _ signatureCache
322325
)
323326
}
324327

328+
@_spi(Internals)
329+
public struct SwiftInterceptionDefaultInterceptionHandlerKey: Hashable {
330+
public static let shared: Self = .init()
331+
332+
private init() {}
333+
334+
public static func unwrap(_ key: AnyHashable?) -> AnyHashable {
335+
key ?? Self.shared as AnyHashable
336+
}
337+
}
338+
325339
@_spi(Internals)
326340
public protocol InterceptionHandlerProtocol {
327341
func handle(_ result: InterceptionResult<Any, Any>)

0 commit comments

Comments
 (0)