2017-10-03 08:55:22 -07:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright 2017 Google
|
|
|
|
|
|
*
|
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
|
*
|
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
*
|
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
|
2019-05-17 15:29:30 -07:00
|
|
|
|
#include <memory>
|
2019-02-07 22:03:17 -05:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
2018-08-16 15:24:51 -07:00
|
|
|
|
#import "Firestore/Source/Core/FSTTypes.h"
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
2018-02-15 17:23:08 -05:00
|
|
|
|
#include "Firestore/core/src/firebase/firestore/auth/user.h"
|
2019-04-16 17:37:38 -07:00
|
|
|
|
#include "Firestore/core/src/firebase/firestore/core/transaction.h"
|
2018-08-15 15:56:45 -07:00
|
|
|
|
#include "Firestore/core/src/firebase/firestore/model/types.h"
|
2019-02-08 14:37:42 -05:00
|
|
|
|
#include "Firestore/core/src/firebase/firestore/remote/remote_store.h"
|
2018-11-18 17:49:32 -05:00
|
|
|
|
#include "Firestore/core/src/firebase/firestore/util/async_queue.h"
|
2019-04-16 17:37:38 -07:00
|
|
|
|
#include "Firestore/core/src/firebase/firestore/util/statusor_callback.h"
|
2018-02-15 17:23:08 -05:00
|
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
|
@class FSTLocalStore;
|
|
|
|
|
|
@class FSTMutation;
|
|
|
|
|
|
@class FSTQuery;
|
|
|
|
|
|
|
2019-04-12 17:03:26 -07:00
|
|
|
|
namespace auth = firebase::firestore::auth;
|
|
|
|
|
|
namespace core = firebase::firestore::core;
|
|
|
|
|
|
namespace model = firebase::firestore::model;
|
|
|
|
|
|
namespace remote = firebase::firestore::remote;
|
|
|
|
|
|
namespace util = firebase::firestore::util;
|
2018-11-07 11:22:01 -08:00
|
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - FSTSyncEngineDelegate
|
|
|
|
|
|
|
2018-11-07 11:22:01 -08:00
|
|
|
|
/**
|
|
|
|
|
|
* A delegate to be notified when the client's online state changes or when the sync engine produces
|
|
|
|
|
|
* new view snapshots or errors.
|
|
|
|
|
|
*/
|
2017-10-03 08:55:22 -07:00
|
|
|
|
@protocol FSTSyncEngineDelegate
|
2019-04-12 17:03:26 -07:00
|
|
|
|
- (void)handleViewSnapshots:(std::vector<core::ViewSnapshot> &&)viewSnapshots;
|
2017-10-03 08:55:22 -07:00
|
|
|
|
- (void)handleError:(NSError *)error forQuery:(FSTQuery *)query;
|
2019-04-12 17:03:26 -07:00
|
|
|
|
- (void)applyChangedOnlineState:(model::OnlineState)onlineState;
|
2017-10-03 08:55:22 -07:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* SyncEngine is the central controller in the client SDK architecture. It is the glue code
|
|
|
|
|
|
* between the EventManager, LocalStore, and RemoteStore. Some of SyncEngine's responsibilities
|
|
|
|
|
|
* include:
|
|
|
|
|
|
* 1. Coordinating client requests and remote events between the EventManager and the local and
|
|
|
|
|
|
* remote data stores.
|
|
|
|
|
|
* 2. Managing a View object for each query, providing the unified view between the local and
|
|
|
|
|
|
* remote data stores.
|
|
|
|
|
|
* 3. Notifying the RemoteStore when the LocalStore has new mutations in its queue that need
|
|
|
|
|
|
* sending to the backend.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The SyncEngine’s methods should only ever be called by methods running on our own worker
|
2018-11-18 17:49:32 -05:00
|
|
|
|
* queue.
|
2017-10-03 08:55:22 -07:00
|
|
|
|
*/
|
|
|
|
|
|
@interface FSTSyncEngine : NSObject <FSTRemoteSyncer>
|
|
|
|
|
|
|
|
|
|
|
|
- (instancetype)init NS_UNAVAILABLE;
|
|
|
|
|
|
- (instancetype)initWithLocalStore:(FSTLocalStore *)localStore
|
2019-04-12 17:03:26 -07:00
|
|
|
|
remoteStore:(remote::RemoteStore *)remoteStore
|
|
|
|
|
|
initialUser:(const auth::User &)user NS_DESIGNATED_INITIALIZER;
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
|
|
|
|
|
/**
|
2018-11-07 11:22:01 -08:00
|
|
|
|
* A delegate to be notified when queries being listened to produce new view snapshots or errors.
|
2017-10-03 08:55:22 -07:00
|
|
|
|
*/
|
2018-11-07 11:22:01 -08:00
|
|
|
|
@property(nonatomic, weak) id<FSTSyncEngineDelegate> syncEngineDelegate;
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Initiates a new listen. The FSTLocalStore will be queried for initial data and the listen will
|
2019-02-08 14:37:42 -05:00
|
|
|
|
* be sent to the `RemoteStore` to get remote data. The registered FSTSyncEngineDelegate will be
|
2017-10-03 08:55:22 -07:00
|
|
|
|
* notified of resulting view snapshots and/or listen errors.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return the target ID assigned to the query.
|
|
|
|
|
|
*/
|
2019-04-12 17:03:26 -07:00
|
|
|
|
- (model::TargetId)listenToQuery:(FSTQuery *)query;
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
|
|
|
|
|
/** Stops listening to a query previously listened to via listenToQuery:. */
|
|
|
|
|
|
- (void)stopListeningToQuery:(FSTQuery *)query;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Initiates the write of local mutation batch which involves adding the writes to the mutation
|
|
|
|
|
|
* queue, notifying the remote store about new mutations, and raising events for any changes this
|
|
|
|
|
|
* write caused. The provided completion block will be called once the write has been acked or
|
|
|
|
|
|
* rejected by the backend (or failed locally for any other reason).
|
|
|
|
|
|
*/
|
2019-02-07 22:03:17 -05:00
|
|
|
|
- (void)writeMutations:(std::vector<FSTMutation *> &&)mutations
|
|
|
|
|
|
completion:(FSTVoidErrorBlock)completion;
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Runs the given transaction block up to retries times and then calls completion.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param retries The number of times to try before giving up.
|
2018-11-18 17:49:32 -05:00
|
|
|
|
* @param workerQueue The queue to dispatch sync engine calls to.
|
2019-04-16 17:37:38 -07:00
|
|
|
|
* @param updateCallback The callback to call to execute the user's transaction.
|
|
|
|
|
|
* @param resultCallback The callback to call when the transaction is finished or failed.
|
2017-10-03 08:55:22 -07:00
|
|
|
|
*/
|
|
|
|
|
|
- (void)transactionWithRetries:(int)retries
|
2019-05-17 15:29:30 -07:00
|
|
|
|
workerQueue:(const std::shared_ptr<util::AsyncQueue> &)workerQueue
|
2019-04-16 17:37:38 -07:00
|
|
|
|
updateCallback:(core::TransactionUpdateCallback)updateCallback
|
|
|
|
|
|
resultCallback:(core::TransactionResultCallback)resultCallback;
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
2019-04-12 17:03:26 -07:00
|
|
|
|
- (void)credentialDidChangeWithUser:(const auth::User &)user;
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
2018-08-16 15:24:51 -07:00
|
|
|
|
/** Applies an OnlineState change to the sync engine and notifies any views of the change. */
|
2019-04-12 17:03:26 -07:00
|
|
|
|
- (void)applyChangedOnlineState:(model::OnlineState)onlineState;
|
2017-12-15 14:56:03 -08:00
|
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|