FSTSyncEngine.h 4.0 KB

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