ActionStage
ActionStage来自于Telegram官方开源的iOS客户端。
ActionStage是一个消息通信、模块解耦的框架。
ActionStage,消息的中枢派发模块
Handle,管理Watcher,发送消息的模块
Actor,处理requestActor的模块
Watcher,观察协议,定义为接收ActionStage的消息
设计图如下
安装
pod 'ActionStage'
示例
在Example文件夹下运行pod install
使用
dispatchResource
类似于通知,
发送消息
[ActionStageInstance() dispatchResource:@"/as/unreadMessageCount" resource:[[NSNumber alloc] initWithInt:2]];
监听对应path,在回调中可以接收消息
_actionHandle = [[ASHandle alloc] initWithDelegate:self releaseOnMainThread:true];
[ActionStageInstance() watchForPaths:@[
@"/as/unreadMessageCount",
] watcher:self];
- (void)actionStageResourceDispatched:(NSString *)path resource:(id)resource arguments:(id)arguments;
{
NSLog(@"ResourceDispatch %@ %@", path, resource);
}
requestActor
requestActor
[ActionStageInstance() requestActor:@"/as/service/auth/sendCode" options:[[NSDictionary alloc] initWithObjectsAndKeys:@"123", @"phoneNumber", nil] watcher:self];
这时可以定义一个Actor处理sendCode的网络任务,例如ASTestSendCodeRequestActor。
+ (NSString *)genericPath
{
return @"/as/service/auth/sendCode";
}
- (void)execute:(NSDictionary *)options
{
NSString *phoneNumber = [options objectForKey:@"phoneNumber"];
[self sendNetwork:phoneNumber];
}
- (void)sendNetwork:(NSString *)phoneNumber
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[ActionStageInstance() actionCompleted:self.path result:nil];
});
}
actionCompleted
当Actor执行完成后,可以发起actionCompleted。
[ActionStageInstance() actionCompleted:self.path result:nil];
在Watcher可以接收消息。
- (void)actorCompleted:(int)resultCode path:(NSString *)path result:(id)result
{
NSLog(@"requestActor completed %@",path);
}
作者
coderyi, [email protected]
许可证
ActionStage遵循MIT许可证。更多信息请参阅LICENSE文件。