FSTSyncEngine.h 4.5 KB

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