Jorge Canizales | e8304d5 | 2015-02-17 19:50:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Jorge Canizales | e8304d5 | 2015-02-17 19:50:51 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * 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 Canizales | e8304d5 | 2015-02-17 19:50:51 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jorge Canizales | e8304d5 | 2015-02-17 19:50:51 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * 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 Canizales | e8304d5 | 2015-02-17 19:50:51 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Jorge Canizales | 30697c9 | 2015-02-17 17:09:14 -0800 | [diff] [blame] | 19 | #import "GRXWriteable.h" |
| 20 | |
| 21 | @implementation GRXWriteable { |
| 22 | GRXValueHandler _valueHandler; |
| 23 | GRXCompletionHandler _completionHandler; |
| 24 | } |
| 25 | |
Jorge Canizales | f95ddba | 2015-08-12 10:51:56 -0700 | [diff] [blame] | 26 | + (instancetype)writeableWithSingleHandler:(GRXSingleHandler)handler { |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 27 | if (!handler) { |
| 28 | return [[self alloc] init]; |
| 29 | } |
Jorge Canizales | 5e67232 | 2016-03-09 13:34:28 -0800 | [diff] [blame] | 30 | // 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 Canizales | 5a8ec75 | 2016-03-11 18:41:54 -0800 | [diff] [blame] | 56 | static NSString *kGRPCErrorDomain = @"io.grpc"; |
| 57 | static NSUInteger kGRPCErrorCodeInternal = 13; |
| 58 | singleHandler(nil, [NSError errorWithDomain:kGRPCErrorDomain |
| 59 | code:kGRPCErrorCodeInternal |
| 60 | userInfo:userInfo]); |
Jorge Canizales | 5e67232 | 2016-03-09 13:34:28 -0800 | [diff] [blame] | 61 | } |
| 62 | }; |
| 63 | return [self writeableWithEventHandler:^(BOOL done, id value, NSError *error) { |
| 64 | if (eventHandler) { |
| 65 | eventHandler(done, value, error); |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 66 | } |
| 67 | }]; |
| 68 | } |
| 69 | |
Jorge Canizales | f95ddba | 2015-08-12 10:51:56 -0700 | [diff] [blame] | 70 | + (instancetype)writeableWithEventHandler:(GRXEventHandler)handler { |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 71 | 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 Canizales | 30697c9 | 2015-02-17 17:09:14 -0800 | [diff] [blame] | 81 | - (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 Canizales | a90a9c3 | 2015-05-18 17:12:41 -0700 | [diff] [blame] | 95 | - (void)writeValue:(id)value { |
Jorge Canizales | 30697c9 | 2015-02-17 17:09:14 -0800 | [diff] [blame] | 96 | if (_valueHandler) { |
| 97 | _valueHandler(value); |
| 98 | } |
| 99 | } |
| 100 | |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 101 | - (void)writesFinishedWithError:(NSError *)errorOrNil { |
Jorge Canizales | 30697c9 | 2015-02-17 17:09:14 -0800 | [diff] [blame] | 102 | if (_completionHandler) { |
| 103 | _completionHandler(errorOrNil); |
| 104 | } |
| 105 | } |
| 106 | @end |