1 /* 2 * 3 * Copyright 2015 gRPC authors. 4 * 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 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 * 17 */ 18 19 #import <Foundation/Foundation.h> 20 21 #import "GRXWriteable.h" 22 #import "GRXWriter.h" 23 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 */ 34 @interface GRXConcurrentWriteable : NSObject 35 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 */ 41 - (instancetype)initWithWriteable:(id<GRXWriteable>)writeable 42 dispatchQueue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER; 43 - (instancetype)initWithWriteable:(id<GRXWriteable>)writeable; 44 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 */ 49 - (void)enqueueValue:(id)value completionHandler:(void (^)(void))handler; 50 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 */ 55 - (void)enqueueSuccessfulCompletion; 56 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 */ 63 - (void)cancelWithError:(NSError *)error; 64 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 */ 69 - (void)cancelSilently; 70 @end 71