GRXConcurrentWriteable.h 3.5 KB

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