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