blob: 3edae788ab900760bcbfc84a5fa201cc1993fabe [file] [log] [blame]
Jorge Canizalese8304d52015-02-17 19:50:51 -08001/*
2 *
Yang Gao5fc90292015-02-20 09:46:22 -08003 * Copyright 2015, Google Inc.
Jorge Canizalese8304d52015-02-17 19:50:51 -08004 * 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
Jorge Canizales30697c92015-02-17 17:09:14 -080034#import "GRXImmediateWriter.h"
35
36#import "NSEnumerator+GRXUtil.h"
37
38@implementation GRXImmediateWriter {
39 NSEnumerator *_enumerator;
40 NSError *_errorOrNil;
41 id<GRXWriteable> _writeable;
42}
43
44@synthesize state = _state;
45
46- (instancetype) init {
47 return [self initWithEnumerator:nil error:nil]; // results in an empty writer.
48}
49
50// Designated initializer
51- (instancetype)initWithEnumerator:(NSEnumerator *)enumerator error:(NSError *)errorOrNil {
52 if (((self = [super init]))) {
53 _enumerator = enumerator;
54 _errorOrNil = errorOrNil;
55 _state = GRXWriterStateNotStarted;
56 }
57 return self;
58}
59
60#pragma mark Convenience constructors
61
62+ (instancetype)writerWithEnumerator:(NSEnumerator *)enumerator error:(NSError *)errorOrNil {
63 return [[self alloc] initWithEnumerator:enumerator error:errorOrNil];
64}
65
Jorge Canizales56047122015-07-17 12:18:08 -070066+ (GRXWriter *)writerWithEnumerator:(NSEnumerator *)enumerator {
Jorge Canizales30697c92015-02-17 17:09:14 -080067 return [self writerWithEnumerator:enumerator error:nil];
68}
69
Jorge Canizales56047122015-07-17 12:18:08 -070070+ (GRXWriter *)writerWithValueSupplier:(id (^)())block {
Jorge Canizales30697c92015-02-17 17:09:14 -080071 return [self writerWithEnumerator:[NSEnumerator grx_enumeratorWithValueSupplier:block]];
72}
73
Jorge Canizales56047122015-07-17 12:18:08 -070074+ (GRXWriter *)writerWithContainer:(id<NSFastEnumeration>)container {
Jorge Canizales30697c92015-02-17 17:09:14 -080075 return [self writerWithEnumerator:[NSEnumerator grx_enumeratorWithContainer:container]];;
76}
77
Jorge Canizales56047122015-07-17 12:18:08 -070078+ (GRXWriter *)writerWithValue:(id)value {
Jorge Canizalesbc970ae2015-07-19 12:27:20 -070079 return [self writerWithEnumerator:[NSEnumerator grx_enumeratorWithSingleValue:value]];
Jorge Canizales30697c92015-02-17 17:09:14 -080080}
81
Jorge Canizales56047122015-07-17 12:18:08 -070082+ (GRXWriter *)writerWithError:(NSError *)error {
Jorge Canizalesbc970ae2015-07-19 12:27:20 -070083 return [self writerWithEnumerator:nil error:error];
Jorge Canizales30697c92015-02-17 17:09:14 -080084}
85
Jorge Canizales56047122015-07-17 12:18:08 -070086+ (GRXWriter *)emptyWriter {
Jorge Canizalesbc970ae2015-07-19 12:27:20 -070087 return [self writerWithEnumerator:nil error:nil];
Jorge Canizales30697c92015-02-17 17:09:14 -080088}
89
90#pragma mark Conformance with GRXWriter
91
92// Most of the complexity in this implementation is the result of supporting pause and resumption of
93// the GRXWriter. It's an important feature for instances of GRXWriter that are backed by a
94// container (which may be huge), or by a NSEnumerator (which may even be infinite).
95
96- (void)writeUntilPausedOrStopped {
97 id value;
98 while (value = [_enumerator nextObject]) {
Jorge Canizalesa90a9c32015-05-18 17:12:41 -070099 [_writeable writeValue:value];
Jorge Canizales30697c92015-02-17 17:09:14 -0800100 // If the writeable has a reference to us, it might change our state to paused or finished.
101 if (_state == GRXWriterStatePaused || _state == GRXWriterStateFinished) {
102 return;
103 }
104 }
105 [self finishWithError:_errorOrNil];
106}
107
108- (void)startWithWriteable:(id<GRXWriteable>)writeable {
109 _state = GRXWriterStateStarted;
110 _writeable = writeable;
111 [self writeUntilPausedOrStopped];
112}
113
114- (void)finishWithError:(NSError *)errorOrNil {
115 _state = GRXWriterStateFinished;
116 _enumerator = nil;
117 _errorOrNil = nil;
118 id<GRXWriteable> writeable = _writeable;
119 _writeable = nil;
Jorge Canizalesb2c300c2015-05-18 17:19:16 -0700120 [writeable writesFinishedWithError:errorOrNil];
Jorge Canizales30697c92015-02-17 17:09:14 -0800121}
122
123- (void)setState:(GRXWriterState)newState {
124 // Manual transitions are only allowed from the started or paused states.
125 if (_state == GRXWriterStateNotStarted || _state == GRXWriterStateFinished) {
126 return;
127 }
128
129 switch (newState) {
130 case GRXWriterStateFinished:
131 _state = newState;
132 _enumerator = nil;
133 _errorOrNil = nil;
134 // Per GRXWriter's contract, setting the state to Finished manually
135 // means one doesn't wish the writeable to be messaged anymore.
136 _writeable = nil;
137 return;
138 case GRXWriterStatePaused:
139 _state = newState;
140 return;
141 case GRXWriterStateStarted:
142 if (_state == GRXWriterStatePaused) {
143 _state = newState;
144 [self writeUntilPausedOrStopped];
145 }
146 return;
147 case GRXWriterStateNotStarted:
148 return;
149 }
150}
151
152@end