GRXConcurrentWriteable.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #import "GRXWriter.h"
  21. /**
  22. * This is a thread-safe wrapper over a GRXWriteable instance. It lets one enqueue calls to a
  23. * GRXWriteable instance for the thread user provided, guaranteeing that writesFinishedWithError: is
  24. * the last message sent to it (no matter what messages are sent to the wrapper, in what order, nor
  25. * from which thread). It also guarantees that, if cancelWithError: is called (e.g. by the app
  26. * cancelling the writes), no further messages are sent to the writeable except
  27. * writesFinishedWithError:.
  28. *
  29. * TODO(jcanizales): Let the user specify another queue for the writeable callbacks.
  30. */
  31. @interface GRXConcurrentWriteable : NSObject
  32. /**
  33. * The GRXWriteable passed is the wrapped writeable.
  34. * The GRXWriteable instance is retained until writesFinishedWithError: is sent to it, and released
  35. * after that.
  36. */
  37. - (instancetype)initWithWriteable:(id<GRXWriteable>)writeable
  38. dispatchQueue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER;
  39. - (instancetype)initWithWriteable:(id<GRXWriteable>)writeable;
  40. /**
  41. * Enqueues writeValue: to be sent to the writeable from the designated dispatch queue.
  42. * The passed handler is invoked from designated dispatch queue after writeValue: returns.
  43. */
  44. - (void)enqueueValue:(id)value completionHandler:(void (^)(void))handler;
  45. /**
  46. * Enqueues writesFinishedWithError:nil to be sent to the writeable in the designated dispatch
  47. * queue. After that message is sent to the writeable, all other methods of this object are
  48. * effectively noops.
  49. */
  50. - (void)enqueueSuccessfulCompletion;
  51. /**
  52. * If the writeable has not yet received a writesFinishedWithError: message, this will enqueue one
  53. * to be sent to it in the designated dispatch queue, and cancel all other pending messages to the
  54. * writeable enqueued by this object (both past and future).
  55. * The error argument cannot be nil.
  56. */
  57. - (void)cancelWithError:(NSError *)error;
  58. /**
  59. * Cancels all pending messages to the writeable enqueued by this object (both past and future).
  60. * Because the writeable won't receive writesFinishedWithError:, this also releases the writeable.
  61. */
  62. - (void)cancelSilently;
  63. @end