FSTRemoteStore.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #import "Firestore/Source/Core/FSTTypes.h"
  18. #import "Firestore/Source/Remote/FSTRemoteEvent.h"
  19. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  20. @class FSTDatastore;
  21. @class FSTLocalStore;
  22. @class FSTMutationBatch;
  23. @class FSTMutationBatchResult;
  24. @class FSTQueryData;
  25. @class FSTRemoteEvent;
  26. @class FSTTransaction;
  27. @class FSTDispatchQueue;
  28. NS_ASSUME_NONNULL_BEGIN
  29. #pragma mark - FSTRemoteSyncer
  30. /**
  31. * A protocol that describes the actions the FSTRemoteStore needs to perform on a cooperating
  32. * synchronization engine.
  33. */
  34. @protocol FSTRemoteSyncer
  35. /**
  36. * Applies one remote event to the sync engine, notifying any views of the changes, and releasing
  37. * any pending mutation batches that would become visible because of the snapshot version the
  38. * remote event contains.
  39. */
  40. - (void)applyRemoteEvent:(FSTRemoteEvent *)remoteEvent;
  41. /**
  42. * Rejects the listen for the given targetID. This can be triggered by the backend for any active
  43. * target.
  44. *
  45. * @param targetID The targetID corresponding to a listen initiated via
  46. * -listenToTargetWithQueryData: on FSTRemoteStore.
  47. * @param error A description of the condition that has forced the rejection. Nearly always this
  48. * will be an indication that the user is no longer authorized to see the data matching the
  49. * target.
  50. */
  51. - (void)rejectListenWithTargetID:(const firebase::firestore::model::TargetId)targetID
  52. error:(NSError *)error;
  53. /**
  54. * Applies the result of a successful write of a mutation batch to the sync engine, emitting
  55. * snapshots in any views that the mutation applies to, and removing the batch from the mutation
  56. * queue.
  57. */
  58. - (void)applySuccessfulWriteWithResult:(FSTMutationBatchResult *)batchResult;
  59. /**
  60. * Rejects the batch, removing the batch from the mutation queue, recomputing the local view of
  61. * any documents affected by the batch and then, emitting snapshots with the reverted value.
  62. */
  63. - (void)rejectFailedWriteWithBatchID:(FSTBatchID)batchID error:(NSError *)error;
  64. /**
  65. * Returns the set of remote document keys for the given target ID. This list includes the
  66. * documents that were assigned to the target when we received the last snapshot.
  67. */
  68. - (firebase::firestore::model::DocumentKeySet)remoteKeysForTarget:(FSTBoxedTargetID *)targetId;
  69. @end
  70. /**
  71. * A protocol for the FSTRemoteStore online state delegate, called whenever the state of the
  72. * online streams of the FSTRemoteStore changes.
  73. * Note that this protocol only supports the watch stream for now.
  74. */
  75. @protocol FSTOnlineStateDelegate <NSObject>
  76. /** Called whenever the online state of the watch stream changes */
  77. - (void)applyChangedOnlineState:(FSTOnlineState)onlineState;
  78. @end
  79. #pragma mark - FSTRemoteStore
  80. /**
  81. * FSTRemoteStore handles all interaction with the backend through a simple, clean interface. This
  82. * class is not thread safe and should be only called from the worker dispatch queue.
  83. */
  84. @interface FSTRemoteStore : NSObject <FSTTargetMetadataProvider>
  85. - (instancetype)initWithLocalStore:(FSTLocalStore *)localStore
  86. datastore:(FSTDatastore *)datastore
  87. workerDispatchQueue:(FSTDispatchQueue *)queue;
  88. - (instancetype)init NS_UNAVAILABLE;
  89. @property(nonatomic, weak) id<FSTRemoteSyncer> syncEngine;
  90. @property(nonatomic, weak) id<FSTOnlineStateDelegate> onlineStateDelegate;
  91. /** Starts up the remote store, creating streams, restoring state from LocalStore, etc. */
  92. - (void)start;
  93. /** Shuts down the remote store, tearing down connections and otherwise cleaning up. */
  94. - (void)shutdown;
  95. /** Temporarily disables the network. The network can be re-enabled using 'enableNetwork:'. */
  96. - (void)disableNetwork;
  97. /** Re-enables the network. Only to be called as the counterpart to 'disableNetwork:'. */
  98. - (void)enableNetwork;
  99. /**
  100. * Tells the FSTRemoteStore that the currently authenticated user has changed.
  101. *
  102. * In response the remote store tears down streams and clears up any tracked operations that should
  103. * not persist across users. Restarts the streams if appropriate.
  104. */
  105. - (void)userDidChange:(const firebase::firestore::auth::User &)user;
  106. /** Listens to the target identified by the given FSTQueryData. */
  107. - (void)listenToTargetWithQueryData:(FSTQueryData *)queryData;
  108. /** Stops listening to the target with the given target ID. */
  109. - (void)stopListeningToTargetID:(FSTTargetID)targetID;
  110. /**
  111. * Tells the FSTRemoteStore that there are new mutations to process in the queue. This is typically
  112. * called by FSTSyncEngine after it has sent mutations to FSTLocalStore.
  113. *
  114. * In response the remote store will pull mutations from the local store until the datastore
  115. * instance reports that it cannot accept further in-progress writes. This mechanism serves to
  116. * maintain a pipeline of in-flight requests between the FSTDatastore and the server that
  117. * applies them.
  118. */
  119. - (void)fillWritePipeline;
  120. /** Returns a new transaction backed by this remote store. */
  121. - (FSTTransaction *)transaction;
  122. @end
  123. NS_ASSUME_NONNULL_END