@@ -6,7 +6,7 @@ extension NSObject {
66 _ selector: Selector ,
77 methodSignature: MethodSignature . Type = MethodSignature . self,
88 hookSignature: HookSignature . Type = HookSignature . self,
9- _ implementation: ( TypedHook < MethodSignature , HookSignature > ) -> HookSignature ? ) throws -> AnyHook {
9+ _ implementation: ( TypedHook < MethodSignature , HookSignature > ) -> HookSignature ? ) throws -> AnyHook {
1010
1111 if let klass = self as? AnyClass {
1212 return try Interpose . ClassHook ( class: klass, selector: selector, implementation: implementation) . apply ( )
@@ -20,8 +20,9 @@ extension NSObject {
2020 _ selector: Selector ,
2121 methodSignature: MethodSignature . Type = MethodSignature . self,
2222 hookSignature: HookSignature . Type = HookSignature . self,
23- _ implementation: ( TypedHook < MethodSignature , HookSignature > ) -> HookSignature ? ) throws -> AnyHook {
24- return try Interpose . ClassHook ( class: self as AnyClass , selector: selector, implementation: implementation) . apply ( )
23+ _ implementation: ( TypedHook < MethodSignature , HookSignature > ) -> HookSignature ? ) throws -> AnyHook {
24+ return try Interpose . ClassHook ( class: self as AnyClass ,
25+ selector: selector, implementation: implementation) . apply ( )
2526 }
2627}
2728
@@ -88,31 +89,47 @@ final public class Interpose {
8889 deinit {
8990 hooks. forEach ( { $0. cleanup ( ) } )
9091 }
91-
92+
9293 /// Hook an `@objc dynamic` instance method via selector name on the current class.
9394 @discardableResult public func hook< MethodSignature, HookSignature> (
9495 _ selName: String ,
9596 methodSignature: MethodSignature . Type = MethodSignature . self,
9697 hookSignature: HookSignature . Type = HookSignature . self,
97- _ implementation: ( TypedHook < MethodSignature , HookSignature > ) -> HookSignature ? ) throws -> TypedHook < MethodSignature , HookSignature > {
98- try hook ( NSSelectorFromString ( selName) , methodSignature: methodSignature, hookSignature: hookSignature, implementation)
98+ _ implementation: ( TypedHook < MethodSignature , HookSignature > ) -> HookSignature ? )
99+ throws -> TypedHook < MethodSignature , HookSignature > {
100+ try hook ( NSSelectorFromString ( selName) ,
101+ methodSignature: methodSignature, hookSignature: hookSignature, implementation)
99102 }
100103
101104 /// Hook an `@objc dynamic` instance method via selector on the current class.
102105 @discardableResult public func hook< MethodSignature, HookSignature> (
103106 _ selector: Selector ,
104107 methodSignature: MethodSignature . Type = MethodSignature . self,
105108 hookSignature: HookSignature . Type = HookSignature . self,
106- _ implementation: ( TypedHook < MethodSignature , HookSignature > ) -> HookSignature ? ) throws -> TypedHook < MethodSignature , HookSignature > {
109+ _ implementation: ( TypedHook < MethodSignature , HookSignature > ) -> HookSignature ? )
110+ throws -> TypedHook < MethodSignature , HookSignature > {
111+ let hook = try prepareHook ( selector, methodSignature: methodSignature,
112+ hookSignature: hookSignature, implementation)
113+ try hook. apply ( )
114+ return hook
107115
108- var hook : TypedHook < MethodSignature , HookSignature >
109- if let object = self . object {
110- hook = try ObjectHook ( object: object, selector: selector, implementation: implementation)
111- } else {
112- hook = try ClassHook ( class: `class`, selector: selector, implementation: implementation)
113- }
114- hooks. append ( hook)
115- return hook
116+ }
117+
118+ /// Prepares a hook, but does not call apply immediately.
119+ @discardableResult public func prepareHook< MethodSignature, HookSignature> (
120+ _ selector: Selector ,
121+ methodSignature: MethodSignature . Type = MethodSignature . self,
122+ hookSignature: HookSignature . Type = HookSignature . self,
123+ _ implementation: ( TypedHook < MethodSignature , HookSignature > ) -> HookSignature ? )
124+ throws -> TypedHook < MethodSignature , HookSignature > {
125+ var hook : TypedHook < MethodSignature , HookSignature >
126+ if let object = self . object {
127+ hook = try ObjectHook ( object: object, selector: selector, implementation: implementation)
128+ } else {
129+ hook = try ClassHook ( class: `class`, selector: selector, implementation: implementation)
130+ }
131+ hooks. append ( hook)
132+ return hook
116133 }
117134
118135 /// Apply all stored hooks.
0 commit comments