blob: f5ed603698d2349c1afe9c9b53aaaa97b4ac47bb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#import "GRXForwardingWriter.h"
@interface GRXForwardingWriter ()<GRXWriteable>
@end
@implementation GRXForwardingWriter {
GRXWriter *_writer;
id<GRXWriteable> _writeable;
}
- (instancetype)init {
return [self initWithWriter:nil];
}
// Designated initializer
- (instancetype)initWithWriter:(GRXWriter *)writer {
if (!writer) {
return nil;
}
if (writer.state != GRXWriterStateNotStarted) {
[NSException raise:NSInvalidArgumentException
format:@"The writer argument must not have already started."];
}
if ((self = [super init])) {
_writer = writer;
}
return self;
}
// This is used to send a completion or an error to the writeable. It nillifies
// our reference to it in order to guarantee no more messages are sent to it,
// and to release it.
- (void)finishOutputWithError:(NSError *)errorOrNil {
id<GRXWriteable> writeable = _writeable;
_writeable = nil;
[writeable writesFinishedWithError:errorOrNil];
}
#pragma mark GRXWriteable implementation
- (void)writeValue:(id)value {
@synchronized(self) {
[_writeable writeValue:value];
}
}
- (void)writesFinishedWithError:(NSError *)errorOrNil {
@synchronized(self) {
_writer = nil;
[self finishOutputWithError:errorOrNil];
}
}
#pragma mark GRXWriter implementation
- (GRXWriterState)state {
return _writer ? _writer.state : GRXWriterStateFinished;
}
- (void)setState:(GRXWriterState)state {
GRXWriter *copiedWriter = nil;
if (state == GRXWriterStateFinished) {
@synchronized(self) {
_writeable = nil;
copiedWriter = _writer;
_writer = nil;
}
copiedWriter.state = GRXWriterStateFinished;
} else {
@synchronized(self) {
copiedWriter = _writer;
}
copiedWriter.state = state;
}
}
- (void)startWithWriteable:(id<GRXWriteable>)writeable {
GRXWriter *copiedWriter = nil;
@synchronized(self) {
_writeable = writeable;
copiedWriter = _writer;
}
[copiedWriter startWithWriteable:self];
}
- (void)finishWithError:(NSError *)errorOrNil {
GRXWriter *copiedWriter = nil;
@synchronized(self) {
[self finishOutputWithError:errorOrNil];
copiedWriter = _writer;
}
copiedWriter.state = GRXWriterStateFinished;
}
@end
|