YH_ModuleService.m
2.92 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// YH_ModuleService.m
// YH_WatchDog
//
// Created by redding on 16/3/7.
// Copyright © 2016年 YOHO. All rights reserved.
//
#import "YH_ModuleService.h"
#import "YH_WatchDog.h"
@implementation YH_ModuleService
{
Class _moduleClass;
NSInvocation *_invocation;
NSString *_methodSignature;
SEL _selector;
}
@synthesize serviceName = _serviceName;
- (instancetype)initWithServiceName:(NSString *)serviceName methodSignature:(NSString *)objCMethodName moduleClass:(__unsafe_unretained Class)moduleClass callbackSignal:(UBSignal *)signal queue:(NSOperationQueue *)queue
{
if ((self = [super init])) {
_serviceName = [serviceName copy];
_moduleClass = moduleClass;
_methodSignature = [objCMethodName copy];
_signal = signal;
_queue = queue;
}
return self;
}
- (instancetype)initWithServiceName:(NSString *)serviceName methodSignature:(NSString *)objCMethodName moduleClass:(Class)moduleClass
{
self = [self initWithServiceName:serviceName methodSignature:objCMethodName moduleClass:moduleClass callbackSignal:nil queue:nil];
if (self) {
}
return self;
}
- (void)processMethodSignature
{
// module的service方法的名称格式:serviceName:(NSDictionary *)parameters
// 生成selector需要的是:serviceName:
NSRange range = [_methodSignature rangeOfString:@":"];
NSString *selectorString = [_methodSignature substringToIndex:range.location + 1];
_selector = NSSelectorFromString(selectorString);
NSMethodSignature *methodSignature = [_moduleClass instanceMethodSignatureForSelector:_selector];
NSAssert(methodSignature, @"%@ is not a recognized Objective-C method.", _methodSignature);
// Create method invocation
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
invocation.selector = _selector;
_invocation = invocation;
}
- (SEL)selector
{
if (_selector == NULL) {
[self processMethodSignature];
}
return _selector;
}
- (void)invokeWithBridge:(YH_WatchDog *)bridge
module:(id)module
arguments:(NSArray *)arguments
{
if (!_invocation) {
[self processMethodSignature];
}
// Set arguments
NSDictionary *input = [arguments firstObject];
[_invocation setArgument:&input atIndex:2];
[_invocation retainArguments];
// Invoke method
[_invocation invokeWithTarget:module];
}
- (NSString *)serviceName
{
if (_selector == NULL) {
[self processMethodSignature];
}
return [NSString stringWithFormat:@"-[%@ %@]", _moduleClass, NSStringFromSelector(_selector)];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p; exports %@;>", [self class], self, [self serviceName]];
}
- (NSString *)debugDescription
{
return [NSString stringWithFormat:@"\n serviceId %@: %p, \n methodSignature: %@", [self serviceName], self, _methodSignature];
}
@end