Skip to content

Commit

Permalink
fix(datastore): only start the remote sync engine if we have awsapipl…
Browse files Browse the repository at this point in the history
…ugin (#442)

* fix(datastore): only start the remote sync engine if we have awsapiplugin
  • Loading branch information
wooj2 authored May 14, 2020
1 parent 5dc1546 commit 532058a
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 12 deletions.
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

0 comments on commit 532058a

Please sign in to comment.