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 | a38baae | 2015-07-16 21:12:56 -0700 | [diff] [blame^] | 36 | #import <RxLibrary/GRXWriter.h> |
| 37 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 38 | @protocol GRXWriteable; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 39 | |
| 40 | // This is a thread-safe wrapper over a GRXWriteable instance. It lets one |
| 41 | // enqueue calls to a GRXWriteable instance for the main thread, guaranteeing |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 42 | // that writesFinishedWithError: is the last message sent to it (no matter what |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 43 | // messages are sent to the wrapper, in what order, nor from which thread). It |
| 44 | // also guarantees that, if cancelWithError: is called from the main thread |
| 45 | // (e.g. by the app cancelling the writes), no further messages are sent to the |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 46 | // writeable except writesFinishedWithError:. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 47 | // |
| 48 | // TODO(jcanizales): Let the user specify another queue for the writeable |
| 49 | // callbacks. |
| 50 | // TODO(jcanizales): Rename to GRXWriteableWrapper and move to the Rx library. |
| 51 | @interface GRPCDelegateWrapper : NSObject |
| 52 | |
| 53 | // The GRXWriteable passed is the wrapped writeable. |
| 54 | // Both the GRXWriter instance and the GRXWriteable instance are retained until |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 55 | // writesFinishedWithError: is sent to the writeable, and released after that. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 56 | // This is used to create a retain cycle that keeps both objects alive until the |
| 57 | // writing is explicitly finished. |
Jorge Canizales | 5604712 | 2015-07-17 12:18:08 -0700 | [diff] [blame] | 58 | - (instancetype)initWithWriteable:(id<GRXWriteable>)writeable writer:(GRXWriter *)writer |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 59 | NS_DESIGNATED_INITIALIZER; |
| 60 | |
Jorge Canizales | a90a9c3 | 2015-05-18 17:12:41 -0700 | [diff] [blame] | 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. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 63 | - (void)enqueueMessage:(NSData *)message completionHandler:(void (^)())handler; |
| 64 | |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 65 | // Enqueues writesFinishedWithError:nil to be sent to the writeable in the main |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 66 | // thread. After that message is sent to the writeable, all other methods of |
| 67 | // this object are effectively noops. |
| 68 | - (void)enqueueSuccessfulCompletion; |
| 69 | |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 70 | // If the writeable has not yet received a writesFinishedWithError: message, this |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 71 | // will enqueue one to be sent to it in the main thread, and cancel all other |
| 72 | // pending messages to the writeable enqueued by this object (both past and |
| 73 | // future). |
| 74 | // The error argument cannot be nil. |
| 75 | - (void)cancelWithError:(NSError *)error; |
| 76 | |
| 77 | // Cancels all pending messages to the writeable enqueued by this object (both |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 78 | // past and future). Because the writeable won't receive writesFinishedWithError:, |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 79 | // this also releases the writeable and the writer. |
| 80 | - (void)cancelSilently; |
| 81 | @end |