GRXWriter.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #import <Foundation/Foundation.h>
  19. #import "GRXWriteable.h"
  20. /** States of a writer. */
  21. typedef NS_ENUM(NSInteger, GRXWriterState) {
  22. /**
  23. * The writer has not yet been given a writeable to which it can push its values. To have a writer
  24. * transition to the Started state, send it a startWithWriteable: message.
  25. *
  26. * A writer's state cannot be manually set to this value.
  27. */
  28. GRXWriterStateNotStarted,
  29. /** The writer might push values to the writeable at any moment. */
  30. GRXWriterStateStarted,
  31. /**
  32. * The writer is temporarily paused, and won't send any more values to the writeable unless its
  33. * state is set back to Started. The writer might still transition to the Finished state at any
  34. * moment, and is allowed to send writesFinishedWithError: to its writeable.
  35. */
  36. GRXWriterStatePaused,
  37. /**
  38. * The writer has released its writeable and won't interact with it anymore.
  39. *
  40. * One seldom wants to set a writer's state to this value, as its writeable isn't notified with
  41. * a writesFinishedWithError: message. Instead, sending finishWithError: to the writer will make
  42. * it notify the writeable and then transition to this state.
  43. */
  44. GRXWriterStateFinished
  45. };
  46. /**
  47. * An GRXWriter object can produce, on demand, a sequence of values. The sequence may be produced
  48. * asynchronously, and it may consist of any number of elements, including none or an infinite
  49. * number.
  50. *
  51. * GRXWriter is the active dual of NSEnumerator. The difference between them is thus whether the
  52. * object plays an active or passive role during usage: A user of NSEnumerator pulls values off it,
  53. * and passes the values to a writeable. A user of GRXWriter, though, just gives it a writeable, and
  54. * the GRXWriter instance pushes values to the writeable. This makes this protocol suitable to
  55. * represent a sequence of future values, as well as collections with internal iteration.
  56. *
  57. * An instance of GRXWriter can start producing values after a writeable is passed to it. It can
  58. * also be commanded to finish the sequence immediately (with an optional error). Finally, it can be
  59. * asked to pause, and resumed later. All GRXWriter objects support pausing and early termination.
  60. *
  61. * Thread-safety:
  62. *
  63. * State transitions take immediate effect if the object is used from a single thread. Subclasses
  64. * might offer stronger guarantees.
  65. *
  66. * Unless otherwise indicated by a conforming subclass, no messages should be sent concurrently to a
  67. * GRXWriter. I.e., conforming classes aren't required to be thread-safe.
  68. */
  69. @interface GRXWriter : NSObject
  70. /**
  71. * This property can be used to query the current state of the writer, which determines how it might
  72. * currently use its writeable. Some state transitions can be triggered by setting this property to
  73. * the corresponding value, and that's useful for advanced use cases like pausing an writer. For
  74. * more details, see the documentation of the enum further down. The property is thread safe.
  75. */
  76. @property GRXWriterState state;
  77. /**
  78. * Transition to the Started state, and start sending messages to the writeable (a reference to it
  79. * is retained). Messages to the writeable may be sent before the method returns, or they may be
  80. * sent later in the future. See GRXWriteable.h for the different messages a writeable can receive.
  81. *
  82. * If this writer draws its values from an external source (e.g. from the filesystem or from a
  83. * server), calling this method will commonly trigger side effects (like network connections).
  84. *
  85. * This method might only be called on writers in the NotStarted state.
  86. */
  87. - (void)startWithWriteable:(id<GRXWriteable>)writeable;
  88. /**
  89. * Send writesFinishedWithError:errorOrNil to the writeable. Then release the reference to it and
  90. * transition to the Finished state.
  91. *
  92. * This method might only be called on writers in the Started or Paused state.
  93. */
  94. - (void)finishWithError:(NSError *)errorOrNil;
  95. @end