Move issuance of response in helper functions

pull/16190/head
Muxi Yan 6 years ago
parent 7b08066d8f
commit 5d16c2ff92
  1. 54
      src/objective-c/GRPCClient/GRPCCall.m

@ -141,25 +141,16 @@ const char *kCFStreamVarName = "grpc_cfstream";
id<GRXWriteable> responseWriteable = [[GRXWriteable alloc] initWithValueHandler:^(id value) {
dispatch_async(self->_dispatchQueue, ^{
if (self->_handler) {
id<GRPCResponseHandler> handler = self->_handler;
NSDictionary *headers = nil;
if (!self->_initialMetadataPublished) {
headers = self->_call.responseHeaders;
self->_initialMetadataPublished = YES;
}
if (headers) {
dispatch_async(handler.dispatchQueue, ^{
if ([handler respondsToSelector:@selector(receivedInitialMetadata:)]) {
[handler receivedInitialMetadata:headers];
}
});
[self issueInitialMetadata:headers];
}
if (value) {
dispatch_async(handler.dispatchQueue, ^{
if ([handler respondsToSelector:@selector(receivedMessage:)]) {
[handler receivedMessage:value];
}
});
[self issueMessage:value];
}
}
});
@ -167,24 +158,15 @@ const char *kCFStreamVarName = "grpc_cfstream";
completionHandler:^(NSError *errorOrNil) {
dispatch_async(self->_dispatchQueue, ^{
if (self->_handler) {
id<GRPCResponseHandler> handler = self->_handler;
NSDictionary *headers = nil;
if (!self->_initialMetadataPublished) {
headers = self->_call.responseHeaders;
self->_initialMetadataPublished = YES;
}
if (headers) {
dispatch_async(handler.dispatchQueue, ^{
if ([handler respondsToSelector:@selector(receivedInitialMetadata:)]) {
[handler receivedInitialMetadata:headers];
}
});
[self issueInitialMetadata:headers];
}
dispatch_async(handler.dispatchQueue, ^{
if ([handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) {
[handler closedWithTrailingMetadata:self->_call.responseTrailers error:errorOrNil];
}
});
[self issueClosedWithTrailingMetadata:self->_call.responseTrailers error:errorOrNil];
}
});
}];
@ -230,6 +212,34 @@ const char *kCFStreamVarName = "grpc_cfstream";
});
}
- (void)issueInitialMetadata:(NSDictionary *)initialMetadata {
id<GRPCResponseHandler> handler = self->_handler;
if ([handler respondsToSelector:@selector(receivedInitialMetadata:)]) {
dispatch_async(handler.dispatchQueue, ^{
[handler receivedInitialMetadata:initialMetadata];
});
}
}
- (void)issueMessage:(id)message {
id<GRPCResponseHandler> handler = self->_handler;
if ([handler respondsToSelector:@selector(receivedMessage:)]) {
dispatch_async(handler.dispatchQueue, ^{
[handler receivedMessage:message];
});
}
}
- (void)issueClosedWithTrailingMetadata:(NSDictionary *)trailingMetadata
error:(NSError *)error {
id<GRPCResponseHandler> handler = self->_handler;
if ([handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) {
dispatch_async(handler.dispatchQueue, ^{
[handler closedWithTrailingMetadata:self->_call.responseTrailers error:error];
});
}
}
@end
// The following methods of a C gRPC call object aren't reentrant, and thus

Loading…
Cancel
Save