blob: 0096c996d4c57fd0048c76a12a3c74597c4e7d2d [file] [log] [blame]
Muxi Yana40ccd82016-11-05 21:39:44 -07001/*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#import "GRXImmediateSingleWriter.h"
35
36@implementation GRXImmediateSingleWriter {
37 id _value;
Muxi Yana40ccd82016-11-05 21:39:44 -070038 id<GRXWriteable> _writeable;
39}
40
41@synthesize state = _state;
42
Muxi Yanc2e53b52017-03-22 14:30:16 -070043- (instancetype)initWithValue:(id)value {
Muxi Yana40ccd82016-11-05 21:39:44 -070044 if (self = [super init]) {
45 _value = value;
Muxi Yana40ccd82016-11-05 21:39:44 -070046 _state = GRXWriterStateNotStarted;
47 }
48 return self;
49}
50
51+ (GRXWriter *)writerWithValue:(id)value {
Muxi Yanc2e53b52017-03-22 14:30:16 -070052 return [[self alloc] initWithValue:value];
Muxi Yana40ccd82016-11-05 21:39:44 -070053}
54
55- (void)startWithWriteable:(id<GRXWriteable>)writeable {
56 _state = GRXWriterStateStarted;
57 _writeable = writeable;
58 [writeable writeValue:_value];
Muxi Yanc2e53b52017-03-22 14:30:16 -070059 [self finish];
Muxi Yana40ccd82016-11-05 21:39:44 -070060}
61
Muxi Yanc2e53b52017-03-22 14:30:16 -070062- (void)finish {
Muxi Yana40ccd82016-11-05 21:39:44 -070063 _state = GRXWriterStateFinished;
Muxi Yana40ccd82016-11-05 21:39:44 -070064 _value = nil;
65 id<GRXWriteable> writeable = _writeable;
66 _writeable = nil;
Muxi Yanc2e53b52017-03-22 14:30:16 -070067 [writeable writesFinishedWithError:nil];
Muxi Yana40ccd82016-11-05 21:39:44 -070068}
69
Muxi Yanc2e53b52017-03-22 14:30:16 -070070// Overwrite the setter to disallow manual state transition. The getter
71// of _state is synthesized.
Muxi Yana40ccd82016-11-05 21:39:44 -070072- (void)setState:(GRXWriterState)newState {
73 // Manual state transition is not allowed
74 return;
75}
76
Muxi Yan01bbf872017-03-10 15:02:28 -080077// Overrides [requestWriter(Transformations):map:] for Protocol Buffers
78// encoding.
Muxi Yan29a912d2017-03-27 15:14:23 -070079// We need the return value of this map to be a GRXImmediateSingleWriter but
80// the original \a map function returns a new Writer of another type. So we
81// need to override this function here.
Muxi Yana40ccd82016-11-05 21:39:44 -070082- (GRXWriter *)map:(id (^)(id))map {
Muxi Yan01bbf872017-03-10 15:02:28 -080083 // Since _value is available when creating the object, we can simply
84 // apply the map and store the output.
Muxi Yana40ccd82016-11-05 21:39:44 -070085 _value = map(_value);
86 return self;
87}
88
Muxi Yan29a912d2017-03-27 15:14:23 -070089@end