blob: 5d6e1a472af965b4a5d084ff4fcf2031c5ab5383 [file] [log] [blame]
Jorge Canizalese8304d52015-02-17 19:50:51 -08001/*
2 *
Yang Gao5fc90292015-02-20 09:46:22 -08003 * Copyright 2015, Google Inc.
Jorge Canizalese8304d52015-02-17 19:50:51 -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 Canizales30697c92015-02-17 17:09:14 -080034#import <Foundation/Foundation.h>
35
36#import "GRXWriteable.h"
37
38typedef NS_ENUM(NSInteger, GRXWriterState) {
39
40 // The writer has not yet been given a writeable to which it can push its
41 // values. To have an writer transition to the Started state, send it a
42 // startWithWriteable: message.
43 //
44 // An writer's state cannot be manually set to this value.
45 GRXWriterStateNotStarted,
46
47 // The writer might push values to the writeable at any moment.
48 GRXWriterStateStarted,
49
50 // The writer is temporarily paused, and won't send any more values to the
51 // writeable unless its state is set back to Started. The writer might still
52 // transition to the Finished state at any moment, and is allowed to send
Jorge Canizalesb2c300c2015-05-18 17:19:16 -070053 // writesFinishedWithError: to its writeable.
Jorge Canizales30697c92015-02-17 17:09:14 -080054 //
55 // Not all implementations of writer have to support pausing, and thus
56 // trying to set an writer's state to this value might have no effect.
57 GRXWriterStatePaused,
58
59 // The writer has released its writeable and won't interact with it anymore.
60 //
61 // One seldomly wants to set an writer's state to this value, as its
Jorge Canizalesb2c300c2015-05-18 17:19:16 -070062 // writeable isn't notified with a writesFinishedWithError: message. Instead, sending
Jorge Canizales30697c92015-02-17 17:09:14 -080063 // finishWithError: to the writer will make it notify the writeable and then
64 // transition to this state.
65 GRXWriterStateFinished
66};
67
68// An object that conforms to this protocol can produce, on demand, a sequence
69// of values. The sequence may be produced asynchronously, and it may consist of
70// any number of elements, including none or an infinite number.
71//
72// GRXWriter is the active dual of NSEnumerator. The difference between them
73// is thus whether the object plays an active or passive role during usage: A
74// user of NSEnumerator pulls values off it, and passes the values to a writeable.
75// A user of GRXWriter, though, just gives it a writeable, and the
76// GRXWriter instance pushes values to the writeable. This makes this protocol
77// suitable to represent a sequence of future values, as well as collections
78// with internal iteration.
79//
80// An instance of GRXWriter can start producing values after a writeable is
81// passed to it. It can also be commanded to finish the sequence immediately
82// (with an optional error). Finally, it can be asked to pause, but the
83// conforming instance is not required to oblige.
84//
85// Unless otherwise indicated by a conforming class, no messages should be sent
86// concurrently to a GRXWriter. I.e., conforming classes aren't required to
87// be thread-safe.
Jorge Canizales7b52c982015-07-16 18:41:21 -070088@interface GRXWriter : NSObject
Jorge Canizales30697c92015-02-17 17:09:14 -080089
90// This property can be used to query the current state of the writer, which
91// determines how it might currently use its writeable. Some state transitions can
92// be triggered by setting this property to the corresponding value, and that's
93// useful for advanced use cases like pausing an writer. For more details,
94// see the documentation of the enum.
95@property(nonatomic) GRXWriterState state;
96
97// Start sending messages to the writeable. Messages may be sent before the method
98// returns, or they may be sent later in the future. See GRXWriteable.h for the
99// different messages a writeable can receive.
100//
101// If this writer draws its values from an external source (e.g. from the
102// filesystem or from a server), calling this method will commonly trigger side
103// effects (like network connections).
104//
105// This method might only be called on writers in the NotStarted state.
106- (void)startWithWriteable:(id<GRXWriteable>)writeable;
107
Jorge Canizalesb2c300c2015-05-18 17:19:16 -0700108// Send writesFinishedWithError:errorOrNil immediately to the writeable, and don't send
Jorge Canizales30697c92015-02-17 17:09:14 -0800109// any more messages to it.
110//
111// This method might only be called on writers in the Started or Paused
112// state.
113//
114// TODO(jcanizales): Consider adding some guarantee about the immediacy of that
115// stopping. I know I've relied on it in part of the code that uses this, but
116// can't remember the details in the presence of concurrency.
117- (void)finishWithError:(NSError *)errorOrNil;
118@end