Skip to content

Commit

Permalink
Merge pull request alibaba#88 from ZoMinster/module_service_opt
Browse files Browse the repository at this point in the history
模块和service增加销毁方法,优化bhrouter中invocation返回值获取
  • Loading branch information
SoXeon committed Aug 12, 2017
2 parents a72a818 + 2b03921 commit 6211166
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 39 deletions.
2 changes: 2 additions & 0 deletions BeeHive/BHContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ typedef enum

- (void)addServiceWithImplInstance:(id)implInstance serviceName:(NSString *)serviceName;

- (void)removeServiceWithServiceName:(NSString *)serviceName;

- (id)getServiceInstanceFromServiceName:(NSString *)serviceName;

@end
5 changes: 5 additions & 0 deletions BeeHive/BHContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ - (void)addServiceWithImplInstance:(id)implInstance serviceName:(NSString *)serv
[[BHContext shareInstance].servicesByName setObject:implInstance forKey:serviceName];
}

- (void)removeServiceWithServiceName:(NSString *)serviceName
{
[[BHContext shareInstance].servicesByName removeObjectForKey:serviceName];
}

- (id)getServiceInstanceFromServiceName:(NSString *)serviceName
{
return [[BHContext shareInstance].servicesByName objectForKey:serviceName];
Expand Down
2 changes: 2 additions & 0 deletions BeeHive/BHModuleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ typedef NS_ENUM(NSInteger, BHModuleEventType)
- (void)registerDynamicModule:(Class)moduleClass
shouldTriggerInitEvent:(BOOL)shouldTriggerInitEvent;

- (void)unRegisterDynamicModule:(Class)moduleClass;

- (void)loadLocalModules;

- (void)registedAllModules;
Expand Down
58 changes: 52 additions & 6 deletions BeeHive/BHModuleManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define kModuleArrayKey @"moduleClasses"
#define kModuleInfoNameKey @"moduleClass"
#define kModuleInfoLevelKey @"moduleLevel"
#define kModuleInfoPriorityKey @"modulePriority"

static NSString *kSetupSelector = @"modSetUp:";
static NSString *kInitSelector = @"modInit:";
Expand Down Expand Up @@ -96,14 +97,48 @@ - (void)registerDynamicModule:(Class)moduleClass
[self addModuleFromObject:moduleClass shouldTriggerInitEvent:shouldTriggerInitEvent];
}

- (void)unRegisterDynamicModule:(Class)moduleClass {
if (!moduleClass) {
return;
}
[self.BHModuleInfos filterUsingPredicate:[NSPredicate predicateWithFormat:@"%@!=%@", kModuleInfoNameKey, NSStringFromClass(moduleClass)]];
__block NSInteger index = -1;
[self.BHModules enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:moduleClass]) {
index = idx;
*stop = YES;
}
}];
if (index >= 0) {
[self.BHModules removeObjectAtIndex:index];
}
[self.BHModulesByEvent enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, NSMutableArray<id<BHModuleProtocol>> * _Nonnull obj, BOOL * _Nonnull stop) {
__block NSInteger index = -1;
[obj enumerateObjectsUsingBlock:^(id<BHModuleProtocol> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:moduleClass]) {
index = idx;
*stop = NO;
}
}];
if (index >= 0) {
[obj removeObjectAtIndex:index];
}
}];
}

- (void)registedAllModules
{

[self.BHModuleInfos sortUsingComparator:^NSComparisonResult(NSDictionary *module1, NSDictionary *module2) {
NSNumber *module1Level = (NSNumber *)[module1 objectForKey:kModuleInfoLevelKey];
NSNumber *module2Level = (NSNumber *)[module2 objectForKey:kModuleInfoLevelKey];

return [module1Level intValue] > [module2Level intValue];
NSNumber *module1Level = (NSNumber *)[module1 objectForKey:kModuleInfoLevelKey];
NSNumber *module2Level = (NSNumber *)[module2 objectForKey:kModuleInfoLevelKey];
if (module1Level.integerValue != module2Level.integerValue) {
return module1Level.integerValue > module2Level.integerValue;
} else {
NSNumber *module1Priority = (NSNumber *)[module1 objectForKey:kModuleInfoPriorityKey];
NSNumber *module2Priority = (NSNumber *)[module2 objectForKey:kModuleInfoPriorityKey];
return module1Priority.integerValue < module2Priority.integerValue;
}
}];

NSMutableArray *tmpArray = [NSMutableArray array];
Expand Down Expand Up @@ -234,8 +269,19 @@ - (void)addModuleFromObject:(id)object
if ([moduleInstance2 respondsToSelector:@selector(basicModuleLevel)]) {
module2Level = @(BHModuleBasic);
}

return [module1Level intValue] > [module2Level intValue];
if (module1Level.integerValue != module2Level.integerValue) {
return module1Level.integerValue > module2Level.integerValue;
} else {
NSInteger module1Priority = 0;
NSInteger module2Priority = 0;
if ([moduleInstance1 respondsToSelector:@selector(modulePriority)]) {
module1Priority = [moduleInstance1 modulePriority];
}
if ([moduleInstance2 respondsToSelector:@selector(modulePriority)]) {
module2Priority = [moduleInstance2 modulePriority];
}
return module1Priority < module2Priority;
}
}];
[self registerEventsByModuleInstance:moduleInstance];

Expand Down
2 changes: 2 additions & 0 deletions BeeHive/BHModuleProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
//如果不去设置Level默认是Normal
//basicModuleLevel不去实现默认Normal
- (void)basicModuleLevel;
//越大越优先
- (NSInteger)modulePriority;

- (BOOL)async;

Expand Down
122 changes: 89 additions & 33 deletions BeeHive/BHRouter.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,84 @@
#import "BHModuleManager.h"
#import "BHServiceManager.h"

@interface NSObject (BHRetType)

+ (id)bh_getReturnFromInv:(NSInvocation *)inv withSig:(NSMethodSignature *)sig;

@end

@implementation NSObject (BHRetType)

+ (id)bh_getReturnFromInv:(NSInvocation *)inv withSig:(NSMethodSignature *)sig {
NSUInteger length = [sig methodReturnLength];
if (length == 0) return nil;

char *type = (char *)[sig methodReturnType];
while (*type == 'r' || // const
*type == 'n' || // in
*type == 'N' || // inout
*type == 'o' || // out
*type == 'O' || // bycopy
*type == 'R' || // byref
*type == 'V') { // oneway
type++; // cutoff useless prefix
}

#define return_with_number(_type_) \
do { \
_type_ ret; \
[inv getReturnValue:&ret]; \
return @(ret); \
} while (0)

switch (*type) {
case 'v': return nil; // void
case 'B': return_with_number(bool);
case 'c': return_with_number(char);
case 'C': return_with_number(unsigned char);
case 's': return_with_number(short);
case 'S': return_with_number(unsigned short);
case 'i': return_with_number(int);
case 'I': return_with_number(unsigned int);
case 'l': return_with_number(int);
case 'L': return_with_number(unsigned int);
case 'q': return_with_number(long long);
case 'Q': return_with_number(unsigned long long);
case 'f': return_with_number(float);
case 'd': return_with_number(double);
case 'D': { // long double
long double ret;
[inv getReturnValue:&ret];
return [NSNumber numberWithDouble:ret];
};

case '@': { // id
id ret = nil;
[inv getReturnValue:&ret];
return ret;
};

case '#': { // Class
Class ret = nil;
[inv getReturnValue:&ret];
return ret;
};

default: { // struct / union / SEL / void* / unknown
const char *objCType = [sig methodReturnType];
char *buf = calloc(1, length);
if (!buf) return nil;
[inv getReturnValue:buf];
NSValue *value = [NSValue valueWithBytes:buf objCType:objCType];
free(buf);
return value;
};
}
#undef return_with_number
}

@end

static NSString *const BHRClassRegex = @"(?<=T@\")(.*)(?=\",)";

typedef NS_ENUM(NSUInteger, BHRViewControlerEnterMode) {
Expand Down Expand Up @@ -513,12 +591,12 @@ + (id)safePerformAction:(SEL)action
forTarget:(NSObject *)target
withParams:(NSDictionary *)params
{
NSMethodSignature* methodSig = [target methodSignatureForSelector:action];
if(methodSig == nil) {
return nil;
}
const char* retType = [methodSig methodReturnType];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSig];
NSMethodSignature * sig = [self methodSignatureForSelector:action];
if (!sig) { return nil; }
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
if (!inv) { return nil; }
[inv setTarget:target];
[inv setSelector:action];
NSArray<NSString *> *keys = params.allKeys;
keys = [keys sortedArrayUsingComparator:^NSComparisonResult(NSString * _Nonnull obj1, NSString * _Nonnull obj2) {
if (obj1.integerValue < obj2.integerValue) {
Expand All @@ -531,34 +609,12 @@ + (id)safePerformAction:(SEL)action
}];
[keys enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
id value = params[obj];
[invocation setArgument:&value atIndex:idx+2];
[inv setArgument:&value atIndex:idx+2];
}];
[invocation setSelector:action];
[invocation setTarget:target];
[invocation invoke];
if (strcmp(retType, @encode(void)) == 0) {
return nil;
} else if (strcmp(retType, @encode(NSInteger)) == 0) {
NSInteger result = 0;
[invocation getReturnValue:&result];
return @(result);
} else if (strcmp(retType, @encode(BOOL)) == 0) {
BOOL result = NO;
[invocation getReturnValue:&result];
return @(result);
} else if (strcmp(retType, @encode(CGFloat)) == 0) {
CGFloat result = 0;
[invocation getReturnValue:&result];
return @(result);
} else if (strcmp(retType, @encode(NSUInteger)) == 0) {
NSUInteger result = 0;
[invocation getReturnValue:&result];
return @(result);
} else {
id result = nil;
[invocation getReturnValue:&result];
return result;
}
[inv invoke];
return [NSObject bh_getReturnFromInv:inv withSig:sig];
}

@end


4 changes: 4 additions & 0 deletions BeeHive/BeeHive.bundle/BeeHive.plist
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
<string>HomeModule</string>
<key>moduleLevel</key>
<integer>1</integer>
<key>modulePriority</key>
<string>600</string>
</dict>
<dict>
<key>moduleClass</key>
<string>TMTradeAdapter</string>
<key>moduleLevel</key>
<integer>1</integer>
<key>modulePriority</key>
<string>599</string>
</dict>
</array>
</dict>
Expand Down

0 comments on commit 6211166

Please sign in to comment.