blob: 9a30a2f9660be3764573b3bf3d5f060f8d3e360d [file] [log] [blame]
Jorge Canizales9409ad82015-02-18 16:19:56 -08001/*
2 *
Yang Gao5fc90292015-02-20 09:46:22 -08003 * Copyright 2015, Google Inc.
Jorge Canizales9409ad82015-02-18 16:19:56 -08004 * 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 Canizales5e0efd92015-02-17 18:23:58 -080034#import <Foundation/Foundation.h>
35
Jorge Canizalesa38baae2015-07-16 21:12:56 -070036#import <RxLibrary/GRXWriter.h>
37
Jorge Canizales5e0efd92015-02-17 18:23:58 -080038@protocol GRXWriteable;
Jorge Canizales5e0efd92015-02-17 18:23:58 -080039
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 Canizalesb2c300c2015-05-18 17:19:16 -070042// that writesFinishedWithError: is the last message sent to it (no matter what
Jorge Canizales5e0efd92015-02-17 18:23:58 -080043// 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 Canizalesb2c300c2015-05-18 17:19:16 -070046// writeable except writesFinishedWithError:.
Jorge Canizales5e0efd92015-02-17 18:23:58 -080047//
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 Canizalesb2c300c2015-05-18 17:19:16 -070055// writesFinishedWithError: is sent to the writeable, and released after that.
Jorge Canizales5e0efd92015-02-17 18:23:58 -080056// This is used to create a retain cycle that keeps both objects alive until the
57// writing is explicitly finished.
Jorge Canizales56047122015-07-17 12:18:08 -070058- (instancetype)initWithWriteable:(id<GRXWriteable>)writeable writer:(GRXWriter *)writer
Jorge Canizales5e0efd92015-02-17 18:23:58 -080059 NS_DESIGNATED_INITIALIZER;
60
Jorge Canizalesa90a9c32015-05-18 17:12:41 -070061// 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 Canizales5e0efd92015-02-17 18:23:58 -080063- (void)enqueueMessage:(NSData *)message completionHandler:(void (^)())handler;
64
Jorge Canizalesb2c300c2015-05-18 17:19:16 -070065// Enqueues writesFinishedWithError:nil to be sent to the writeable in the main
Jorge Canizales5e0efd92015-02-17 18:23:58 -080066// thread. After that message is sent to the writeable, all other methods of
67// this object are effectively noops.
68- (void)enqueueSuccessfulCompletion;
69
Jorge Canizalesb2c300c2015-05-18 17:19:16 -070070// If the writeable has not yet received a writesFinishedWithError: message, this
Jorge Canizales5e0efd92015-02-17 18:23:58 -080071// 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 Canizalesb2c300c2015-05-18 17:19:16 -070078// past and future). Because the writeable won't receive writesFinishedWithError:,
Jorge Canizales5e0efd92015-02-17 18:23:58 -080079// this also releases the writeable and the writer.
80- (void)cancelSilently;
81@end