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

Loading…
Cancel
Save