FSTSyncEngine.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/FSTRemoteStore.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/util/async_queue.h"
  22. @class FSTLocalStore;
  23. @class FSTMutation;
  24. @class FSTQuery;
  25. @class FSTRemoteEvent;
  26. @class FSTRemoteStore;
  27. @class FSTViewSnapshot;
  28. using firebase::firestore::model::OnlineState;
  29. NS_ASSUME_NONNULL_BEGIN
  30. #pragma mark - FSTSyncEngineDelegate
  31. /**
  32. * A delegate to be notified when the client's online state changes or when the sync engine produces
  33. * new view snapshots or errors.
  34. */
  35. @protocol FSTSyncEngineDelegate
  36. - (void)handleViewSnapshots:(NSArray<FSTViewSnapshot *> *)viewSnapshots;
  37. - (void)handleError:(NSError *)error forQuery:(FSTQuery *)query;
  38. - (void)applyChangedOnlineState:(OnlineState)onlineState;
  39. @end
  40. /**
  41. * SyncEngine is the central controller in the client SDK architecture. It is the glue code
  42. * between the EventManager, LocalStore, and RemoteStore. Some of SyncEngine's responsibilities
  43. * include:
  44. * 1. Coordinating client requests and remote events between the EventManager and the local and
  45. * remote data stores.
  46. * 2. Managing a View object for each query, providing the unified view between the local and
  47. * remote data stores.
  48. * 3. Notifying the RemoteStore when the LocalStore has new mutations in its queue that need
  49. * sending to the backend.
  50. *
  51. * The SyncEngine’s methods should only ever be called by methods running on our own worker
  52. * queue.
  53. */
  54. @interface FSTSyncEngine : NSObject <FSTRemoteSyncer>
  55. - (instancetype)init NS_UNAVAILABLE;
  56. - (instancetype)initWithLocalStore:(FSTLocalStore *)localStore
  57. remoteStore:(FSTRemoteStore *)remoteStore
  58. initialUser:(const firebase::firestore::auth::User &)user
  59. NS_DESIGNATED_INITIALIZER;
  60. /**
  61. * A delegate to be notified when queries being listened to produce new view snapshots or errors.
  62. */
  63. @property(nonatomic, weak) id<FSTSyncEngineDelegate> syncEngineDelegate;
  64. /**
  65. * Initiates a new listen. The FSTLocalStore will be queried for initial data and the listen will
  66. * be sent to the FSTRemoteStore to get remote data. The registered FSTSyncEngineDelegate will be
  67. * notified of resulting view snapshots and/or listen errors.
  68. *
  69. * @return the target ID assigned to the query.
  70. */
  71. - (firebase::firestore::model::TargetId)listenToQuery:(FSTQuery *)query;
  72. /** Stops listening to a query previously listened to via listenToQuery:. */
  73. - (void)stopListeningToQuery:(FSTQuery *)query;
  74. /**
  75. * Initiates the write of local mutation batch which involves adding the writes to the mutation
  76. * queue, notifying the remote store about new mutations, and raising events for any changes this
  77. * write caused. The provided completion block will be called once the write has been acked or
  78. * rejected by the backend (or failed locally for any other reason).
  79. */
  80. - (void)writeMutations:(NSArray<FSTMutation *> *)mutations completion:(FSTVoidErrorBlock)completion;
  81. /**
  82. * Runs the given transaction block up to retries times and then calls completion.
  83. *
  84. * @param retries The number of times to try before giving up.
  85. * @param workerQueue The queue to dispatch sync engine calls to.
  86. * @param updateBlock The block to call to execute the user's transaction.
  87. * @param completion The block to call when the transaction is finished or failed.
  88. */
  89. - (void)transactionWithRetries:(int)retries
  90. workerQueue:(firebase::firestore::util::AsyncQueue *)workerQueue
  91. updateBlock:(FSTTransactionBlock)updateBlock
  92. completion:(FSTVoidIDErrorBlock)completion;
  93. - (void)credentialDidChangeWithUser:(const firebase::firestore::auth::User &)user;
  94. /** Applies an OnlineState change to the sync engine and notifies any views of the change. */
  95. - (void)applyChangedOnlineState:(firebase::firestore::model::OnlineState)onlineState;
  96. @end
  97. NS_ASSUME_NONNULL_END