OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, 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 "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 *)e
rrorOrNil { |
| 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 |
| 66 + (GRXWriter *)writerWithEnumerator:(NSEnumerator *)enumerator { |
| 67 return [self writerWithEnumerator:enumerator error:nil]; |
| 68 } |
| 69 |
| 70 + (GRXWriter *)writerWithValueSupplier:(id (^)())block { |
| 71 return [self writerWithEnumerator:[NSEnumerator grx_enumeratorWithValueSupplie
r:block]]; |
| 72 } |
| 73 |
| 74 + (GRXWriter *)writerWithContainer:(id<NSFastEnumeration>)container { |
| 75 return [self writerWithEnumerator:[NSEnumerator grx_enumeratorWithContainer:co
ntainer]];; |
| 76 } |
| 77 |
| 78 + (GRXWriter *)writerWithValue:(id)value { |
| 79 return [self writerWithEnumerator:[NSEnumerator grx_enumeratorWithSingleValue:
value]]; |
| 80 } |
| 81 |
| 82 + (GRXWriter *)writerWithError:(NSError *)error { |
| 83 return [self writerWithEnumerator:nil error:error]; |
| 84 } |
| 85 |
| 86 + (GRXWriter *)emptyWriter { |
| 87 return [self writerWithEnumerator:nil error:nil]; |
| 88 } |
| 89 |
| 90 #pragma mark Conformance with GRXWriter |
| 91 |
| 92 // Most of the complexity in this implementation is the result of supporting pau
se 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 infini
te). |
| 95 |
| 96 - (void)writeUntilPausedOrStopped { |
| 97 id value; |
| 98 while (value = [_enumerator nextObject]) { |
| 99 [_writeable writeValue:value]; |
| 100 // If the writeable has a reference to us, it might change our state to paus
ed 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; |
| 120 [writeable writesFinishedWithError:errorOrNil]; |
| 121 } |
| 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 |
OLD | NEW |