Pyramid.h 4.19 KB
//
//  Pyramid.h
//  Pyramid
//
//  Modify by jilutao on 04/01/2018.
//  Copyright © 2017 Baidu. All rights reserved.
//

#import <Foundation/Foundation.h>

#import <Pyramid/BBPContext.h>
#import <Pyramid/BBPAppSpeedMonitor.h>
#import <Pyramid/BBPApplicationDelegate.h>

#import <Pyramid/BBPModuleProtocol.h>
#import <Pyramid/BBPServiceProtocol.h>

#import <Pyramid/PyramidDefines.h>

#define PyramidInstance ([Pyramid shared])
#define PyramidContext ([Pyramid shared].context)
#define PyramidAppDelegate ([Pyramid shared].applicationDelegate)
#define PyramidCreateService(p) ([Pyramid createService:@protocol(p)])

#define AliasService(Protocol, Selector) \
@protocol Protocol; \
@protocol Pyramid_##Protocol \
+ (Class<Protocol>)Selector; \
@end \
@interface Pyramid(Protocol)<Pyramid_##Protocol> \
@end

/**
 Pyramid框架主类,包含环境变量,注册,获取和触发组件,服务和事件等功能
 */
@interface Pyramid : NSObject

// app参数存放对象
@property (nonatomic, strong) BBPContext *context;

// app事件接收
@property (nonatomic, strong) BBPApplicationDelegate *applicationDelegate;
// 开启异常崩溃,建议debug下开启
@property (nonatomic, assign) BOOL enableException;

+ (instancetype)shared;

/**
 给指定的组件注册自定义事件,触发此事件后只要注册的组件才会接收
 
 @param event 事件
 @param moduleInstance 组件对象
 */
+ (void)registerCustomEvent:(BDPPyramidEvent)event moduleInstance:(id<BBPModuleProtocol>)moduleInstance;

/**
 注销指定组件的自定义事件
 
 @param event 事件
 @param moduleInstance 组件对象
 */
+ (void)unregisterCustomEvent:(BDPPyramidEvent)event moduleInstance:(id<BBPModuleProtocol>)moduleInstance;

/**
 注销指定组件的所有自定义事件
 
 @param moduleInstance 组件对象
 */
+ (void)unregisterCustomEvent:(id<BBPModuleProtocol>)moduleInstance;

/**
 触发自定义事件
 
 @param event 事件
 */
+ (void)triggerCustomEvent:(BDPPyramidEvent)event;

/**
 触发自定义事件
 
 @param event 事件
 @param customParam 参数
 */
+ (void)triggerCustomEvent:(BDPPyramidEvent)event customParam:(id)customParam;

/**
 App环境加载完成调用,如:首页加载完成
 */
+ (void)applicationEnvironmentDidSetup;

/**
 Window加载完成调用
 */
+ (void)applicationMakeKeyWindowAndVisible;

@end

#import <Pyramid/Pyramid+Module.h>
#import <Pyramid/Pyramid+Service.h>

// 注册Service
#define registerService(Protocol, Implementation) \
do { \
    [Pyramid registerService:@protocol(Protocol) implClass:[Implementation class]]; \
    [Pyramid registerDispatcher:[Implementation class] forModuleInstance:self]; \
} while(0)

// 注册Service和别名
#define registerServiceWithAlias(Protocol, Implementation, Alias) \
do {  \
    [Pyramid registerService:@protocol(Protocol) implClass:[Implementation class]];\
    [Pyramid registerAlias:@selector(Alias) forService:@protocol(Protocol)]; \
    [Pyramid registerDispatcher:[Implementation class] forModuleInstance:self]; \
} while(0)

// 注册外部Service
#define registerServiceWithIdentifier(Protocol, Implementation, identifier) \
do {  \
    [Pyramid registerExternService:@protocol(Protocol) implClass:[Implementation class] identifier:identifier];\
} while(0)

// 定义组件
#define ModuleDefine(Module) \
__attribute__((constructor)) \
static void registerModuleFor##Module(void) { \
    [Pyramid addReadyRegisterModule:[Module class]]; \
}

// 定义Service
#define ServiceDefine(Protocol, Implementation) \
__attribute__((constructor)) \
static void registerModuleFor##MODULE(void) { \
    [Pyramid registerService:@protocol(Protocol) implClass:[Implementation class]]; \
}

// 定义Service别名
#define ServiceDefineWithAlias(Protocol, Implementation, Alias) \
__attribute__((constructor)) \
static void registerModuleFor##MODULE(void) { \
    [Pyramid registerService:@protocol(Protocol) implClass:[Implementation class]]; \
    [Pyramid registerAlias:@selector(Alias) forService:@protocol(Protocol)];\
}

// 自动Hook系统事件,此宏不调动不自动Hook,系统事件需手动派发
#define PyramidAutoDispatcherSystemEvents(void) \
__attribute__((constructor)) \
static void AutoDispatcherSystemEventsForPyramid() { \
    BBPContext.autoDispatcherSystemEvents = YES; \
}