FSTSyncEngine.h 4.6 KB

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