FSTRemoteStore.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #include <memory>
  18. #import "Firestore/Source/Remote/FSTRemoteEvent.h"
  19. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  20. #include "Firestore/core/src/firebase/firestore/model/types.h"
  21. #include "Firestore/core/src/firebase/firestore/remote/datastore.h"
  22. #include "Firestore/core/src/firebase/firestore/util/async_queue.h"
  23. @class FSTLocalStore;
  24. @class FSTMutationBatch;
  25. @class FSTMutationBatchResult;
  26. @class FSTQueryData;
  27. @class FSTRemoteEvent;
  28. @class FSTTransaction;
  29. NS_ASSUME_NONNULL_BEGIN
  30. #pragma mark - FSTRemoteSyncer
  31. /**
  32. * A protocol that describes the actions the FSTRemoteStore needs to perform on a cooperating
  33. * synchronization engine.
  34. */
  35. @protocol FSTRemoteSyncer
  36. /**
  37. * Applies one remote event to the sync engine, notifying any views of the changes, and releasing
  38. * any pending mutation batches that would become visible because of the snapshot version the
  39. * remote event contains.
  40. */
  41. - (void)applyRemoteEvent:(FSTRemoteEvent *)remoteEvent;
  42. /**
  43. * Rejects the listen for the given targetID. This can be triggered by the backend for any active
  44. * target.
  45. *
  46. * @param targetID The targetID corresponding to a listen initiated via
  47. * -listenToTargetWithQueryData: on FSTRemoteStore.
  48. * @param error A description of the condition that has forced the rejection. Nearly always this
  49. * will be an indication that the user is no longer authorized to see the data matching the
  50. * target.
  51. */
  52. - (void)rejectListenWithTargetID:(const firebase::firestore::model::TargetId)targetID
  53. error:(NSError *)error;
  54. /**
  55. * Applies the result of a successful write of a mutation batch to the sync engine, emitting
  56. * snapshots in any views that the mutation applies to, and removing the batch from the mutation
  57. * queue.
  58. */
  59. - (void)applySuccessfulWriteWithResult:(FSTMutationBatchResult *)batchResult;
  60. /**
  61. * Rejects the batch, removing the batch from the mutation queue, recomputing the local view of
  62. * any documents affected by the batch and then, emitting snapshots with the reverted value.
  63. */
  64. - (void)rejectFailedWriteWithBatchID:(firebase::firestore::model::BatchId)batchID
  65. error:(NSError *)error;
  66. /**
  67. * Returns the set of remote document keys for the given target ID. This list includes the
  68. * documents that were assigned to the target when we received the last snapshot.
  69. */
  70. - (firebase::firestore::model::DocumentKeySet)remoteKeysForTarget:(FSTBoxedTargetID *)targetId;
  71. @end
  72. /**
  73. * A protocol for the FSTRemoteStore online state delegate, called whenever the state of the
  74. * online streams of the FSTRemoteStore changes.
  75. * Note that this protocol only supports the watch stream for now.
  76. */
  77. @protocol FSTOnlineStateDelegate <NSObject>
  78. /** Called whenever the online state of the watch stream changes */
  79. - (void)applyChangedOnlineState:(firebase::firestore::model::OnlineState)onlineState;
  80. @end
  81. #pragma mark - FSTRemoteStore
  82. /**
  83. * FSTRemoteStore handles all interaction with the backend through a simple, clean interface. This
  84. * class is not thread safe and should be only called from the worker dispatch queue.
  85. */
  86. @interface FSTRemoteStore : NSObject <FSTTargetMetadataProvider>
  87. - (instancetype)initWithLocalStore:(FSTLocalStore *)localStore
  88. datastore:
  89. (std::shared_ptr<firebase::firestore::remote::Datastore>)datastore
  90. workerQueue:(firebase::firestore::util::AsyncQueue *)queue;
  91. - (instancetype)init NS_UNAVAILABLE;
  92. @property(nonatomic, weak) id<FSTRemoteSyncer> syncEngine;
  93. @property(nonatomic, weak) id<FSTOnlineStateDelegate> onlineStateDelegate;
  94. /** Starts up the remote store, creating streams, restoring state from LocalStore, etc. */
  95. - (void)start;
  96. /** Shuts down the remote store, tearing down connections and otherwise cleaning up. */
  97. - (void)shutdown;
  98. /** Temporarily disables the network. The network can be re-enabled using 'enableNetwork:'. */
  99. - (void)disableNetwork;
  100. /** Re-enables the network. Only to be called as the counterpart to 'disableNetwork:'. */
  101. - (void)enableNetwork;
  102. /**
  103. * Tells the FSTRemoteStore that the currently authenticated user has changed.
  104. *
  105. * In response the remote store tears down streams and clears up any tracked operations that should
  106. * not persist across users. Restarts the streams if appropriate.
  107. */
  108. - (void)credentialDidChange;
  109. /** Listens to the target identified by the given FSTQueryData. */
  110. - (void)listenToTargetWithQueryData:(FSTQueryData *)queryData;
  111. /** Stops listening to the target with the given target ID. */
  112. - (void)stopListeningToTargetID:(firebase::firestore::model::TargetId)targetID;
  113. /**
  114. * Tells the FSTRemoteStore that there are new mutations to process in the queue. This is typically
  115. * called by FSTSyncEngine after it has sent mutations to FSTLocalStore.
  116. *
  117. * In response the remote store will pull mutations from the local store until the datastore
  118. * instance reports that it cannot accept further in-progress writes. This mechanism serves to
  119. * maintain a pipeline of in-flight requests between the `Datastore` and the server that
  120. * applies them.
  121. */
  122. - (void)fillWritePipeline;
  123. /** Returns a new transaction backed by this remote store. */
  124. - (FSTTransaction *)transaction;
  125. @end
  126. NS_ASSUME_NONNULL_END