-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathRACDelegateProxy.m
75 lines (54 loc) · 1.84 KB
/
RACDelegateProxy.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// RACDelegateProxy.m
// ReactiveObjC
//
// Created by Cody Krieger on 5/19/12.
// Copyright (c) 2012 GitHub, Inc. All rights reserved.
//
#import "RACDelegateProxy.h"
#import "NSObject+RACSelectorSignal.h"
#import <objc/runtime.h>
@interface RACDelegateProxy () {
// Declared as an ivar to avoid method naming conflicts.
Protocol *_protocol;
}
@end
@implementation RACDelegateProxy
#pragma mark Lifecycle
- (instancetype)initWithProtocol:(Protocol *)protocol {
NSCParameterAssert(protocol != NULL);
self = [super init];
class_addProtocol(self.class, protocol);
_protocol = protocol;
return self;
}
#pragma mark API
- (RACSignal *)signalForSelector:(SEL)selector {
return [self rac_signalForSelector:selector fromProtocol:_protocol];
}
#pragma mark NSObject
- (BOOL)isProxy {
return YES;
}
- (void)forwardInvocation:(NSInvocation *)invocation {
[invocation invokeWithTarget:self.rac_proxiedDelegate];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
// Look for the selector as an optional instance method.
struct objc_method_description methodDescription = protocol_getMethodDescription(_protocol, selector, NO, YES);
if (methodDescription.name == NULL) {
// Then fall back to looking for a required instance
// method.
methodDescription = protocol_getMethodDescription(_protocol, selector, YES, YES);
if (methodDescription.name == NULL) return [super methodSignatureForSelector:selector];
}
return [NSMethodSignature signatureWithObjCTypes:methodDescription.types];
}
- (BOOL)respondsToSelector:(SEL)selector {
// Add the delegate to the autorelease pool, so it doesn't get deallocated
// between this method call and -forwardInvocation:.
__autoreleasing id delegate = self.rac_proxiedDelegate;
if ([delegate respondsToSelector:selector]) return YES;
return [super respondsToSelector:selector];
}
@end