blob: 3e522ef24e0267f22772459ab7ab144b61ce760a [file] [log] [blame]
Jorge Canizales7b52c982015-07-16 18:41:21 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Jorge Canizales7b52c982015-07-16 18:41:21 -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
Jorge Canizales7b52c982015-07-16 18:41:21 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Jorge Canizales7b52c982015-07-16 18:41:21 -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.
Jorge Canizales7b52c982015-07-16 18:41:21 -070016 *
17 */
18
19#import "GRXForwardingWriter.h"
20
Muxi Yanc92d90a2018-04-11 18:10:02 -070021@interface GRXForwardingWriter ()<GRXWriteable>
Jorge Canizales7b52c982015-07-16 18:41:21 -070022@end
23
24@implementation GRXForwardingWriter {
Jorge Canizales56047122015-07-17 12:18:08 -070025 GRXWriter *_writer;
Jorge Canizales7b52c982015-07-16 18:41:21 -070026 id<GRXWriteable> _writeable;
27}
28
29- (instancetype)init {
30 return [self initWithWriter:nil];
31}
32
33// Designated initializer
Jorge Canizales56047122015-07-17 12:18:08 -070034- (instancetype)initWithWriter:(GRXWriter *)writer {
Jorge Canizales7b52c982015-07-16 18:41:21 -070035 if (!writer) {
Jorge Canizalesc678c302015-08-07 18:28:29 -070036 return nil;
37 }
38 if (writer.state != GRXWriterStateNotStarted) {
Jorge Canizalesfd51dff2015-08-08 16:12:52 -070039 [NSException raise:NSInvalidArgumentException
40 format:@"The writer argument must not have already started."];
Jorge Canizales7b52c982015-07-16 18:41:21 -070041 }
42 if ((self = [super init])) {
43 _writer = writer;
44 }
45 return self;
46}
47
48// This is used to send a completion or an error to the writeable. It nillifies
49// our reference to it in order to guarantee no more messages are sent to it,
50// and to release it.
51- (void)finishOutputWithError:(NSError *)errorOrNil {
52 id<GRXWriteable> writeable = _writeable;
53 _writeable = nil;
54 [writeable writesFinishedWithError:errorOrNil];
55}
56
57// This is used to stop the input writer. It nillifies our reference to it
58// to release it.
59- (void)finishInput {
Jorge Canizales56047122015-07-17 12:18:08 -070060 GRXWriter *writer = _writer;
Jorge Canizales7b52c982015-07-16 18:41:21 -070061 _writer = nil;
62 writer.state = GRXWriterStateFinished;
63}
64
65#pragma mark GRXWriteable implementation
66
67- (void)writeValue:(id)value {
68 [_writeable writeValue:value];
69}
70
71- (void)writesFinishedWithError:(NSError *)errorOrNil {
72 _writer = nil;
73 [self finishOutputWithError:errorOrNil];
74}
75
76#pragma mark GRXWriter implementation
77
78- (GRXWriterState)state {
79 return _writer ? _writer.state : GRXWriterStateFinished;
80}
81
82- (void)setState:(GRXWriterState)state {
83 if (state == GRXWriterStateFinished) {
84 _writeable = nil;
85 [self finishInput];
86 } else {
87 _writer.state = state;
88 }
89}
90
91- (void)startWithWriteable:(id<GRXWriteable>)writeable {
92 _writeable = writeable;
93 [_writer startWithWriteable:self];
94}
95
96- (void)finishWithError:(NSError *)errorOrNil {
97 [self finishOutputWithError:errorOrNil];
98 [self finishInput];
99}
100
101@end