blob: cec45fae715ca503cbe234c298c15bd098441336 [file] [log] [blame]
Jorge Canizales9409ad82015-02-18 16:19:56 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Jorge Canizales9409ad82015-02-18 16:19:56 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Canizales9409ad82015-02-18 16:19:56 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Jorge Canizales9409ad82015-02-18 16:19:56 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Canizales9409ad82015-02-18 16:19:56 -080016 *
17 */
18
Jorge Canizales5e0efd92015-02-17 18:23:58 -080019#import <Foundation/Foundation.h>
20
Jorge Canizales4c6f7782015-07-17 23:13:36 -070021#import "GRXWriter.h"
22#import "GRXWriteable.h"
Jorge Canizales5e0efd92015-02-17 18:23:58 -080023
Jorge Canizalesb10776c2015-10-26 10:44:55 -070024/**
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 Canizales35f003b2015-07-17 21:14:36 -070034@interface GRXConcurrentWriteable : NSObject
Jorge Canizales5e0efd92015-02-17 18:23:58 -080035
Jorge Canizalesb10776c2015-10-26 10:44:55 -070036/**
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 Yan895f3d82017-04-05 13:12:30 -070041- (instancetype)initWithWriteable:(id<GRXWriteable>)writeable
42 dispatchQueue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER;
43- (instancetype)initWithWriteable:(id<GRXWriteable>)writeable;
Jorge Canizales5e0efd92015-02-17 18:23:58 -080044
Jorge Canizalesb10776c2015-10-26 10:44:55 -070045/**
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 */
Jorge Canizales4c6f7782015-07-17 23:13:36 -070049- (void)enqueueValue:(id)value completionHandler:(void (^)())handler;
Jorge Canizales5e0efd92015-02-17 18:23:58 -080050
Jorge Canizalesb10776c2015-10-26 10:44:55 -070051/**
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 Canizales5e0efd92015-02-17 18:23:58 -080055- (void)enqueueSuccessfulCompletion;
56
Jorge Canizalesb10776c2015-10-26 10:44:55 -070057/**
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 Canizales5e0efd92015-02-17 18:23:58 -080063- (void)cancelWithError:(NSError *)error;
64
Jorge Canizalesb10776c2015-10-26 10:44:55 -070065/**
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 Canizales5e0efd92015-02-17 18:23:58 -080069- (void)cancelSilently;
70@end