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 thread user provided, guaranteeing that writesFinishedWithError: is
27  * the last message sent to it (no matter what messages are sent to the wrapper, in what order, nor
28  * from which thread). It also guarantees that, if cancelWithError: is called (e.g. by the app
29  * 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 from the designated dispatch queue.
47  * The passed handler is invoked from designated dispatch queue 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 designated dispatch
53  * queue. After that message is sent to the writeable, all other methods of this object are
54  * effectively noops.
55  */
56 - (void)enqueueSuccessfulCompletion;
57 
58 /**
59  * If the writeable has not yet received a writesFinishedWithError: message, this will enqueue one
60  * to be sent to it in the designated dispatch queue, and cancel all other pending messages to the
61  * writeable enqueued by this object (both past and future).
62  * The error argument cannot be nil.
63  */
64 - (void)cancelWithError:(NSError *)error;
65 
66 /**
67  * Cancels all pending messages to the writeable enqueued by this object (both past and future).
68  * Because the writeable won't receive writesFinishedWithError:, this also releases the writeable.
69  */
70 - (void)cancelSilently;
71 @end
72