Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2016, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #import "GRXImmediateSingleWriter.h" |
| 35 | |
| 36 | @implementation GRXImmediateSingleWriter { |
| 37 | id _value; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 38 | id<GRXWriteable> _writeable; |
| 39 | } |
| 40 | |
| 41 | @synthesize state = _state; |
| 42 | |
Muxi Yan | c2e53b5 | 2017-03-22 14:30:16 -0700 | [diff] [blame] | 43 | - (instancetype)initWithValue:(id)value { |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 44 | if (self = [super init]) { |
| 45 | _value = value; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 46 | _state = GRXWriterStateNotStarted; |
| 47 | } |
| 48 | return self; |
| 49 | } |
| 50 | |
| 51 | + (GRXWriter *)writerWithValue:(id)value { |
Muxi Yan | c2e53b5 | 2017-03-22 14:30:16 -0700 | [diff] [blame] | 52 | return [[self alloc] initWithValue:value]; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | - (void)startWithWriteable:(id<GRXWriteable>)writeable { |
| 56 | _state = GRXWriterStateStarted; |
| 57 | _writeable = writeable; |
| 58 | [writeable writeValue:_value]; |
Muxi Yan | c2e53b5 | 2017-03-22 14:30:16 -0700 | [diff] [blame] | 59 | [self finish]; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Muxi Yan | c2e53b5 | 2017-03-22 14:30:16 -0700 | [diff] [blame] | 62 | - (void)finish { |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 63 | _state = GRXWriterStateFinished; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 64 | _value = nil; |
| 65 | id<GRXWriteable> writeable = _writeable; |
| 66 | _writeable = nil; |
Muxi Yan | c2e53b5 | 2017-03-22 14:30:16 -0700 | [diff] [blame] | 67 | [writeable writesFinishedWithError:nil]; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Muxi Yan | c2e53b5 | 2017-03-22 14:30:16 -0700 | [diff] [blame] | 70 | // Overwrite the setter to disallow manual state transition. The getter |
| 71 | // of _state is synthesized. |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 72 | - (void)setState:(GRXWriterState)newState { |
| 73 | // Manual state transition is not allowed |
| 74 | return; |
| 75 | } |
| 76 | |
Muxi Yan | 01bbf87 | 2017-03-10 15:02:28 -0800 | [diff] [blame] | 77 | // Overrides [requestWriter(Transformations):map:] for Protocol Buffers |
| 78 | // encoding. |
Muxi Yan | 29a912d | 2017-03-27 15:14:23 -0700 | [diff] [blame] | 79 | // We need the return value of this map to be a GRXImmediateSingleWriter but |
| 80 | // the original \a map function returns a new Writer of another type. So we |
| 81 | // need to override this function here. |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 82 | - (GRXWriter *)map:(id (^)(id))map { |
Muxi Yan | 01bbf87 | 2017-03-10 15:02:28 -0800 | [diff] [blame] | 83 | // Since _value is available when creating the object, we can simply |
| 84 | // apply the map and store the output. |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 85 | _value = map(_value); |
| 86 | return self; |
| 87 | } |
| 88 | |
Muxi Yan | 29a912d | 2017-03-27 15:14:23 -0700 | [diff] [blame] | 89 | @end |