blob: 3126ae4bd161327ba89cd45a80f65de8c6f08a15 [file] [log] [blame]
Muxi Yana40ccd82016-11-05 21:39:44 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2016 gRPC authors.
Muxi Yana40ccd82016-11-05 21:39:44 -07004 *
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
Muxi Yana40ccd82016-11-05 21:39:44 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Muxi Yana40ccd82016-11-05 21:39:44 -070010 *
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.
Muxi Yana40ccd82016-11-05 21:39:44 -070016 *
17 */
18
19#import "GRXImmediateSingleWriter.h"
20
21@implementation GRXImmediateSingleWriter {
22 id _value;
Muxi Yana40ccd82016-11-05 21:39:44 -070023 id<GRXWriteable> _writeable;
24}
25
26@synthesize state = _state;
27
Muxi Yanc2e53b52017-03-22 14:30:16 -070028- (instancetype)initWithValue:(id)value {
Muxi Yana40ccd82016-11-05 21:39:44 -070029 if (self = [super init]) {
30 _value = value;
Muxi Yana40ccd82016-11-05 21:39:44 -070031 _state = GRXWriterStateNotStarted;
32 }
33 return self;
34}
35
36+ (GRXWriter *)writerWithValue:(id)value {
Muxi Yanc2e53b52017-03-22 14:30:16 -070037 return [[self alloc] initWithValue:value];
Muxi Yana40ccd82016-11-05 21:39:44 -070038}
39
40- (void)startWithWriteable:(id<GRXWriteable>)writeable {
41 _state = GRXWriterStateStarted;
42 _writeable = writeable;
43 [writeable writeValue:_value];
Muxi Yanc2e53b52017-03-22 14:30:16 -070044 [self finish];
Muxi Yana40ccd82016-11-05 21:39:44 -070045}
46
Muxi Yanc2e53b52017-03-22 14:30:16 -070047- (void)finish {
Muxi Yana40ccd82016-11-05 21:39:44 -070048 _state = GRXWriterStateFinished;
Muxi Yana40ccd82016-11-05 21:39:44 -070049 _value = nil;
50 id<GRXWriteable> writeable = _writeable;
51 _writeable = nil;
Muxi Yanc2e53b52017-03-22 14:30:16 -070052 [writeable writesFinishedWithError:nil];
Muxi Yana40ccd82016-11-05 21:39:44 -070053}
54
Muxi Yanc2e53b52017-03-22 14:30:16 -070055// Overwrite the setter to disallow manual state transition. The getter
56// of _state is synthesized.
Muxi Yana40ccd82016-11-05 21:39:44 -070057- (void)setState:(GRXWriterState)newState {
58 // Manual state transition is not allowed
59 return;
60}
61
Muxi Yan01bbf872017-03-10 15:02:28 -080062// Overrides [requestWriter(Transformations):map:] for Protocol Buffers
63// encoding.
Muxi Yan29a912d2017-03-27 15:14:23 -070064// 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 Yana40ccd82016-11-05 21:39:44 -070067- (GRXWriter *)map:(id (^)(id))map {
Muxi Yan01bbf872017-03-10 15:02:28 -080068 // Since _value is available when creating the object, we can simply
69 // apply the map and store the output.
Muxi Yana40ccd82016-11-05 21:39:44 -070070 _value = map(_value);
71 return self;
72}
73
Muxi Yan29a912d2017-03-27 15:14:23 -070074@end