blob: 763f107678482c5ced2c851a22d36ad6700b7293 [file] [log] [blame]
Jorge Canizales142acc92015-05-15 18:43:34 -07001/*
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 Yan91d7bb02017-05-10 14:26:40 -070041 dispatch_queue_t _writeQueue;
Jorge Canizales142acc92015-05-15 18:43:34 -070042}
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 Yan91d7bb02017-05-10 14:26:40 -070054 _writeQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
Jorge Canizales142acc92015-05-15 18:43:34 -070055 }
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 Yan91d7bb02017-05-10 14:26:40 -070066 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 Canizales142acc92015-05-15 18:43:34 -070083}
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 Yan91d7bb02017-05-10 14:26:40 -070089 BOOL started;
90 @synchronized (self) {
91 started = (_state == GRXWriterStateStarted);
92 }
93 return _state == started && _queue.count == 0;
Jorge Canizales142acc92015-05-15 18:43:34 -070094}
95
Jorge Canizalesa90a9c32015-05-18 17:12:41 -070096- (void)writeValue:(id)value {
Muxi Yan91d7bb02017-05-10 14:26:40 -070097 if ([value respondsToSelector:@selector(copy)]) {
98 value = [value copy];
Jorge Canizales142acc92015-05-15 18:43:34 -070099 }
Muxi Yan91d7bb02017-05-10 14:26:40 -0700100 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 Canizales142acc92015-05-15 18:43:34 -0700111}
112
Jorge Canizalesb2c300c2015-05-18 17:19:16 -0700113- (void)writesFinishedWithError:(NSError *)errorOrNil {
Jorge Canizales142acc92015-05-15 18:43:34 -0700114 _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 Canizalesb2c300c2015-05-18 17:19:16 -0700161 [writeable writesFinishedWithError:errorOrNil];
Jorge Canizales142acc92015-05-15 18:43:34 -0700162}
163
164@end