Skip to content

Commit

Permalink
去除class BeeHive;增加一些去重处理和排序处理,防止模块重复加载初始化,保证响应事件的顺序和配置的优先级一致
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoMinster committed Sep 5, 2017
1 parent ca48c52 commit 4cc4dff
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
4 changes: 2 additions & 2 deletions BeeHive/BHAnnotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@


#define BeeHiveMod(name) \
class BeeHive; char * k##name##_mod BeeHiveDATA(BeehiveMods) = ""#name"";
char * k##name##_mod BeeHiveDATA(BeehiveMods) = ""#name"";

#define BeeHiveService(servicename,impl) \
class BeeHive;char * k##servicename##_service BeeHiveDATA(BeehiveServices) = "{ \""#servicename"\" : \""#impl"\"}";
char * k##servicename##_service BeeHiveDATA(BeehiveServices) = "{ \""#servicename"\" : \""#impl"\"}";

@interface BHAnnotation : NSObject

Expand Down
49 changes: 40 additions & 9 deletions BeeHive/BHModuleManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define kModuleInfoNameKey @"moduleClass"
#define kModuleInfoLevelKey @"moduleLevel"
#define kModuleInfoPriorityKey @"modulePriority"
#define kModuleInfoHasInstantiatedKey @"moduleHasInstantiated"

static NSString *kSetupSelector = @"modSetUp:";
static NSString *kInitSelector = @"modInit:";
Expand Down Expand Up @@ -80,10 +81,16 @@ - (void)loadLocalModules

NSDictionary *moduleList = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

NSArray *modulesArray = [moduleList objectForKey:kModuleArrayKey];

[self.BHModuleInfos addObjectsFromArray:modulesArray];

NSArray<NSDictionary *> *modulesArray = [moduleList objectForKey:kModuleArrayKey];
NSMutableDictionary<NSString *, NSNumber *> *moduleInfoByClass = @{}.mutableCopy;
[self.BHModuleInfos enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[moduleInfoByClass setObject:@1 forKey:[obj objectForKey:kModuleInfoNameKey]];
}];
[modulesArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (!moduleInfoByClass[[obj objectForKey:kModuleInfoNameKey]]) {
[self.BHModuleInfos addObject:obj];
}
}];
}

- (void)registerDynamicModule:(Class)moduleClass
Expand Down Expand Up @@ -149,15 +156,15 @@ - (void)registedAllModules
NSString *classStr = [module objectForKey:kModuleInfoNameKey];

Class moduleClass = NSClassFromString(classStr);

if (NSStringFromClass(moduleClass)) {
BOOL hasInstantiated = ((NSNumber *)[module objectForKey:kModuleInfoHasInstantiatedKey]).boolValue;
if (NSStringFromClass(moduleClass) && !hasInstantiated) {
id<BHModuleProtocol> moduleInstance = [[moduleClass alloc] init];
[tmpArray addObject:moduleInstance];
}

}];

[self.BHModules removeAllObjects];
// [self.BHModules removeAllObjects];

[self.BHModules addObjectsFromArray:tmpArray];

Expand Down Expand Up @@ -260,6 +267,7 @@ - (void)addModuleFromObject:(id)object

id<BHModuleProtocol> moduleInstance = [[class alloc] init];
[self.BHModules addObject:moduleInstance];
[moduleInfo setObject:@(YES) forKey:kModuleInfoHasInstantiatedKey];
[self.BHModules sortUsingComparator:^NSComparisonResult(id<BHModuleProtocol> moduleInstance1, id<BHModuleProtocol> moduleInstance2) {
NSNumber *module1Level = @(BHModuleNormal);
NSNumber *module2Level = @(BHModuleNormal);
Expand Down Expand Up @@ -318,15 +326,38 @@ - (void)registerEvent:(NSInteger)eventType
return;
}
NSNumber *eventTypeNumber = @(eventType);
if (![self.BHSelectorByEvent.allKeys containsObject:eventTypeNumber]) {
if (!self.BHSelectorByEvent[eventTypeNumber]) {
[self.BHSelectorByEvent setObject:selectorStr forKey:eventTypeNumber];
}
if (![self.BHModulesByEvent.allKeys containsObject:eventTypeNumber]) {
if (!self.BHModulesByEvent[eventTypeNumber]) {
[self.BHModulesByEvent setObject:@[].mutableCopy forKey:eventTypeNumber];
}
NSMutableArray *eventModules = [self.BHModulesByEvent objectForKey:eventTypeNumber];
if (![eventModules containsObject:moduleInstance]) {
[eventModules addObject:moduleInstance];
[eventModules sortUsingComparator:^NSComparisonResult(id<BHModuleProtocol> moduleInstance1, id<BHModuleProtocol> moduleInstance2) {
NSNumber *module1Level = @(BHModuleNormal);
NSNumber *module2Level = @(BHModuleNormal);
if ([moduleInstance1 respondsToSelector:@selector(basicModuleLevel)]) {
module1Level = @(BHModuleBasic);
}
if ([moduleInstance2 respondsToSelector:@selector(basicModuleLevel)]) {
module2Level = @(BHModuleBasic);
}
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;
}
}];
}
}

Expand Down

0 comments on commit 4cc4dff

Please sign in to comment.