blob: fcdf3bab40b44dedb997e014265386750bc77b11 [file] [log] [blame]
Jorge Canizalese8304d52015-02-17 19:50:51 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Jorge Canizalese8304d52015-02-17 19:50:51 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Jorge Canizalese8304d52015-02-17 19:50:51 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Jorge Canizalese8304d52015-02-17 19:50:51 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Jorge Canizalese8304d52015-02-17 19:50:51 -080016 *
17 */
18
Jorge Canizales30697c92015-02-17 17:09:14 -080019#import "GRXWriteable.h"
20
21@implementation GRXWriteable {
22 GRXValueHandler _valueHandler;
23 GRXCompletionHandler _completionHandler;
24}
25
Jorge Canizalesf95ddba2015-08-12 10:51:56 -070026+ (instancetype)writeableWithSingleHandler:(GRXSingleHandler)handler {
Jorge Canizales2779ccb2015-04-20 23:42:46 -070027 if (!handler) {
28 return [[self alloc] init];
29 }
Jorge Canizales5e672322016-03-09 13:34:28 -080030 // We nilify this variable when the block is invoked, so that handler is only invoked once even if
31 // the writer tries to write multiple values.
32 __block GRXEventHandler eventHandler = ^(BOOL done, id value, NSError *error) {
33 // Nillify eventHandler before invoking handler, in case the latter causes the former to be
34 // executed recursively. Because blocks can be deallocated even during execution, we have to
35 // first retain handler locally to guarantee it's valid.
36 // TODO(jcanizales): Just turn this craziness into a simple subclass of GRXWriteable.
37 GRXSingleHandler singleHandler = handler;
38 eventHandler = nil;
39
40 if (value) {
41 singleHandler(value, nil);
42 } else if (error) {
43 singleHandler(nil, error);
44 } else {
45 NSDictionary *userInfo = @{
46 NSLocalizedDescriptionKey: @"The writer finished without producing any value."
47 };
48 // Even though RxLibrary is independent of gRPC, the domain and code here are, for the moment,
49 // set to the values of kGRPCErrorDomain and GRPCErrorCodeInternal. This way, the error formed
50 // is the one user of gRPC would expect if the server failed to produce a response.
51 //
52 // TODO(jcanizales): Figure out a way to keep errors of RxLibrary generic without making users
53 // of gRPC take care of two different error domains and error code enums. A possibility is to
54 // add error handling to GRXWriters or GRXWriteables, and use them to translate errors between
55 // the two domains.
Jorge Canizales5a8ec752016-03-11 18:41:54 -080056 static NSString *kGRPCErrorDomain = @"io.grpc";
57 static NSUInteger kGRPCErrorCodeInternal = 13;
58 singleHandler(nil, [NSError errorWithDomain:kGRPCErrorDomain
59 code:kGRPCErrorCodeInternal
60 userInfo:userInfo]);
Jorge Canizales5e672322016-03-09 13:34:28 -080061 }
62 };
63 return [self writeableWithEventHandler:^(BOOL done, id value, NSError *error) {
64 if (eventHandler) {
65 eventHandler(done, value, error);
Jorge Canizales2779ccb2015-04-20 23:42:46 -070066 }
67 }];
68}
69
Jorge Canizalesf95ddba2015-08-12 10:51:56 -070070+ (instancetype)writeableWithEventHandler:(GRXEventHandler)handler {
Jorge Canizales2779ccb2015-04-20 23:42:46 -070071 if (!handler) {
72 return [[self alloc] init];
73 }
74 return [[self alloc] initWithValueHandler:^(id value) {
75 handler(NO, value, nil);
76 } completionHandler:^(NSError *errorOrNil) {
77 handler(YES, nil, errorOrNil);
78 }];
79}
80
Jorge Canizales30697c92015-02-17 17:09:14 -080081- (instancetype)init {
82 return [self initWithValueHandler:nil completionHandler:nil];
83}
84
85// Designated initializer
86- (instancetype)initWithValueHandler:(GRXValueHandler)valueHandler
87 completionHandler:(GRXCompletionHandler)completionHandler {
88 if ((self = [super init])) {
89 _valueHandler = valueHandler;
90 _completionHandler = completionHandler;
91 }
92 return self;
93}
94
Jorge Canizalesa90a9c32015-05-18 17:12:41 -070095- (void)writeValue:(id)value {
Jorge Canizales30697c92015-02-17 17:09:14 -080096 if (_valueHandler) {
97 _valueHandler(value);
98 }
99}
100
Jorge Canizalesb2c300c2015-05-18 17:19:16 -0700101- (void)writesFinishedWithError:(NSError *)errorOrNil {
Jorge Canizales30697c92015-02-17 17:09:14 -0800102 if (_completionHandler) {
103 _completionHandler(errorOrNil);
104 }
105}
106@end