Debian Codegraph @

src/objective-c/RxLibrary/GRXImmediateSingleWriter.m

raw
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 }
24
25 @synthesize state = _state;
26
27 - (instancetype)initWithValue:(id)value {
28 if (self = [super init]) {
29 _value = value;
30 _state = GRXWriterStateNotStarted;
31 }
32 return self;
33 }
34
35 + (GRXWriter *)writerWithValue:(id)value {
36 return [[self alloc] initWithValue:value];
37 }
38
39 - (void)startWithWriteable:(id<GRXWriteable>)writeable {
40 id copiedValue = nil;
41 @synchronized(self) {
42 if (_state != GRXWriterStateNotStarted) {
43 return;
44 }
45 copiedValue = _value;
46 _value = nil;
47 _state = GRXWriterStateFinished;
48 }
49 [writeable writeValue:copiedValue];
50 [writeable writesFinishedWithError:nil];
51 }
52
53 // Overwrite the setter to disallow manual state transition. The getter
54 // of _state is synthesized.
55 - (void)setState:(GRXWriterState)newState {
56 // Manual state transition is not allowed
57 return;
58 }
59
60 // Overrides [requestWriter(Transformations):map:] for Protocol Buffers
61 // encoding.
62 // We need the return value of this map to be a GRXImmediateSingleWriter but
63 // the original \a map function returns a new Writer of another type. So we
64 // need to override this function here.
65 - (GRXWriter *)map:(id (^)(id))map {
66 @synchronized(self) {
67 // Since _value is available when creating the object, we can simply
68 // apply the map and store the output.
69 _value = map(_value);
70 }
71 return self;
72 }
73
74 @end