diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 78a4507b1..022ffc985 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -806,7 +806,7 @@ extension DatabasePool: DatabaseReader { // MARK: - Database Observation /// :nodoc: - public func _add( + public func _add( observation: ValueObservation, scheduling scheduler: ValueObservationScheduler, onChange: @escaping (Reducer.Value) -> Void) @@ -836,7 +836,7 @@ extension DatabasePool: DatabaseReader { /// A concurrent observation fetches the initial value without waiting for /// the writer. - private func _addConcurrent( + private func _addConcurrent( observation: ValueObservation, scheduling scheduler: ValueObservationScheduler, onChange: @escaping (Reducer.Value) -> Void) @@ -926,7 +926,7 @@ extension DatabasePool: DatabaseReader { } // Support for _addConcurrent(observation:) - private func add( + private func add( observer: ValueObserver, from initialSnapshot: DatabaseSnapshot) { diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index b3d7db68f..3ec0ae5e1 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -473,7 +473,7 @@ extension DatabaseQueue { // MARK: - Database Observation /// :nodoc: - public func _add( + public func _add( observation: ValueObservation, scheduling scheduler: ValueObservationScheduler, onChange: @escaping (Reducer.Value) -> Void) diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index 1a76555cb..3108d9076 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -3,16 +3,17 @@ import Combine #endif import Dispatch -/// The protocol for all types that can fetch values from a database. +/// `DatabaseReader` is the protocol for all types that can fetch values from +/// an SQLite database. /// -/// It is adopted by DatabaseQueue and DatabasePool. +/// It is adopted by `DatabaseQueue`, `DatabasePool`, and `DatabaseSnapshot`. /// /// The protocol comes with isolation guarantees that describe the behavior of /// adopting types in a multithreaded application. /// /// Types that adopt the protocol can provide in practice stronger guarantees. -/// For example, DatabaseQueue provides a stronger isolation level -/// than DatabasePool. +/// For example, `DatabaseQueue` provides a stronger isolation level +/// than `DatabasePool`. /// /// **Warning**: Isolation guarantees stand as long as there is no external /// connection to the database. Should you have to cope with external @@ -252,7 +253,7 @@ public protocol DatabaseReader: AnyObject { /// - returns: a TransactionObserver /// /// :nodoc: - func _add( + func _add( observation: ValueObservation, scheduling scheduler: ValueObservationScheduler, onChange: @escaping (Reducer.Value) -> Void) @@ -378,7 +379,7 @@ extension DatabaseReader { /// Adding an observation in a read-only database emits only the /// initial value. - func _addReadOnly( + func _addReadOnly( observation: ValueObservation, scheduling scheduler: ValueObservationScheduler, onChange: @escaping (Reducer.Value) -> Void) @@ -486,7 +487,7 @@ public final class AnyDatabaseReader: DatabaseReader { // MARK: - Value Observation /// :nodoc: - public func _add( + public func _add( observation: ValueObservation, scheduling scheduler: ValueObservationScheduler, onChange: @escaping (Reducer.Value) -> Void) diff --git a/GRDB/Core/DatabaseRegion.swift b/GRDB/Core/DatabaseRegion.swift index bdec7342c..f75ec2823 100644 --- a/GRDB/Core/DatabaseRegion.swift +++ b/GRDB/Core/DatabaseRegion.swift @@ -315,6 +315,10 @@ private struct TableRegion: Equatable { // MARK: - DatabaseRegionConvertible +/// `DatabaseRegionConvertible` is the protocol for values that can be turned +/// into a `DatabaseRegion`. +/// +/// Such values specify the region obserbed by `DatabaseRegionObservation`. public protocol DatabaseRegionConvertible { /// Returns a database region. /// diff --git a/GRDB/Core/DatabaseSnapshot.swift b/GRDB/Core/DatabaseSnapshot.swift index 0ba392732..edd3edbeb 100644 --- a/GRDB/Core/DatabaseSnapshot.swift +++ b/GRDB/Core/DatabaseSnapshot.swift @@ -151,7 +151,7 @@ extension DatabaseSnapshot { // MARK: - Database Observation /// :nodoc: - public func _add( + public func _add( observation: ValueObservation, scheduling scheduler: ValueObservationScheduler, onChange: @escaping (Reducer.Value) -> Void) diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index 9ec545709..a6e8c1509 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -3,16 +3,17 @@ import Combine #endif import Dispatch -/// The protocol for all types that can update a database. +/// `DatabaseWriter` is the protocol for all types that can write into an +/// SQLite database. /// -/// It is adopted by DatabaseQueue and DatabasePool. +/// It is adopted by `DatabaseQueue` and `DatabasePool`. /// /// The protocol comes with isolation guarantees that describe the behavior of /// adopting types in a multithreaded application. /// /// Types that adopt the protocol can in practice provide stronger guarantees. -/// For example, DatabaseQueue provides a stronger isolation level -/// than DatabasePool. +/// For example, `DatabaseQueue` provides a stronger isolation level +/// than `DatabasePool`. /// /// **Warning**: Isolation guarantees stand as long as there is no external /// connection to the database. Should you have to cope with external @@ -312,7 +313,7 @@ extension DatabaseWriter { // MARK: - Database Observation /// A write-only observation only uses the serialized writer - func _addWriteOnly( + func _addWriteOnly( observation: ValueObservation, scheduling scheduler: ValueObservationScheduler, onChange: @escaping (Reducer.Value) -> Void) @@ -488,10 +489,10 @@ extension DatabasePublishers { /// /// See: /// - /// - `DatabaseReader.writePublisher(updates:)`. - /// - `DatabaseReader.writePublisher(updates:thenRead:)`. - /// - `DatabaseReader.writePublisher(receiveOn:updates:)`. - /// - `DatabaseReader.writePublisher(receiveOn:updates:thenRead:)`. + /// - `DatabaseWriter.writePublisher(updates:)`. + /// - `DatabaseWriter.writePublisher(updates:thenRead:)`. + /// - `DatabaseWriter.writePublisher(receiveOn:updates:)`. + /// - `DatabaseWriter.writePublisher(receiveOn:updates:thenRead:)`. public struct Write: Publisher { public typealias Output = Output public typealias Failure = Error @@ -662,7 +663,7 @@ public final class AnyDatabaseWriter: DatabaseWriter { // MARK: - Database Observation /// :nodoc: - public func _add( + public func _add( observation: ValueObservation, scheduling scheduler: ValueObservationScheduler, onChange: @escaping (Reducer.Value) -> Void) diff --git a/GRDB/Core/FetchRequest.swift b/GRDB/Core/FetchRequest.swift index aab20ce65..1f182b198 100644 --- a/GRDB/Core/FetchRequest.swift +++ b/GRDB/Core/FetchRequest.swift @@ -1,6 +1,6 @@ // MARK: - FetchRequest -/// Implementation details of FetchRequest. +/// Implementation details of `FetchRequest`. /// /// :nodoc: public protocol _FetchRequest { @@ -15,6 +15,7 @@ public protocol _FetchRequest { /// let request: ... // Some FetchRequest that fetches Player /// try request.fetchCursor(db) // Cursor of Player /// try request.fetchAll(db) // [Player] +/// try request.fetchSet(db) // Set /// try request.fetchOne(db) // Player? /// try request.fetchCount(db) // Int public protocol FetchRequest: _FetchRequest, DatabaseRegionConvertible, SQLExpression, SQLCollection { diff --git a/GRDB/Core/TransactionObserver.swift b/GRDB/Core/TransactionObserver.swift index 96a9d47dd..d284e2924 100644 --- a/GRDB/Core/TransactionObserver.swift +++ b/GRDB/Core/TransactionObserver.swift @@ -877,7 +877,7 @@ typealias StatementObservation = (TransactionObservation, DatabaseEventPredicate // MARK: - Database events -/// A kind of database event. See the TransactionObserver protocol for +/// A kind of database event. See the `TransactionObserver` protocol for /// more information. public enum DatabaseEventKind { /// The insertion of a row in a database table diff --git a/GRDB/Fixit/GRDB-5.0.swift b/GRDB/Fixit/GRDB-5.0.swift index 9c69272fe..c54ab45ab 100644 --- a/GRDB/Fixit/GRDB-5.0.swift +++ b/GRDB/Fixit/GRDB-5.0.swift @@ -20,8 +20,8 @@ extension AnyFetchRequest { } @available(*, unavailable, message: "Custom reducers are no longer supported. See the \"Migrating From GRDB 4 to GRDB 5\" guide.") -public struct AnyValueReducer: _ValueReducer { - public var isSelectedRegionDeterministic: Bool +public struct AnyValueReducer: ValueReducer { + public var _isSelectedRegionDeterministic: Bool { preconditionFailure() } public init(fetch: @escaping (Database) throws -> Fetched, value: @escaping (Fetched) -> Value?) @@ -30,10 +30,10 @@ public struct AnyValueReducer: _ValueReducer { public init(_ reducer: Base) where Base.Fetched == Fetched, Base.Value == Value { preconditionFailure() } - public func fetch(_ db: Database) throws -> Fetched + public func _fetch(_ db: Database) throws -> Fetched { preconditionFailure() } - public func value(_ fetched: Fetched) -> Value? + public func _value(_ fetched: Fetched) -> Value? { preconditionFailure() } } @@ -522,20 +522,17 @@ extension ValueObservation where Reducer.Value: Equatable { extension ValueReducers { @available(*, unavailable) - public enum Unavailable: _ValueReducer { - public var isSelectedRegionDeterministic: Bool + public enum Unavailable: ValueReducer { + public var _isSelectedRegionDeterministic: Bool { preconditionFailure() } - public func fetch(_ db: Database) throws -> Never + public func _fetch(_ db: Database) throws -> Never { preconditionFailure() } - public mutating func value(_ fetched: Never) -> T? { } + public mutating func _value(_ fetched: Never) -> T? { } } } -@available(*, unavailable, message: "Custom reducers are no longer supported. See the \"Migrating From GRDB 4 to GRDB 5\" guide.") -typealias ValueReducer = _ValueReducer - @available(*, unavailable, message: "ValueObservation now schedules its values asynchronously on the main queue by default. See ValueObservation.start() for possible configuration") enum ValueScheduling { case mainQueue diff --git a/GRDB/QueryInterface/Request/Association/Association.swift b/GRDB/QueryInterface/Request/Association/Association.swift index 8647b6725..f500389b1 100644 --- a/GRDB/QueryInterface/Request/Association/Association.swift +++ b/GRDB/QueryInterface/Request/Association/Association.swift @@ -1,6 +1,6 @@ import Foundation -/// Implementation details of Association. +/// Implementation details of `Association`. /// /// :nodoc: public protocol _Association { diff --git a/GRDB/QueryInterface/Request/RequestProtocols.swift b/GRDB/QueryInterface/Request/RequestProtocols.swift index eb08811e6..b13cdcde5 100644 --- a/GRDB/QueryInterface/Request/RequestProtocols.swift +++ b/GRDB/QueryInterface/Request/RequestProtocols.swift @@ -493,7 +493,7 @@ extension OrderedRequest { // MARK: - JoinableRequest -/// Implementation details of the JoinableRequest protocol. +/// Implementation details of `JoinableRequest`. /// /// :nodoc: public protocol _JoinableRequest { diff --git a/GRDB/QueryInterface/SQL/SQLCollection.swift b/GRDB/QueryInterface/SQL/SQLCollection.swift index 6310b684d..d5699c1d7 100644 --- a/GRDB/QueryInterface/SQL/SQLCollection.swift +++ b/GRDB/QueryInterface/SQL/SQLCollection.swift @@ -1,6 +1,6 @@ // MARK: - SQLCollection -/// Implementation details of SQLCollection. +/// Implementation details of `SQLCollection`. /// /// :nodoc: public protocol _SQLCollection { diff --git a/GRDB/QueryInterface/SQL/SQLExpression.swift b/GRDB/QueryInterface/SQL/SQLExpression.swift index e2512cc11..41a7540a0 100644 --- a/GRDB/QueryInterface/SQL/SQLExpression.swift +++ b/GRDB/QueryInterface/SQL/SQLExpression.swift @@ -1,6 +1,6 @@ // MARK: - SQLExpression -/// Implementation details of SQLExpression. +/// Implementation details of `SQLExpression`. /// /// :nodoc: public protocol _SQLExpression { diff --git a/GRDB/QueryInterface/SQL/SQLFunctions.swift b/GRDB/QueryInterface/SQL/SQLFunctions.swift index f312068c8..c6a9e2cbb 100644 --- a/GRDB/QueryInterface/SQL/SQLFunctions.swift +++ b/GRDB/QueryInterface/SQL/SQLFunctions.swift @@ -182,22 +182,48 @@ extension SQLSpecificExpressible { // MARK: - Date functions -/// A date modifier for SQLite date functions. +/// A date modifier for SQLite date functions such as `julianDay(_:_:)` and +/// `dateTime(_:_:)`. /// /// For more information, see https://www.sqlite.org/lang_datefunc.html public enum SQLDateModifier: SQLExpression { - case day(Int) - case hour(Int) - case minute(Int) + /// Adds the specified amount of seconds case second(Double) + + /// Adds the specified amount of minutes + case minute(Int) + + /// Adds the specified amount of hours + case hour(Int) + + /// Adds the specified amount of days + case day(Int) + + /// Adds the specified amount of months case month(Int) + + /// Adds the specified amount of years case year(Int) - case startOfMonth - case startOfYear + + /// Shifts the date backwards to the beginning of the current day case startOfDay + + /// Shifts the date backwards to the beginning of the current month + case startOfMonth + + /// Shifts the date backwards to the beginning of the current year + case startOfYear + + /// See https://www.sqlite.org/lang_datefunc.html case weekday(Int) + + /// See https://www.sqlite.org/lang_datefunc.html case unixEpoch + + /// See https://www.sqlite.org/lang_datefunc.html case localTime + + /// See https://www.sqlite.org/lang_datefunc.html case utc var rawValue: String { diff --git a/GRDB/QueryInterface/SQL/SQLOrdering.swift b/GRDB/QueryInterface/SQL/SQLOrdering.swift index 6c3d77ae8..54785a7d7 100644 --- a/GRDB/QueryInterface/SQL/SQLOrdering.swift +++ b/GRDB/QueryInterface/SQL/SQLOrdering.swift @@ -1,6 +1,6 @@ // MARK: - SQLOrderingTerm -/// Implementation details of SQLOrderingTerm. +/// Implementation details of `SQLOrderingTerm`. /// /// :nodoc: public protocol _SQLOrderingTerm { @@ -16,7 +16,6 @@ public protocol _SQLOrderingTerm { /// The protocol for all types that can be used as an SQL ordering term, as /// described at https://www.sqlite.org/syntax/ordering-term.html -/// public protocol SQLOrderingTerm: _SQLOrderingTerm { } // MARK: - _SQLOrdering diff --git a/GRDB/QueryInterface/SQL/SQLSelectable.swift b/GRDB/QueryInterface/SQL/SQLSelectable.swift index 3e04252a2..46b156f55 100644 --- a/GRDB/QueryInterface/SQL/SQLSelectable.swift +++ b/GRDB/QueryInterface/SQL/SQLSelectable.swift @@ -1,6 +1,6 @@ // MARK: - SQLSelectable -/// Implementation details of SQLSelectable. +/// Implementation details of `SQLSelectable`. /// /// :nodoc: public protocol _SQLSelectable { diff --git a/GRDB/Record/EncodableRecord.swift b/GRDB/Record/EncodableRecord.swift index 30b4454e3..8cec507a0 100644 --- a/GRDB/Record/EncodableRecord.swift +++ b/GRDB/Record/EncodableRecord.swift @@ -313,13 +313,13 @@ extension Row { // MARK: - DatabaseDateEncodingStrategy -/// DatabaseDateEncodingStrategy specifies how EncodableRecord types that also -/// adopt the standard Encodable protocol encode their date properties. +/// `DatabaseDateEncodingStrategy` specifies how `EncodableRecord` types that +/// also adopt the standard `Encodable` protocol encode their `Date` properties. /// /// For example: /// /// struct Player: EncodableRecord, Encodable { -/// static let databaseDateEncodingStrategy: DatabaseDateEncodingStrategy = .timeIntervalSince1970 +/// static let databaseDateEncodingStrategy = DatabaseDateEncodingStrategy.timeIntervalSince1970 /// /// var name: String /// var registrationDate: Date // encoded as an epoch timestamp @@ -360,13 +360,13 @@ public enum DatabaseDateEncodingStrategy { // MARK: - DatabaseUUIDEncodingStrategy -/// DatabaseUUIDEncodingStrategy specifies how EncodableRecord types that also -/// adopt the standard Encodable protocol encode their UUID properties. +/// `DatabaseUUIDEncodingStrategy` specifies how `EncodableRecord` types that +/// also adopt the standard `Encodable` protocol encode their `UUID` properties. /// /// For example: /// /// struct Player: EncodableProtocol, Encodable { -/// static let databaseUUIDEncodingStrategy: DatabaseUUIDEncodingStrategy = .string +/// static let databaseUUIDEncodingStrategy = DatabaseUUIDEncodingStrategy.string /// /// // encoded in a string like "E621E1F8-C36C-495A-93FC-0C247A3E6E5F" /// var uuid: UUID diff --git a/GRDB/Record/FetchableRecord.swift b/GRDB/Record/FetchableRecord.swift index 46151d6ff..20adc03ea 100644 --- a/GRDB/Record/FetchableRecord.swift +++ b/GRDB/Record/FetchableRecord.swift @@ -537,13 +537,14 @@ public final class RecordCursor: Cursor { // MARK: - DatabaseDateDecodingStrategy -/// DatabaseDateDecodingStrategy specifies how FetchableRecord types that also -/// adopt the standard Decodable protocol decode their date properties. +/// `DatabaseDateDecodingStrategy` specifies how `FetchableRecord` types that +/// also adopt the standard `Decodable` protocol decode their +/// `Date` properties. /// /// For example: /// /// struct Player: FetchableRecord, Decodable { -/// static let databaseDateDecodingStrategy: DatabaseDateDecodingStrategy = .timeIntervalSince1970 +/// static let databaseDateDecodingStrategy = DatabaseDateDecodingStrategy.timeIntervalSince1970 /// /// var name: String /// var registrationDate: Date // decoded from epoch timestamp diff --git a/GRDB/Record/PersistableRecord.swift b/GRDB/Record/PersistableRecord.swift index b3bb97aa7..6e4dac101 100644 --- a/GRDB/Record/PersistableRecord.swift +++ b/GRDB/Record/PersistableRecord.swift @@ -13,11 +13,11 @@ extension Database.ConflictResolution { /// An error thrown by a type that adopts PersistableRecord. public enum PersistenceError: Error, CustomStringConvertible { - /// Thrown by MutablePersistableRecord.update() when no matching row could be - /// found in the database. + /// Thrown by `MutablePersistableRecord.update(_:)` methods when no matching + /// row could be found in the database. /// - /// - databaseTableName: the table of the unfound record - /// - key: the key of the unfound record (column and values) + /// - `databaseTableName`: the table of the unfound record + /// - `key`: the key of the unfound record (column and values) case recordNotFound(databaseTableName: String, key: [String: DatabaseValue]) } diff --git a/GRDB/ValueObservation/ValueObservation.swift b/GRDB/ValueObservation/ValueObservation.swift index 4a6b1479d..29ccf8925 100644 --- a/GRDB/ValueObservation/ValueObservation.swift +++ b/GRDB/ValueObservation/ValueObservation.swift @@ -19,7 +19,7 @@ import Foundation /// onChange: { players: [Player] in /// print("Players have changed.") /// }) -public struct ValueObservation { +public struct ValueObservation { var events = ValueObservationEvents() /// The reducer is created when observation starts, and is triggered upon diff --git a/GRDB/ValueObservation/ValueObservationScheduler.swift b/GRDB/ValueObservation/ValueObservationScheduler.swift index b52f93d21..cc8036f46 100644 --- a/GRDB/ValueObservation/ValueObservationScheduler.swift +++ b/GRDB/ValueObservation/ValueObservationScheduler.swift @@ -1,7 +1,7 @@ import Dispatch import Foundation -/// ValueObservationScheduler determines how ValueObservation notifies its +/// ValueObservationScheduler determines how `ValueObservation` notifies its /// fresh values. public class ValueObservationScheduler { private let impl: ValueObservationSchedulerImpl diff --git a/GRDB/ValueObservation/ValueObserver.swift b/GRDB/ValueObservation/ValueObserver.swift index 5eb3320f0..3503aaab3 100644 --- a/GRDB/ValueObservation/ValueObserver.swift +++ b/GRDB/ValueObservation/ValueObserver.swift @@ -2,7 +2,7 @@ import Foundation /// Support for ValueObservation. /// See DatabaseWriter.add(observation:onError:onChange:) -final class ValueObserver { +final class ValueObserver { var isCompleted: Bool { synchronized { _isCompleted } } let events: ValueObservationEvents private var observedRegion: DatabaseRegion? { @@ -90,10 +90,10 @@ extension ValueObserver: TransactionObserver { events.databaseDidChange?() // 1. Fetch - let fetchedValue: DatabaseFuture + let fetchedFuture: DatabaseFuture if requiresWriteAccess || needsRecordingSelectedRegion { // Synchronously - fetchedValue = DatabaseFuture(Result { + fetchedFuture = DatabaseFuture(Result { try recordingSelectedRegionIfNeeded(db) { try reducer.fetch(db, requiringWriteAccess: requiresWriteAccess) } @@ -101,7 +101,7 @@ extension ValueObserver: TransactionObserver { } else { // Concurrently guard let writer = writer else { return } - fetchedValue = writer.concurrentRead(reducer.fetch) + fetchedFuture = writer.concurrentRead(reducer._fetch) } // 2. Reduce @@ -113,7 +113,8 @@ extension ValueObserver: TransactionObserver { reduceQueue.async { if self.isCompleted { return } do { - if let value = try self.reducer.value(fetchedValue.wait()) { + let fetchedValue = try fetchedFuture.wait() + if let value = self.reducer._value(fetchedValue) { self.notifyChange(value) } } catch { @@ -199,7 +200,7 @@ extension ValueObserver { } private var needsRecordingSelectedRegion: Bool { - observedRegion == nil || !reducer.isSelectedRegionDeterministic + observedRegion == nil || !reducer._isSelectedRegionDeterministic } private func recordingSelectedRegionIfNeeded( diff --git a/GRDB/ValueObservation/ValueReducer/Fetch.swift b/GRDB/ValueObservation/ValueReducer/Fetch.swift index a15ff071c..4fdcc04e7 100644 --- a/GRDB/ValueObservation/ValueReducer/Fetch.swift +++ b/GRDB/ValueObservation/ValueReducer/Fetch.swift @@ -1,10 +1,9 @@ extension ValueReducers { /// A reducer which passes raw fetched values through. - /// - /// :nodoc: - public struct Fetch: _ValueReducer { - private let _fetch: (Database) throws -> Value - public let isSelectedRegionDeterministic: Bool + public struct Fetch: ValueReducer { + private let __fetch: (Database) throws -> Value + /// :nodoc: + public let _isSelectedRegionDeterministic: Bool /// Creates a reducer, which passes raw fetched values through. /// @@ -19,16 +18,18 @@ extension ValueReducers { /// each fetch. /// /// - parameter fetch: A function that fetches the observed value. - public init(isSelectedRegionDeterministic: Bool, fetch: @escaping (Database) throws -> Value) { - self.isSelectedRegionDeterministic = isSelectedRegionDeterministic - self._fetch = fetch + init(isSelectedRegionDeterministic: Bool, fetch: @escaping (Database) throws -> Value) { + self._isSelectedRegionDeterministic = isSelectedRegionDeterministic + self.__fetch = fetch } - public func fetch(_ db: Database) throws -> Value { - try _fetch(db) + /// :nodoc: + public func _fetch(_ db: Database) throws -> Value { + try __fetch(db) } - public func value(_ fetched: Value) -> Value? { + /// :nodoc: + public func _value(_ fetched: Value) -> Value? { fetched } } diff --git a/GRDB/ValueObservation/ValueReducer/Map.swift b/GRDB/ValueObservation/ValueReducer/Map.swift index 86bc42421..89659b694 100644 --- a/GRDB/ValueObservation/ValueReducer/Map.swift +++ b/GRDB/ValueObservation/ValueReducer/Map.swift @@ -14,24 +14,25 @@ extension ValueReducers { /// passed through a transform function. /// /// See `ValueObservation.map(_:)` - /// - /// :nodoc: - public struct Map: _ValueReducer { + public struct Map: ValueReducer { private var base: Base private let transform: (Base.Value) -> Value - public var isSelectedRegionDeterministic: Bool { base.isSelectedRegionDeterministic } + /// :nodoc: + public var _isSelectedRegionDeterministic: Bool { base._isSelectedRegionDeterministic } init(_ base: Base, _ transform: @escaping (Base.Value) -> Value) { self.base = base self.transform = transform } - public func fetch(_ db: Database) throws -> Base.Fetched { - try base.fetch(db) + /// :nodoc: + public func _fetch(_ db: Database) throws -> Base.Fetched { + try base._fetch(db) } - public mutating func value(_ fetched: Base.Fetched) -> Value? { - guard let value = base.value(fetched) else { return nil } + /// :nodoc: + public mutating func _value(_ fetched: Base.Fetched) -> Value? { + guard let value = base._value(fetched) else { return nil } return transform(value) } } diff --git a/GRDB/ValueObservation/ValueReducer/RemoveDuplicates.swift b/GRDB/ValueObservation/ValueReducer/RemoveDuplicates.swift index 4dbea9367..0282de898 100644 --- a/GRDB/ValueObservation/ValueReducer/RemoveDuplicates.swift +++ b/GRDB/ValueObservation/ValueReducer/RemoveDuplicates.swift @@ -10,23 +10,24 @@ extension ValueObservation where Reducer.Value: Equatable { extension ValueReducers { /// See `ValueObservation.removeDuplicates()` - /// - /// :nodoc: - public struct RemoveDuplicates: _ValueReducer where Base.Value: Equatable { + public struct RemoveDuplicates: ValueReducer where Base.Value: Equatable { private var base: Base private var previousValue: Base.Value? - public var isSelectedRegionDeterministic: Bool { base.isSelectedRegionDeterministic } + /// :nodoc: + public var _isSelectedRegionDeterministic: Bool { base._isSelectedRegionDeterministic } init(_ base: Base) { self.base = base } - public func fetch(_ db: Database) throws -> Base.Fetched { - try base.fetch(db) + /// :nodoc: + public func _fetch(_ db: Database) throws -> Base.Fetched { + try base._fetch(db) } - public mutating func value(_ fetched: Base.Fetched) -> Base.Value? { - guard let value = base.value(fetched) else { + /// :nodoc: + public mutating func _value(_ fetched: Base.Fetched) -> Base.Value? { + guard let value = base._value(fetched) else { return nil } if let previousValue = previousValue, previousValue == value { diff --git a/GRDB/ValueObservation/ValueReducer/Trace.swift b/GRDB/ValueObservation/ValueReducer/Trace.swift index ed23b7900..8b2a2310b 100644 --- a/GRDB/ValueObservation/ValueReducer/Trace.swift +++ b/GRDB/ValueObservation/ValueReducer/Trace.swift @@ -1,18 +1,21 @@ extension ValueReducers { - /// :nodoc: - public struct Trace: _ValueReducer { + /// See `ValueObservation.handleEvents()` + public struct Trace: ValueReducer { var base: Base let willFetch: () -> Void let didReceiveValue: (Base.Value) -> Void - public var isSelectedRegionDeterministic: Bool { base.isSelectedRegionDeterministic } + /// :nodoc: + public var _isSelectedRegionDeterministic: Bool { base._isSelectedRegionDeterministic } - public func fetch(_ db: Database) throws -> Base.Fetched { + /// :nodoc: + public func _fetch(_ db: Database) throws -> Base.Fetched { willFetch() - return try base.fetch(db) + return try base._fetch(db) } - public mutating func value(_ fetched: Base.Fetched) -> Base.Value? { - guard let value = base.value(fetched) else { + /// :nodoc: + public mutating func _value(_ fetched: Base.Fetched) -> Base.Value? { + guard let value = base._value(fetched) else { return nil } didReceiveValue(value) diff --git a/GRDB/ValueObservation/ValueReducer/ValueReducer.swift b/GRDB/ValueObservation/ValueReducer/ValueReducer.swift index de5c09075..f770a1b6b 100644 --- a/GRDB/ValueObservation/ValueReducer/ValueReducer.swift +++ b/GRDB/ValueObservation/ValueReducer/ValueReducer.swift @@ -1,4 +1,4 @@ -/// The _ValueReducer protocol supports ValueObservation. +/// Implementation details of `ValueReducer`. public protocol _ValueReducer { /// The type of fetched database values associatedtype Fetched @@ -6,63 +6,66 @@ public protocol _ValueReducer { /// The type of observed values associatedtype Value - /// Returns whether the database region selected by the fetch(_:) method + /// Returns whether the database region selected by the `fetch(_:)` method /// is constant. - var isSelectedRegionDeterministic: Bool { get } + var _isSelectedRegionDeterministic: Bool { get } /// Fetches database values upon changes in an observed database region. /// - /// _ValueReducer semantics require that this method does not depend on + /// ValueReducer semantics require that this method does not depend on /// the state of the reducer. - func fetch(_ db: Database) throws -> Fetched + func _fetch(_ db: Database) throws -> Fetched /// Transforms a fetched value into an eventual observed value. Returns nil /// when observer should not be notified. /// /// This method runs in some unspecified dispatch queue. /// - /// _ValueReducer semantics require that the first invocation of this + /// ValueReducer semantics require that the first invocation of this /// method returns a non-nil value: /// /// let reducer = MyReducer() - /// reducer.value(...) // MUST NOT be nil - /// reducer.value(...) // MAY be nil - /// reducer.value(...) // MAY be nil - mutating func value(_ fetched: Fetched) -> Value? + /// reducer._value(...) // MUST NOT be nil + /// reducer._value(...) // MAY be nil + /// reducer._value(...) // MAY be nil + mutating func _value(_ fetched: Fetched) -> Value? } -extension _ValueReducer { +/// The `ValueReducer` protocol supports `ValueObservation`. +public protocol ValueReducer: _ValueReducer { } + +extension ValueReducer { func fetch(_ db: Database, requiringWriteAccess: Bool) throws -> Fetched { if requiringWriteAccess { var fetchedValue: Fetched? try db.inSavepoint { - fetchedValue = try fetch(db) + fetchedValue = try _fetch(db) return .commit } return fetchedValue! } else { return try db.readOnly { - try fetch(db) + try _fetch(db) } } } mutating func fetchAndReduce(_ db: Database, requiringWriteAccess: Bool) throws -> Value? { let fetchedValue = try fetch(db, requiringWriteAccess: requiringWriteAccess) - return value(fetchedValue) + return _value(fetchedValue) } } -/// A namespace for types related to the _ValueReducer protocol. +/// A namespace for types related to the `ValueReducer` protocol. public enum ValueReducers { // ValueReducers.Auto allows us to define ValueObservation factory methods. // // For example, ValueObservation.tracking(_:) is, practically, // ValueObservation.tracking(_:). /// :nodoc: - public enum Auto: _ValueReducer { - public var isSelectedRegionDeterministic: Bool { preconditionFailure() } - public func fetch(_ db: Database) throws -> Never { preconditionFailure() } - public mutating func value(_ fetched: Never) -> Never? { } + public enum Auto: ValueReducer { + public var _isSelectedRegionDeterministic: Bool { preconditionFailure() } + public func _fetch(_ db: Database) throws -> Never { preconditionFailure() } + public mutating func _value(_ fetched: Never) -> Never? { } } } diff --git a/Tests/GRDBTests/GRDBTestCase.swift b/Tests/GRDBTests/GRDBTestCase.swift index c597c3bc1..989534118 100644 --- a/Tests/GRDBTests/GRDBTestCase.swift +++ b/Tests/GRDBTests/GRDBTestCase.swift @@ -215,7 +215,7 @@ class GRDBTestCase: XCTestCase { try _assertEqualSQL(databaseReader, request, sql, file: file, line: line) } #endif - + func sql(_ databaseReader: DatabaseReader, _ request: Request) -> String { try! databaseReader.unsafeRead { db in try request.makeStatement(db).makeCursor().next() @@ -238,25 +238,25 @@ extension FetchRequest { } /// A type-erased ValueReducer. -public struct AnyValueReducer: _ValueReducer { - private var _fetch: (Database) throws -> Fetched - private var _value: (Fetched) -> Value? +public struct AnyValueReducer: ValueReducer { + private var __fetch: (Database) throws -> Fetched + private var __value: (Fetched) -> Value? - public var isSelectedRegionDeterministic: Bool { false } + public var _isSelectedRegionDeterministic: Bool { false } public init( fetch: @escaping (Database) throws -> Fetched, value: @escaping (Fetched) -> Value?) { - self._fetch = fetch - self._value = value + self.__fetch = fetch + self.__value = value } - public func fetch(_ db: Database) throws -> Fetched { - try _fetch(db) + public func _fetch(_ db: Database) throws -> Fetched { + try __fetch(db) } - public func value(_ fetched: Fetched) -> Value? { - _value(fetched) + public func _value(_ fetched: Fetched) -> Value? { + __value(fetched) } } diff --git a/Tests/GRDBTests/ValueObservationPrintTests.swift b/Tests/GRDBTests/ValueObservationPrintTests.swift index 08ebbf60c..71020f969 100644 --- a/Tests/GRDBTests/ValueObservationPrintTests.swift +++ b/Tests/GRDBTests/ValueObservationPrintTests.swift @@ -601,7 +601,7 @@ class ValueObservationPrintTests: GRDBTestCase { } func test_handleEvents() throws { - func waitFor(_ observation: ValueObservation) throws { + func waitFor(_ observation: ValueObservation) throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in try db.execute(sql: "CREATE TABLE player(id INTEGER PRIMARY KEY)") @@ -624,7 +624,7 @@ class ValueObservationPrintTests: GRDBTestCase { } } - func waitForError(_ observation: ValueObservation) throws { + func waitForError(_ observation: ValueObservation) throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in try db.execute(sql: "CREATE TABLE player(id INTEGER PRIMARY KEY)") @@ -648,7 +648,7 @@ class ValueObservationPrintTests: GRDBTestCase { } } - func waitForCancel(_ observation: ValueObservation) throws { + func waitForCancel(_ observation: ValueObservation) throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in try db.execute(sql: "CREATE TABLE player(id INTEGER PRIMARY KEY)") diff --git a/Tests/GRDBTests/ValueObservationRecorder.swift b/Tests/GRDBTests/ValueObservationRecorder.swift index 02b06d650..2363cf256 100644 --- a/Tests/GRDBTests/ValueObservationRecorder.swift +++ b/Tests/GRDBTests/ValueObservationRecorder.swift @@ -412,7 +412,7 @@ extension XCTestCase { // MARK: - GRDBTestCase + ValueObservationExpectation extension GRDBTestCase { - func _assertValueObservation( + func _assertValueObservation( _ observation: ValueObservation, records expectedValues: [Reducer.Value], setup: (Database) throws -> Void, @@ -631,7 +631,7 @@ extension GRDBTestCase { } } - func _assertValueObservation( + func _assertValueObservation( _ observation: ValueObservation, fails testFailure: (Failure, DatabaseWriter) throws -> Void, setup: (Database) throws -> Void, @@ -735,7 +735,7 @@ extension GRDBTestCase { try _assertValueObservation(observation, fails: testFailure, setup: setup, file: file, line: line) } #else - func assertValueObservation( + func assertValueObservation( _ observation: ValueObservation, records expectedValues: [Reducer.Value], setup: (Database) throws -> Void, @@ -753,7 +753,7 @@ extension GRDBTestCase { file: file, line: line) } - func assertValueObservation( + func assertValueObservation( _ observation: ValueObservation, fails testFailure: (Failure, DatabaseWriter) throws -> Void, setup: (Database) throws -> Void,