Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 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 "GRXBufferedPipe.h" |
| 35 | |
| 36 | @implementation GRXBufferedPipe { |
| 37 | id<GRXWriteable> _writeable; |
| 38 | NSMutableArray *_queue; |
| 39 | BOOL _inputIsFinished; |
| 40 | NSError *_errorOrNil; |
| 41 | } |
| 42 | |
| 43 | @synthesize state = _state; |
| 44 | |
| 45 | + (instancetype)pipe { |
| 46 | return [[self alloc] init]; |
| 47 | } |
| 48 | |
| 49 | - (instancetype)init { |
| 50 | if (self = [super init]) { |
| 51 | _queue = [NSMutableArray array]; |
| 52 | _state = GRXWriterStateNotStarted; |
| 53 | } |
| 54 | return self; |
| 55 | } |
| 56 | |
| 57 | - (id)popValue { |
| 58 | id value = _queue[0]; |
| 59 | [_queue removeObjectAtIndex:0]; |
| 60 | return value; |
| 61 | } |
| 62 | |
| 63 | - (void)writeBufferUntilPausedOrStopped { |
| 64 | while (_state == GRXWriterStateStarted && _queue.count > 0) { |
Jorge Canizales | a90a9c3 | 2015-05-18 17:12:41 -0700 | [diff] [blame] | 65 | [_writeable writeValue:[self popValue]]; |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 66 | } |
| 67 | if (_inputIsFinished && _queue.count == 0) { |
| 68 | // Our writer finished normally while we were paused or not-started-yet. |
| 69 | [self finishWithError:_errorOrNil]; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #pragma mark GRXWriteable implementation |
| 74 | |
| 75 | // Returns whether events can be simply propagated to the other end of the pipe. |
| 76 | - (BOOL)shouldFastForward { |
| 77 | return _state == GRXWriterStateStarted && _queue.count == 0; |
| 78 | } |
| 79 | |
Jorge Canizales | a90a9c3 | 2015-05-18 17:12:41 -0700 | [diff] [blame] | 80 | - (void)writeValue:(id)value { |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 81 | if (self.shouldFastForward) { |
| 82 | // Skip the queue. |
Jorge Canizales | a90a9c3 | 2015-05-18 17:12:41 -0700 | [diff] [blame] | 83 | [_writeable writeValue:value]; |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 84 | } else { |
| 85 | // Even if we're paused and with enqueued values, we can't excert back-pressure to our writer. |
| 86 | // So just buffer the new value. |
Jorge Canizales | 42e47b4 | 2015-05-18 23:38:17 -0700 | [diff] [blame] | 87 | // We need a copy, so that it doesn't mutate before it's written at the other end of the pipe. |
| 88 | if ([value respondsToSelector:@selector(copy)]) { |
| 89 | value = [value copy]; |
| 90 | } |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 91 | [_queue addObject:value]; |
| 92 | } |
| 93 | } |
| 94 | |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 95 | - (void)writesFinishedWithError:(NSError *)errorOrNil { |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 96 | _inputIsFinished = YES; |
| 97 | _errorOrNil = errorOrNil; |
| 98 | if (errorOrNil || self.shouldFastForward) { |
| 99 | // No need to write pending values. |
| 100 | [self finishWithError:_errorOrNil]; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | #pragma mark GRXWriter implementation |
| 105 | |
| 106 | - (void)setState:(GRXWriterState)newState { |
| 107 | // Manual transitions are only allowed from the started or paused states. |
| 108 | if (_state == GRXWriterStateNotStarted || _state == GRXWriterStateFinished) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | switch (newState) { |
| 113 | case GRXWriterStateFinished: |
| 114 | _state = newState; |
| 115 | _queue = nil; |
| 116 | // Per GRXWriter's contract, setting the state to Finished manually means one doesn't wish the |
| 117 | // writeable to be messaged anymore. |
| 118 | _writeable = nil; |
| 119 | return; |
| 120 | case GRXWriterStatePaused: |
| 121 | _state = newState; |
| 122 | return; |
| 123 | case GRXWriterStateStarted: |
| 124 | if (_state == GRXWriterStatePaused) { |
| 125 | _state = newState; |
| 126 | [self writeBufferUntilPausedOrStopped]; |
| 127 | } |
| 128 | return; |
| 129 | case GRXWriterStateNotStarted: |
| 130 | return; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | - (void)startWithWriteable:(id<GRXWriteable>)writeable { |
| 135 | _state = GRXWriterStateStarted; |
| 136 | _writeable = writeable; |
| 137 | [self writeBufferUntilPausedOrStopped]; |
| 138 | } |
| 139 | |
| 140 | - (void)finishWithError:(NSError *)errorOrNil { |
| 141 | id<GRXWriteable> writeable = _writeable; |
| 142 | self.state = GRXWriterStateFinished; |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 143 | [writeable writesFinishedWithError:errorOrNil]; |
Jorge Canizales | 142acc9 | 2015-05-15 18:43:34 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @end |