|
| 1 | +// |
| 2 | +// Copyright 2018-2020 Amazon.com, |
| 3 | +// Inc. or its affiliates. All Rights Reserved. |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: Apache-2.0 |
| 6 | +// |
| 7 | + |
| 8 | +import Amplify |
| 9 | +import Foundation |
| 10 | + |
| 11 | +/// Error Handler function typealias |
| 12 | +public typealias DataStoreErrorHandler = (AmplifyError) -> Void |
| 13 | + |
| 14 | +/// Holds a reference to both the local `Model` and the remote one during a conflict |
| 15 | +/// resolution. Implementations of the `DataStoreConflictHandler` use this to decide |
| 16 | +/// what the outcome of a conflict should be. |
| 17 | +public struct DataStoreConclictData { |
| 18 | + public let local: Model |
| 19 | + public let remote: Model |
| 20 | +} |
| 21 | + |
| 22 | +/// Conflict Handler function typealias. The function is used during a conflict that |
| 23 | +/// could not be resolved and requires a decision from the consumer. |
| 24 | +public typealias DataStoreConflictHandler = (DataStoreConclictData, DataStoreConflictHandlerResolver) -> Void |
| 25 | + |
| 26 | +/// Callback for the `DataStoreConflictHandler`. |
| 27 | +public typealias DataStoreConflictHandlerResolver = (DataStoreConflictHandlerResult) -> Void |
| 28 | + |
| 29 | +/// The conflict resolution result enum. |
| 30 | +public enum DataStoreConflictHandlerResult { |
| 31 | + |
| 32 | + /// Discard the local changes in favor of the remote ones. |
| 33 | + case discard |
| 34 | + |
| 35 | + /// Keep the local changes (semantic shortcut to `retry(local)`). |
| 36 | + case keep |
| 37 | + |
| 38 | + /// Return a new `Model` instance that should used instead of the local and remote changes. |
| 39 | + case retry(Model) |
| 40 | +} |
| 41 | + |
| 42 | +/// The `DataStore` plugin configuration object. |
| 43 | +public struct DataStoreConfiguration { |
| 44 | + |
| 45 | + /// A callback function called on unhandled errors |
| 46 | + public let errorHandler: DataStoreErrorHandler |
| 47 | + |
| 48 | + /// A callback called when a conflict could not be resolved by the service |
| 49 | + public let conflictHandler: DataStoreConflictHandler |
| 50 | + |
| 51 | + /// How often the sync engine will run (in seconds) |
| 52 | + public let syncInterval: TimeInterval |
| 53 | + |
| 54 | + /// The number of records to sync per execution |
| 55 | + public let syncMaxRecords: UInt |
| 56 | + |
| 57 | + /// The page size of each sync execution |
| 58 | + public let syncPageSize: UInt |
| 59 | + |
| 60 | + init(errorHandler: @escaping DataStoreErrorHandler, |
| 61 | + conflictHandler: @escaping DataStoreConflictHandler, |
| 62 | + syncInterval: TimeInterval, |
| 63 | + syncMaxRecords: UInt, |
| 64 | + syncPageSize: UInt) { |
| 65 | + self.errorHandler = errorHandler |
| 66 | + self.conflictHandler = conflictHandler |
| 67 | + self.syncInterval = syncInterval |
| 68 | + self.syncMaxRecords = syncMaxRecords |
| 69 | + self.syncPageSize = syncPageSize |
| 70 | + } |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | +extension DataStoreConfiguration { |
| 75 | + |
| 76 | + public static let defaultSyncInterval: TimeInterval = .hours(24) |
| 77 | + public static let defaultSyncMaxRecords: UInt = 10_000 |
| 78 | + public static let defaultSyncPageSize: UInt = 1_000 |
| 79 | + |
| 80 | + /// Creates a custom configuration. The only required property is `conflictHandler`. |
| 81 | + /// |
| 82 | + /// - Parameters: |
| 83 | + /// - errorHandler: a callback function called on unhandled errors |
| 84 | + /// - conflictHandler: a callback called when a conflict could not be resolved by the service |
| 85 | + /// - syncInterval: how often the sync engine will run (in seconds) |
| 86 | + /// - syncMaxRecords: the number of records to sync per execution |
| 87 | + /// - syncPageSize: the page size of each sync execution |
| 88 | + /// - Returns: an instance of `DataStoreConfiguration` with the passed parameters. |
| 89 | + public static func custom( |
| 90 | + errorHandler: @escaping DataStoreErrorHandler = { error in |
| 91 | + Amplify.Logging.error(error: error) |
| 92 | + }, |
| 93 | + conflictHandler: @escaping DataStoreConflictHandler = { _, resolve in |
| 94 | + resolve(.discard) |
| 95 | + }, |
| 96 | + syncInterval: TimeInterval = DataStoreConfiguration.defaultSyncInterval, |
| 97 | + syncMaxRecords: UInt = DataStoreConfiguration.defaultSyncMaxRecords, |
| 98 | + syncPageSize: UInt = DataStoreConfiguration.defaultSyncPageSize |
| 99 | + ) -> DataStoreConfiguration { |
| 100 | + return DataStoreConfiguration(errorHandler: errorHandler, |
| 101 | + conflictHandler: conflictHandler, |
| 102 | + syncInterval: syncInterval, |
| 103 | + syncMaxRecords: syncMaxRecords, |
| 104 | + syncPageSize: syncPageSize) |
| 105 | + } |
| 106 | + |
| 107 | + /// The default configuration. |
| 108 | + public static var `default`: DataStoreConfiguration { |
| 109 | + .custom() |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments