blob: 07004f6d4dc66e7eb0fb4dc7c6d5c6479524b262 [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 Canizales4c6f7782015-07-17 23:13:36 -070036#import "GRXWriter.h"
37#import "GRXWriteable.h"
Jorge Canizales5e0efd92015-02-17 18:23:58 -080038
Jorge Canizalesb10776c2015-10-26 10:44:55 -070039/**
40 * This is a thread-safe wrapper over a GRXWriteable instance. It lets one enqueue calls to a
41 * GRXWriteable instance for the main thread, guaranteeing that writesFinishedWithError: is the last
42 * message sent to it (no matter what messages are sent to the wrapper, in what order, nor from
43 * which thread). It also guarantees that, if cancelWithError: is called from the main thread (e.g.
44 * by the app cancelling the writes), no further messages are sent to the writeable except
45 * writesFinishedWithError:.
46 *
47 * TODO(jcanizales): Let the user specify another queue for the writeable callbacks.
48 */
Jorge Canizales35f003b2015-07-17 21:14:36 -070049@interface GRXConcurrentWriteable : NSObject
Jorge Canizales5e0efd92015-02-17 18:23:58 -080050
Jorge Canizalesb10776c2015-10-26 10:44:55 -070051/**
52 * The GRXWriteable passed is the wrapped writeable.
53 * The GRXWriteable instance is retained until writesFinishedWithError: is sent to it, and released
54 * after that.
55 */
Muxi Yan895f3d82017-04-05 13:12:30 -070056- (instancetype)initWithWriteable:(id<GRXWriteable>)writeable
57 dispatchQueue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER;
58- (instancetype)initWithWriteable:(id<GRXWriteable>)writeable;
Jorge Canizales5e0efd92015-02-17 18:23:58 -080059
Jorge Canizalesb10776c2015-10-26 10:44:55 -070060/**
61 * 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.
63 */
Jorge Canizales4c6f7782015-07-17 23:13:36 -070064- (void)enqueueValue:(id)value completionHandler:(void (^)())handler;
Jorge Canizales5e0efd92015-02-17 18:23:58 -080065
Jorge Canizalesb10776c2015-10-26 10:44:55 -070066/**
67 * Enqueues writesFinishedWithError:nil to be sent to the writeable in the main thread. After that
68 * message is sent to the writeable, all other methods of this object are effectively noops.
69 */
Jorge Canizales5e0efd92015-02-17 18:23:58 -080070- (void)enqueueSuccessfulCompletion;
71
Jorge Canizalesb10776c2015-10-26 10:44:55 -070072/**
73 * If the writeable has not yet received a writesFinishedWithError: message, this will enqueue one
74 * to be sent to it in the main thread, and cancel all other pending messages to the writeable
75 * enqueued by this object (both past and future).
76 * The error argument cannot be nil.
77 */
Jorge Canizales5e0efd92015-02-17 18:23:58 -080078- (void)cancelWithError:(NSError *)error;
79
Jorge Canizalesb10776c2015-10-26 10:44:55 -070080/**
81 * Cancels all pending messages to the writeable enqueued by this object (both past and future).
82 * Because the writeable won't receive writesFinishedWithError:, this also releases the writeable.
83 */
Jorge Canizales5e0efd92015-02-17 18:23:58 -080084- (void)cancelSilently;
85@end