Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2016 gRPC authors. |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [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 |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [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. |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #import "GRXImmediateSingleWriter.h" |
| 20 | |
| 21 | @implementation GRXImmediateSingleWriter { |
| 22 | id _value; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 23 | id<GRXWriteable> _writeable; |
| 24 | } |
| 25 | |
| 26 | @synthesize state = _state; |
| 27 | |
Muxi Yan | c2e53b5 | 2017-03-22 14:30:16 -0700 | [diff] [blame] | 28 | - (instancetype)initWithValue:(id)value { |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 29 | if (self = [super init]) { |
| 30 | _value = value; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 31 | _state = GRXWriterStateNotStarted; |
| 32 | } |
| 33 | return self; |
| 34 | } |
| 35 | |
| 36 | + (GRXWriter *)writerWithValue:(id)value { |
Muxi Yan | c2e53b5 | 2017-03-22 14:30:16 -0700 | [diff] [blame] | 37 | return [[self alloc] initWithValue:value]; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | - (void)startWithWriteable:(id<GRXWriteable>)writeable { |
| 41 | _state = GRXWriterStateStarted; |
| 42 | _writeable = writeable; |
| 43 | [writeable writeValue:_value]; |
Muxi Yan | c2e53b5 | 2017-03-22 14:30:16 -0700 | [diff] [blame] | 44 | [self finish]; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Muxi Yan | c2e53b5 | 2017-03-22 14:30:16 -0700 | [diff] [blame] | 47 | - (void)finish { |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 48 | _state = GRXWriterStateFinished; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 49 | _value = nil; |
| 50 | id<GRXWriteable> writeable = _writeable; |
| 51 | _writeable = nil; |
Muxi Yan | c2e53b5 | 2017-03-22 14:30:16 -0700 | [diff] [blame] | 52 | [writeable writesFinishedWithError:nil]; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Muxi Yan | c2e53b5 | 2017-03-22 14:30:16 -0700 | [diff] [blame] | 55 | // Overwrite the setter to disallow manual state transition. The getter |
| 56 | // of _state is synthesized. |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 57 | - (void)setState:(GRXWriterState)newState { |
| 58 | // Manual state transition is not allowed |
| 59 | return; |
| 60 | } |
| 61 | |
Muxi Yan | 01bbf87 | 2017-03-10 15:02:28 -0800 | [diff] [blame] | 62 | // Overrides [requestWriter(Transformations):map:] for Protocol Buffers |
| 63 | // encoding. |
Muxi Yan | 29a912d | 2017-03-27 15:14:23 -0700 | [diff] [blame] | 64 | // We need the return value of this map to be a GRXImmediateSingleWriter but |
| 65 | // the original \a map function returns a new Writer of another type. So we |
| 66 | // need to override this function here. |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 67 | - (GRXWriter *)map:(id (^)(id))map { |
Muxi Yan | 01bbf87 | 2017-03-10 15:02:28 -0800 | [diff] [blame] | 68 | // Since _value is available when creating the object, we can simply |
| 69 | // apply the map and store the output. |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 70 | _value = map(_value); |
| 71 | return self; |
| 72 | } |
| 73 | |
Muxi Yan | 29a912d | 2017-03-27 15:14:23 -0700 | [diff] [blame] | 74 | @end |