Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datastore): only start the remote sync engine if we have awsapiplugin #442

Merged
merged 3 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
/// The DataStore configuration
let dataStoreConfiguration: DataStoreConfiguration

let validAPIPluginKey: String
/// The local storage provider. Resolved during configuration phase
var storageEngine: StorageEngineBehavior!

Expand All @@ -46,6 +47,7 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
self.modelRegistration = modelRegistration
self.dataStoreConfiguration = dataStoreConfiguration
self.isSyncEnabled = false
self.validAPIPluginKey = "awsAPIPlugin"
if #available(iOS 13.0, *) {
self.dataStorePublisher = DataStorePublisher()
} else {
Expand All @@ -57,12 +59,14 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
init(modelRegistration: AmplifyModelRegistration,
configuration dataStoreConfiguration: DataStoreConfiguration = .default,
storageEngine: StorageEngineBehavior,
dataStorePublisher: DataStoreSubscribeBehavior) {
dataStorePublisher: DataStoreSubscribeBehavior,
validAPIPluginKey: String) {
self.modelRegistration = modelRegistration
self.dataStoreConfiguration = dataStoreConfiguration
self.isSyncEnabled = false
self.storageEngine = storageEngine
self.dataStorePublisher = dataStorePublisher
self.validAPIPluginKey = validAPIPluginKey
}

/// By the time this method gets called, DataStore will already have invoked
Expand All @@ -78,13 +82,27 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
let filter = HubFilters.forEventName(HubPayload.EventName.Amplify.configured)
var token: UnsubscribeToken?
token = Amplify.Hub.listen(to: .dataStore, isIncluded: filter) { _ in
self.storageEngine.startSync()
if self.hasValidAPIPlugin() {
self.storageEngine.startSync()
} else {
self.log.info("Unable to find suitable plugin for syncEngine. syncEngine will not be started")
}

if let token = token {
Amplify.Hub.removeListener(token)
}
}
}

func hasValidAPIPlugin() -> Bool {
do {
_ = try Amplify.API.getPlugin(for: validAPIPluginKey)
return true
} catch {
return false
}
}

func reinitStorageEngineIfNeeded() {
if storageEngine != nil {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ extension APICategoryDependencyTests {
let dataStorePublisher = DataStorePublisher()
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
storageEngine: storageEngine,
dataStorePublisher: dataStorePublisher)
dataStorePublisher: dataStorePublisher,
validAPIPluginKey: "MockAPICategoryPlugin")
try Amplify.add(plugin: dataStorePlugin)

let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class LocalSubscriptionTests: XCTestCase {
let dataStorePublisher = DataStorePublisher()
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
storageEngine: storageEngine,
dataStorePublisher: dataStorePublisher)
dataStorePublisher: dataStorePublisher,
validAPIPluginKey: "MockAPICategoryPlugin")

let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [
"awsDataStorePlugin": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class AWSMutationEventIngesterTests: XCTestCase {
let publisher = DataStorePublisher()
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
storageEngine: storageEngine,
dataStorePublisher: publisher)
dataStorePublisher: publisher,
validAPIPluginKey: "MockAPICategoryPlugin")

try Amplify.add(plugin: apiPlugin)
try Amplify.add(plugin: dataStorePlugin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ extension OutgoingMutationQueueMockStateTest {
let dataStorePublisher = DataStorePublisher()
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
storageEngine: storageEngine,
dataStorePublisher: dataStorePublisher)
dataStorePublisher: dataStorePublisher,
validAPIPluginKey: "MockAPICategoryPlugin")
try Amplify.add(plugin: dataStorePlugin)
let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [
"awsDataStorePlugin": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,8 @@ extension ProcessMutationErrorFromCloudOperationTests {
let dataStorePublisher = DataStorePublisher()
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
storageEngine: storageEngine,
dataStorePublisher: dataStorePublisher)
dataStorePublisher: dataStorePublisher,
validAPIPluginKey: "MockAPICategoryPlugin")
try Amplify.add(plugin: dataStorePlugin)
let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [
"awsDataStorePlugin": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ extension SyncMutationToCloudOperationTests {
let dataStorePublisher = DataStorePublisher()
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
storageEngine: storageEngine,
dataStorePublisher: dataStorePublisher)
dataStorePublisher: dataStorePublisher,
validAPIPluginKey: "MockAPICategoryPlugin")

try Amplify.add(plugin: dataStorePlugin)
let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [
"awsDataStorePlugin": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class RemoteSyncAPIInvocationTests: XCTestCase {
let dataStorePublisher = DataStorePublisher()
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
storageEngine: storageEngine,
dataStorePublisher: dataStorePublisher)
dataStorePublisher: dataStorePublisher,
validAPIPluginKey: "MockAPICategoryPlugin")

let apiConfig = APICategoryConfiguration(plugins: [apiPlugin.key: true])
let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [dataStorePlugin.key: true])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ extension ReconcileAndLocalSaveOperationTests {
let dataStorePublisher = DataStorePublisher()
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
storageEngine: storageEngine,
dataStorePublisher: dataStorePublisher)
dataStorePublisher: dataStorePublisher,
validAPIPluginKey: "MockAPICategoryPlugin")
try Amplify.add(plugin: dataStorePlugin)
let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [
"awsDataStorePlugin": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class BaseDataStoreTests: XCTestCase {
let dataStorePublisher = DataStorePublisher()
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: TestModelRegistration(),
storageEngine: storageEngine,
dataStorePublisher: dataStorePublisher)
dataStorePublisher: dataStorePublisher,
validAPIPluginKey: "MockAPICategoryPlugin")

let dataStoreConfig = DataStoreCategoryConfiguration(plugins: [
"awsDataStorePlugin": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ class SyncEngineTestBase: XCTestCase {
let publisher = DataStorePublisher()
let dataStorePlugin = AWSDataStorePlugin(modelRegistration: modelRegistration,
storageEngine: storageEngine,
dataStorePublisher: publisher)
dataStorePublisher: publisher,
validAPIPluginKey: "MockAPICategoryPlugin")

try Amplify.add(plugin: dataStorePlugin)
}
Expand Down