Skip to content

Commit 143c7c0

Browse files
authored
chore(datastore): Remove unused variable and cleanup for AWSDatastorePlugin (#2805)
1 parent d4a2913 commit 143c7c0

9 files changed

+161
-101
lines changed

AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/AWSDataStorePlugin+DataStoreBaseBehavior.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
6868
storageEngine.save(model,
6969
modelSchema: modelSchema,
7070
condition: condition,
71-
eagerLoad: isEagerLoad,
71+
eagerLoad: configuration.isEagerLoad,
7272
completion: publishingCompletion)
7373
}
7474

@@ -218,7 +218,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
218218
predicate: predicate,
219219
sort: sortInput,
220220
paginationInput: paginationInput,
221-
eagerLoad: isEagerLoad,
221+
eagerLoad: configuration.isEagerLoad,
222222
completion: completion)
223223
}
224224

AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/AWSDataStorePlugin+DataStoreSubscribeBehavior.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extension AWSDataStorePlugin: DataStoreSubscribeBehavior {
4646
sortInput: sortInput?.asSortDescriptors(),
4747
storageEngine: storageEngine,
4848
dataStorePublisher: dataStorePublisher,
49-
dataStoreConfiguration: dataStoreConfiguration,
49+
dataStoreConfiguration: configuration.pluginConfiguration,
5050
dispatchedModelSyncedEvent: dispatchedModelSyncedEvent,
5151
dataStoreStatePublisher: dataStoreStateSubject.eraseToAnyPublisher())
5252
return taskRunner.sequence

AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/AWSDataStorePlugin.swift

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
2626

2727
public var key: PluginKey = "awsDataStorePlugin"
2828

29-
/// `true` if any models are syncable. Resolved during configuration phase
30-
var isSyncEnabled: Bool
31-
32-
/// The listener on hub events unsubscribe token
33-
var hubListener: UnsubscribeToken?
34-
3529
/// The Publisher that sends mutation events to subscribers
3630
var dataStorePublisher: ModelSubcriptionBehavior?
3731

@@ -42,19 +36,16 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
4236
let modelRegistration: AmplifyModelRegistration
4337

4438
/// The DataStore configuration
45-
let dataStoreConfiguration: DataStoreConfiguration
46-
47-
/// A queue that regulates the execution of operations. This will be instantiated during initalization phase,
48-
/// and is clearable by `reset()`. This is implicitly unwrapped to be destroyed when resetting.
49-
var operationQueue: OperationQueue!
50-
51-
let validAPIPluginKey: String
52-
53-
let validAuthPluginKey: String
39+
var configuration: InternalDatastoreConfiguration
5440

5541
var storageEngine: StorageEngineBehavior!
42+
43+
/// A queue to allow synchronize access to the storage engine for start/stop/clear operations.
5644
var storageEngineInitQueue = DispatchQueue(label: "AWSDataStorePlugin.storageEngineInitQueue")
45+
46+
/// A queue used for async callback out from`storageEngineInitQueue`
5747
var queue = DispatchQueue(label: "AWSDataStorePlugin.queue", target: DispatchQueue.global())
48+
5849
var storageEngineBehaviorFactory: StorageEngineBehaviorFactory
5950

6051
var iStorageEngineSink: Any?
@@ -69,22 +60,26 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
6960
iStorageEngineSink = newValue
7061
}
7162
}
72-
73-
/// Configuration of the query against the local storage, whether it should load the belongs-to/has-one associations
74-
/// or not.
75-
var isEagerLoad: Bool = true
7663

7764
/// No-argument init that uses defaults for all providers
7865
public init(modelRegistration: AmplifyModelRegistration,
7966
configuration dataStoreConfiguration: DataStoreConfiguration = .default) {
8067
self.modelRegistration = modelRegistration
81-
self.dataStoreConfiguration = dataStoreConfiguration
82-
self.isSyncEnabled = false
83-
self.operationQueue = OperationQueue()
84-
self.validAPIPluginKey = "awsAPIPlugin"
85-
self.validAuthPluginKey = "awsCognitoAuthPlugin"
68+
self.configuration = InternalDatastoreConfiguration(
69+
isSyncEnabled: false,
70+
validAPIPluginKey: "awsAPIPlugin",
71+
validAuthPluginKey: "awsCognitoAuthPlugin",
72+
pluginConfiguration: dataStoreConfiguration)
73+
8674
self.storageEngineBehaviorFactory =
87-
StorageEngine.init(isSyncEnabled:dataStoreConfiguration:validAPIPluginKey:validAuthPluginKey:modelRegistryVersion:userDefault:)
75+
StorageEngine.init(
76+
isSyncEnabled:
77+
dataStoreConfiguration:
78+
validAPIPluginKey:
79+
validAuthPluginKey:
80+
modelRegistryVersion:
81+
userDefault:
82+
)
8883
self.dataStorePublisher = DataStorePublisher()
8984
self.dispatchedModelSyncedEvents = [:]
9085
}
@@ -98,15 +93,23 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
9893
validAPIPluginKey: String,
9994
validAuthPluginKey: String) {
10095
self.modelRegistration = modelRegistration
101-
self.dataStoreConfiguration = dataStoreConfiguration
102-
self.operationQueue = operationQueue
103-
self.isSyncEnabled = false
96+
self.configuration = InternalDatastoreConfiguration(
97+
isSyncEnabled: false,
98+
validAPIPluginKey: validAPIPluginKey,
99+
validAuthPluginKey: validAuthPluginKey,
100+
pluginConfiguration: dataStoreConfiguration)
101+
104102
self.storageEngineBehaviorFactory = storageEngineBehaviorFactory ??
105-
StorageEngine.init(isSyncEnabled:dataStoreConfiguration:validAPIPluginKey:validAuthPluginKey:modelRegistryVersion:userDefault:)
103+
StorageEngine.init(
104+
isSyncEnabled:
105+
dataStoreConfiguration:
106+
validAPIPluginKey:
107+
validAuthPluginKey:
108+
modelRegistryVersion:
109+
userDefault:
110+
)
106111
self.dataStorePublisher = dataStorePublisher
107112
self.dispatchedModelSyncedEvents = [:]
108-
self.validAPIPluginKey = validAPIPluginKey
109-
self.validAuthPluginKey = validAuthPluginKey
110113
}
111114

112115
/// By the time this method gets called, DataStore will already have invoked
@@ -117,11 +120,7 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
117120

118121
for modelSchema in ModelRegistry.modelSchemas {
119122
dispatchedModelSyncedEvents[modelSchema.name] = AtomicValue(initialValue: false)
120-
// `isEagerLoad` is true by default, unless the models contain the rootPath
121-
// which is indication of the codegen that supports for lazy loading.
122-
if isEagerLoad && ModelRegistry.modelType(from: modelSchema.name)?.rootPath != nil {
123-
isEagerLoad = false
124-
}
123+
configuration.updateIsEagerLoad(modelSchema: modelSchema)
125124
}
126125
resolveSyncEnabled()
127126
ModelListDecoderRegistry.registerDecoder(DataStoreListDecoder.self)
@@ -141,7 +140,7 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
141140
if self.dataStorePublisher == nil {
142141
self.dataStorePublisher = DataStorePublisher()
143142
}
144-
try resolveStorageEngine(dataStoreConfiguration: dataStoreConfiguration)
143+
try resolveStorageEngine(dataStoreConfiguration: configuration.pluginConfiguration)
145144
try storageEngine.setUp(modelSchemas: ModelRegistry.modelSchemas)
146145
try storageEngine.applyModelMigrations(modelSchemas: ModelRegistry.modelSchemas)
147146

@@ -176,20 +175,22 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
176175
return
177176
}
178177

179-
storageEngine = try storageEngineBehaviorFactory(isSyncEnabled,
180-
dataStoreConfiguration,
181-
validAPIPluginKey,
182-
validAuthPluginKey,
183-
modelRegistration.version,
184-
UserDefaults.standard)
178+
storageEngine = try storageEngineBehaviorFactory(
179+
configuration.isSyncEnabled,
180+
dataStoreConfiguration,
181+
configuration.validAPIPluginKey,
182+
configuration.validAuthPluginKey,
183+
modelRegistration.version,
184+
UserDefaults.standard
185+
)
185186

186187
setupStorageSink()
187188
}
188189

189190
// MARK: Private
190191

191192
private func resolveSyncEnabled() {
192-
isSyncEnabled = ModelRegistry.hasSyncableModels
193+
configuration.updateIsSyncEnabled(ModelRegistry.hasSyncableModels)
193194
}
194195

195196
private func setupStorageSink() {
@@ -248,14 +249,7 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
248249
}
249250

250251
public func reset() async {
251-
if operationQueue != nil {
252-
operationQueue = nil
253-
}
254252
dispatchedModelSyncedEvents = [:]
255-
if let listener = hubListener {
256-
Amplify.Hub.removeListener(listener)
257-
hubListener = nil
258-
}
259253
if let resettable = storageEngine as? Resettable {
260254
log.verbose("Resetting storageEngine")
261255
await resettable.reset()
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
import Amplify
10+
import AWSPluginsCore
11+
12+
extension DataStoreConfiguration {
13+
14+
public static let defaultSyncInterval: TimeInterval = .hours(24)
15+
public static let defaultSyncMaxRecords: UInt = 10_000
16+
public static let defaultSyncPageSize: UInt = 1_000
17+
18+
/// Creates a custom configuration. The only required property is `conflictHandler`.
19+
///
20+
/// - Parameters:
21+
/// - errorHandler: a callback function called on unhandled errors
22+
/// - conflictHandler: a callback called when a conflict could not be resolved by the service
23+
/// - syncInterval: how often the sync engine will run (in seconds)
24+
/// - syncMaxRecords: the number of records to sync per execution
25+
/// - syncPageSize: the page size of each sync execution
26+
/// - authModeStrategy: authorization strategy (.default | multiauth)
27+
/// - Returns: an instance of `DataStoreConfiguration` with the passed parameters.
28+
public static func custom(
29+
errorHandler: @escaping DataStoreErrorHandler = { error in
30+
Amplify.Logging.error(error: error)
31+
},
32+
conflictHandler: @escaping DataStoreConflictHandler = { _, resolve in
33+
resolve(.applyRemote)
34+
},
35+
syncInterval: TimeInterval = DataStoreConfiguration.defaultSyncInterval,
36+
syncMaxRecords: UInt = DataStoreConfiguration.defaultSyncMaxRecords,
37+
syncPageSize: UInt = DataStoreConfiguration.defaultSyncPageSize,
38+
syncExpressions: [DataStoreSyncExpression] = [],
39+
authModeStrategy: AuthModeStrategyType = .default
40+
) -> DataStoreConfiguration {
41+
return DataStoreConfiguration(errorHandler: errorHandler,
42+
conflictHandler: conflictHandler,
43+
syncInterval: syncInterval,
44+
syncMaxRecords: syncMaxRecords,
45+
syncPageSize: syncPageSize,
46+
syncExpressions: syncExpressions,
47+
authModeStrategy: authModeStrategy)
48+
}
49+
50+
/// The default configuration.
51+
public static var `default`: DataStoreConfiguration {
52+
.custom()
53+
}
54+
55+
}

AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/DataStoreConfiguration.swift renamed to AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Configuration/DataStoreConfiguration.swift

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -87,48 +87,3 @@ public struct DataStoreConfiguration {
8787
}
8888

8989
}
90-
91-
extension DataStoreConfiguration {
92-
93-
public static let defaultSyncInterval: TimeInterval = .hours(24)
94-
public static let defaultSyncMaxRecords: UInt = 10_000
95-
public static let defaultSyncPageSize: UInt = 1_000
96-
97-
/// Creates a custom configuration. The only required property is `conflictHandler`.
98-
///
99-
/// - Parameters:
100-
/// - errorHandler: a callback function called on unhandled errors
101-
/// - conflictHandler: a callback called when a conflict could not be resolved by the service
102-
/// - syncInterval: how often the sync engine will run (in seconds)
103-
/// - syncMaxRecords: the number of records to sync per execution
104-
/// - syncPageSize: the page size of each sync execution
105-
/// - authModeStrategy: authorization strategy (.default | multiauth)
106-
/// - Returns: an instance of `DataStoreConfiguration` with the passed parameters.
107-
public static func custom(
108-
errorHandler: @escaping DataStoreErrorHandler = { error in
109-
Amplify.Logging.error(error: error)
110-
},
111-
conflictHandler: @escaping DataStoreConflictHandler = { _, resolve in
112-
resolve(.applyRemote)
113-
},
114-
syncInterval: TimeInterval = DataStoreConfiguration.defaultSyncInterval,
115-
syncMaxRecords: UInt = DataStoreConfiguration.defaultSyncMaxRecords,
116-
syncPageSize: UInt = DataStoreConfiguration.defaultSyncPageSize,
117-
syncExpressions: [DataStoreSyncExpression] = [],
118-
authModeStrategy: AuthModeStrategyType = .default
119-
) -> DataStoreConfiguration {
120-
return DataStoreConfiguration(errorHandler: errorHandler,
121-
conflictHandler: conflictHandler,
122-
syncInterval: syncInterval,
123-
syncMaxRecords: syncMaxRecords,
124-
syncPageSize: syncPageSize,
125-
syncExpressions: syncExpressions,
126-
authModeStrategy: authModeStrategy)
127-
}
128-
129-
/// The default configuration.
130-
public static var `default`: DataStoreConfiguration {
131-
.custom()
132-
}
133-
134-
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
import Amplify
10+
11+
struct InternalDatastoreConfiguration {
12+
13+
/// `true` if any models are syncable. Resolved during configuration phase
14+
var isSyncEnabled: Bool
15+
16+
/// Configuration of the query against the local storage, whether it should load
17+
/// the belongs-to/has-one associations or not.
18+
///
19+
///`isEagerLoad` is true by default, unless the models contain the rootPath
20+
/// which is indication of the codegen that supports for lazy loading.
21+
var isEagerLoad: Bool = true
22+
23+
/// Identifier used to access the API plugin added to Amplify by api
24+
/// `Amplify.API.getPlugin(for: identifier)`
25+
let validAPIPluginKey: String
26+
27+
/// Identifier used to access the Auth plugin added to Amplify by api
28+
/// `Amplify.Auth.getPlugin(for: identifier)`
29+
let validAuthPluginKey: String
30+
31+
/// Configuration provided during Datastore initialization, this is a `public` configuration.
32+
let pluginConfiguration: DataStoreConfiguration
33+
34+
mutating func updateIsSyncEnabled(_ isEnabled: Bool) {
35+
self.isSyncEnabled = isEnabled
36+
}
37+
38+
mutating func updateIsEagerLoad(modelSchema: ModelSchema) {
39+
guard isEagerLoad else {
40+
return
41+
}
42+
43+
if ModelRegistry.modelType(from: modelSchema.name)?.rootPath != nil {
44+
isEagerLoad = false
45+
}
46+
}
47+
}

AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Migration/MutationSyncMetadataMigration.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import Amplify
99
import Foundation
1010
import AWSPluginsCore
1111

12+
/// Migrates `MutationSyncMetadata` to the new format.
13+
///
14+
/// Format of the `id` in `MutationSyncMetadata` has changed to support unique ids
15+
/// across mutiple model types. Earlier model id is repalced with id of the format `{modelName}|{modelId}`
16+
///
1217
class MutationSyncMetadataMigration: ModelMigration {
1318

1419
weak var delegate: MutationSyncMetadataMigrationDelegate?

AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Storage/ModelStorageBehavior.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import Amplify
99

1010
protocol ModelStorageBehavior {
11+
12+
/// Setup the model store with the given schema
1113
func setUp(modelSchemas: [ModelSchema]) throws
1214

15+
/// Apply any data migration logic for the given schemas in the underlying data store.
1316
func applyModelMigrations(modelSchemas: [ModelSchema]) throws
1417

1518
func save<M: Model>(_ model: M,

AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Storage/SQLite/StorageEngineAdapter+SQLite.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ final class SQLiteStorageEngineAdapter: StorageEngineAdapter {
114114
}
115115

116116
func applyModelMigrations(modelSchemas: [ModelSchema]) throws {
117-
let delegate = SQLiteMutationSyncMetadataMigrationDelegate(storageAdapter: self,
118-
modelSchemas: modelSchemas)
117+
let delegate = SQLiteMutationSyncMetadataMigrationDelegate(
118+
storageAdapter: self,
119+
modelSchemas: modelSchemas)
119120
let modelMigration = MutationSyncMetadataMigration(delegate: delegate)
120121
let modelMigrations = ModelMigrations(modelMigrations: [modelMigration])
121122
do {

0 commit comments

Comments
 (0)