FSTSyncEngine.h 4.8 KB

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