1/* 2 * 3 * Copyright 2016 gRPC authors. 4 * 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 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 * 17 */ 18 19#import "GRXImmediateSingleWriter.h" 20 21@implementation GRXImmediateSingleWriter { 22 id _value; 23 id<GRXWriteable> _writeable; 24} 25 26@synthesize state = _state; 27 28- (instancetype)initWithValue:(id)value { 29 if (self = [super init]) { 30 _value = value; 31 _state = GRXWriterStateNotStarted; 32 } 33 return self; 34} 35 36+ (GRXWriter *)writerWithValue:(id)value { 37 return [[self alloc] initWithValue:value]; 38} 39 40- (void)startWithWriteable:(id<GRXWriteable>)writeable { 41 _state = GRXWriterStateStarted; 42 _writeable = writeable; 43 [writeable writeValue:_value]; 44 [self finish]; 45} 46 47- (void)finish { 48 _state = GRXWriterStateFinished; 49 _value = nil; 50 id<GRXWriteable> writeable = _writeable; 51 _writeable = nil; 52 [writeable writesFinishedWithError:nil]; 53} 54 55// Overwrite the setter to disallow manual state transition. The getter 56// of _state is synthesized. 57- (void)setState:(GRXWriterState)newState { 58 // Manual state transition is not allowed 59 return; 60} 61 62// Overrides [requestWriter(Transformations):map:] for Protocol Buffers 63// encoding. 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. 67- (GRXWriter *)map:(id (^)(id))map { 68 // Since _value is available when creating the object, we can simply 69 // apply the map and store the output. 70 _value = map(_value); 71 return self; 72} 73 74@end 75