Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 19 | #import <Foundation/Foundation.h> |
| 20 | |
Jorge Canizales | 4c6f778 | 2015-07-17 23:13:36 -0700 | [diff] [blame] | 21 | #import "GRXWriter.h" |
| 22 | #import "GRXWriteable.h" |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 23 | |
Jorge Canizales | b10776c | 2015-10-26 10:44:55 -0700 | [diff] [blame] | 24 | /** |
| 25 | * This is a thread-safe wrapper over a GRXWriteable instance. It lets one enqueue calls to a |
| 26 | * GRXWriteable instance for the main thread, guaranteeing that writesFinishedWithError: is the last |
| 27 | * message sent to it (no matter what messages are sent to the wrapper, in what order, nor from |
| 28 | * which thread). It also guarantees that, if cancelWithError: is called from the main thread (e.g. |
| 29 | * by the app cancelling the writes), no further messages are sent to the writeable except |
| 30 | * writesFinishedWithError:. |
| 31 | * |
| 32 | * TODO(jcanizales): Let the user specify another queue for the writeable callbacks. |
| 33 | */ |
Jorge Canizales | 35f003b | 2015-07-17 21:14:36 -0700 | [diff] [blame] | 34 | @interface GRXConcurrentWriteable : NSObject |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 35 | |
Jorge Canizales | b10776c | 2015-10-26 10:44:55 -0700 | [diff] [blame] | 36 | /** |
| 37 | * The GRXWriteable passed is the wrapped writeable. |
| 38 | * The GRXWriteable instance is retained until writesFinishedWithError: is sent to it, and released |
| 39 | * after that. |
| 40 | */ |
Muxi Yan | 895f3d8 | 2017-04-05 13:12:30 -0700 | [diff] [blame] | 41 | - (instancetype)initWithWriteable:(id<GRXWriteable>)writeable |
| 42 | dispatchQueue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER; |
| 43 | - (instancetype)initWithWriteable:(id<GRXWriteable>)writeable; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 44 | |
Jorge Canizales | b10776c | 2015-10-26 10:44:55 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Enqueues writeValue: to be sent to the writeable in the main thread. |
| 47 | * The passed handler is invoked from the main thread after writeValue: returns. |
| 48 | */ |
Muxi Yan | 0c0ebc5 | 2017-10-19 18:41:01 -0700 | [diff] [blame] | 49 | - (void)enqueueValue:(id)value completionHandler:(void (^)(void))handler; |
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 | * Enqueues writesFinishedWithError:nil to be sent to the writeable in the main thread. After that |
| 53 | * message is sent to the writeable, all other methods of this object are effectively noops. |
| 54 | */ |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 55 | - (void)enqueueSuccessfulCompletion; |
| 56 | |
Jorge Canizales | b10776c | 2015-10-26 10:44:55 -0700 | [diff] [blame] | 57 | /** |
| 58 | * If the writeable has not yet received a writesFinishedWithError: message, this will enqueue one |
| 59 | * to be sent to it in the main thread, and cancel all other pending messages to the writeable |
| 60 | * enqueued by this object (both past and future). |
| 61 | * The error argument cannot be nil. |
| 62 | */ |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 63 | - (void)cancelWithError:(NSError *)error; |
| 64 | |
Jorge Canizales | b10776c | 2015-10-26 10:44:55 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Cancels all pending messages to the writeable enqueued by this object (both past and future). |
| 67 | * Because the writeable won't receive writesFinishedWithError:, this also releases the writeable. |
| 68 | */ |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 69 | - (void)cancelSilently; |
| 70 | @end |