Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Yang Gao | 5fc9029 | 2015-02-20 09:46:22 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 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 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 34 | #import <Foundation/Foundation.h> |
| 35 | |
Jorge Canizales | 4c6f778 | 2015-07-17 23:13:36 -0700 | [diff] [blame] | 36 | #import "GRXWriter.h" |
| 37 | #import "GRXWriteable.h" |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 38 | |
Jorge Canizales | b10776c | 2015-10-26 10:44:55 -0700 | [diff] [blame] | 39 | /** |
| 40 | * This is a thread-safe wrapper over a GRXWriteable instance. It lets one enqueue calls to a |
| 41 | * GRXWriteable instance for the main thread, guaranteeing that writesFinishedWithError: is the last |
| 42 | * message sent to it (no matter what messages are sent to the wrapper, in what order, nor from |
| 43 | * which thread). It also guarantees that, if cancelWithError: is called from the main thread (e.g. |
| 44 | * by the app cancelling the writes), no further messages are sent to the writeable except |
| 45 | * writesFinishedWithError:. |
| 46 | * |
| 47 | * TODO(jcanizales): Let the user specify another queue for the writeable callbacks. |
| 48 | */ |
Jorge Canizales | 35f003b | 2015-07-17 21:14:36 -0700 | [diff] [blame] | 49 | @interface GRXConcurrentWriteable : NSObject |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 50 | |
Jorge Canizales | b10776c | 2015-10-26 10:44:55 -0700 | [diff] [blame] | 51 | /** |
| 52 | * The GRXWriteable passed is the wrapped writeable. |
| 53 | * The GRXWriteable instance is retained until writesFinishedWithError: is sent to it, and released |
| 54 | * after that. |
| 55 | */ |
Muxi Yan | 895f3d8 | 2017-04-05 13:12:30 -0700 | [diff] [blame] | 56 | - (instancetype)initWithWriteable:(id<GRXWriteable>)writeable |
| 57 | dispatchQueue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER; |
| 58 | - (instancetype)initWithWriteable:(id<GRXWriteable>)writeable; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 59 | |
Jorge Canizales | b10776c | 2015-10-26 10:44:55 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Enqueues writeValue: to be sent to the writeable in the main thread. |
| 62 | * The passed handler is invoked from the main thread after writeValue: returns. |
| 63 | */ |
Jorge Canizales | 4c6f778 | 2015-07-17 23:13:36 -0700 | [diff] [blame] | 64 | - (void)enqueueValue:(id)value completionHandler:(void (^)())handler; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 65 | |
Jorge Canizales | b10776c | 2015-10-26 10:44:55 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Enqueues writesFinishedWithError:nil to be sent to the writeable in the main thread. After that |
| 68 | * message is sent to the writeable, all other methods of this object are effectively noops. |
| 69 | */ |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 70 | - (void)enqueueSuccessfulCompletion; |
| 71 | |
Jorge Canizales | b10776c | 2015-10-26 10:44:55 -0700 | [diff] [blame] | 72 | /** |
| 73 | * If the writeable has not yet received a writesFinishedWithError: message, this will enqueue one |
| 74 | * to be sent to it in the main thread, and cancel all other pending messages to the writeable |
| 75 | * enqueued by this object (both past and future). |
| 76 | * The error argument cannot be nil. |
| 77 | */ |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 78 | - (void)cancelWithError:(NSError *)error; |
| 79 | |
Jorge Canizales | b10776c | 2015-10-26 10:44:55 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Cancels all pending messages to the writeable enqueued by this object (both past and future). |
| 82 | * Because the writeable won't receive writesFinishedWithError:, this also releases the writeable. |
| 83 | */ |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 84 | - (void)cancelSilently; |
| 85 | @end |