mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
More ValueReducers namespacing
This commit is contained in:
1 parent
cea22f261e
commit
86f7ab74a7
8 files changed
+263
-231
No files matched your search
@@ -17,7 +17,7 @@ extension ValueObservation {
|
||||
public static func tracking(_ regions: DatabaseRegionConvertible..., reducer: Reducer) -> ValueObservation { preconditionFailure() }
|
||||
|
||||
@available(*, unavailable, message: "Use distinctUntilChanged() instead")
|
||||
public static func tracking<Value>(_ regions: DatabaseRegionConvertible..., fetchDistinct fetch: @escaping (Database) throws -> Value) -> ValueObservation<ValueReducers.RemoveDuplicates<ValueReducers.Passthrough<Value>>> where Value: Equatable { preconditionFailure() }
|
||||
public static func tracking<Value>(_ regions: DatabaseRegionConvertible..., fetchDistinct fetch: @escaping (Database) throws -> Value) -> ValueObservation<ValueReducers.RemoveDuplicates<ValueReducers.Fetch<Value>>> where Value: Equatable { preconditionFailure() }
|
||||
}
|
||||
|
||||
@available(*, unavailable, renamed: "FastDatabaseValueCursor")
|
||||
|
||||
@@ -25,7 +25,7 @@ extension FetchRequest {
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForCount() ->
|
||||
ValueObservation<ValueReducers.RemoveDuplicates<ValueReducers.Passthrough<Int>>>
|
||||
ValueObservation<ValueReducers.RemoveDuplicates<ValueReducers.Fetch<Int>>>
|
||||
{
|
||||
return ValueObservation
|
||||
.tracking(self, fetch: fetchCount)
|
||||
@@ -59,7 +59,7 @@ extension TableRecord {
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public static func observationForCount() ->
|
||||
ValueObservation<ValueReducers.RemoveDuplicates<ValueReducers.Passthrough<Int>>>
|
||||
ValueObservation<ValueReducers.RemoveDuplicates<ValueReducers.Fetch<Int>>>
|
||||
{
|
||||
return all().observationForCount()
|
||||
}
|
||||
@@ -94,7 +94,7 @@ extension ValueObservation where Reducer == Void {
|
||||
/// - returns: a ValueObservation.
|
||||
@available(*, deprecated, message: "Use request.observationForCount() instead")
|
||||
public static func trackingCount<Request: FetchRequest>(_ request: Request)
|
||||
-> ValueObservation<ValueReducers.RemoveDuplicates<ValueReducers.Passthrough<Int>>>
|
||||
-> ValueObservation<ValueReducers.RemoveDuplicates<ValueReducers.Fetch<Int>>>
|
||||
{
|
||||
return request.observationForCount()
|
||||
}
|
||||
|
||||
@@ -25,9 +25,9 @@ extension FetchRequest where RowDecoder: DatabaseValueConvertible {
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForAll() -> ValueObservation<DatabaseValuesReducer<RowDecoder>> {
|
||||
public func observationForAll() -> ValueObservation<ValueReducers.AllValues<RowDecoder>> {
|
||||
return ValueObservation.tracking(self, reducer: { _ in
|
||||
DatabaseValuesReducer { try DatabaseValue.fetchAll($0, self) }
|
||||
ValueReducers.AllValues { try DatabaseValue.fetchAll($0, self) }
|
||||
})
|
||||
}
|
||||
|
||||
@@ -54,9 +54,9 @@ extension FetchRequest where RowDecoder: DatabaseValueConvertible {
|
||||
///
|
||||
/// - parameter request: the observed request.
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForFirst() -> ValueObservation<DatabaseValueReducer<RowDecoder>> {
|
||||
public func observationForFirst() -> ValueObservation<ValueReducers.OneValue<RowDecoder>> {
|
||||
return ValueObservation.tracking(self, reducer: { _ in
|
||||
DatabaseValueReducer { try DatabaseValue.fetchOne($0, self) }
|
||||
ValueReducers.OneValue { try DatabaseValue.fetchOne($0, self) }
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -90,9 +90,9 @@ extension FetchRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped:
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForAll() -> ValueObservation<OptionalDatabaseValuesReducer<RowDecoder._Wrapped>> {
|
||||
public func observationForAll() -> ValueObservation<ValueReducers.AllOptionalValues<RowDecoder._Wrapped>> {
|
||||
return ValueObservation.tracking(self, reducer: { _ in
|
||||
OptionalDatabaseValuesReducer { try DatabaseValue.fetchAll($0, self) }
|
||||
ValueReducers.AllOptionalValues { try DatabaseValue.fetchAll($0, self) }
|
||||
})
|
||||
}
|
||||
|
||||
@@ -119,9 +119,9 @@ extension FetchRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped:
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForFirst() -> ValueObservation<DatabaseValueReducer<RowDecoder._Wrapped>> {
|
||||
public func observationForFirst() -> ValueObservation<ValueReducers.OneValue<RowDecoder._Wrapped>> {
|
||||
return ValueObservation.tracking(self, reducer: { _ in
|
||||
DatabaseValueReducer { try DatabaseValue.fetchOne($0, self) }
|
||||
ValueReducers.OneValue { try DatabaseValue.fetchOne($0, self) }
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -227,108 +227,122 @@ extension ValueObservation where Reducer == Void {
|
||||
}
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs arrays of values, filtering out consecutive
|
||||
/// identical database values.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct DatabaseValuesReducer<RowDecoder>: ValueReducer
|
||||
where RowDecoder: DatabaseValueConvertible
|
||||
{
|
||||
private let _fetch: (Database) throws -> [DatabaseValue]
|
||||
private var previousDbValues: [DatabaseValue]?
|
||||
|
||||
init(fetch: @escaping (Database) throws -> [DatabaseValue]) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
|
||||
public func fetch(_ db: Database) throws -> [DatabaseValue] {
|
||||
return try _fetch(db)
|
||||
}
|
||||
|
||||
public mutating func value(_ dbValues: [DatabaseValue]) -> [RowDecoder]? {
|
||||
if let previousDbValues = previousDbValues, previousDbValues == dbValues {
|
||||
// Don't notify consecutive identical dbValue arrays
|
||||
return nil
|
||||
extension ValueReducers {
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs arrays of values, filtering out consecutive
|
||||
/// identical database values.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct AllValues<RowDecoder>: ValueReducer
|
||||
where RowDecoder: DatabaseValueConvertible
|
||||
{
|
||||
private let _fetch: (Database) throws -> [DatabaseValue]
|
||||
private var previousDbValues: [DatabaseValue]?
|
||||
|
||||
init(fetch: @escaping (Database) throws -> [DatabaseValue]) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
self.previousDbValues = dbValues
|
||||
return dbValues.map {
|
||||
RowDecoder.decode(from: $0, conversionContext: nil)
|
||||
|
||||
public func fetch(_ db: Database) throws -> [DatabaseValue] {
|
||||
return try _fetch(db)
|
||||
}
|
||||
|
||||
public mutating func value(_ dbValues: [DatabaseValue]) -> [RowDecoder]? {
|
||||
if let previousDbValues = previousDbValues, previousDbValues == dbValues {
|
||||
// Don't notify consecutive identical dbValue arrays
|
||||
return nil
|
||||
}
|
||||
self.previousDbValues = dbValues
|
||||
return dbValues.map {
|
||||
RowDecoder.decode(from: $0, conversionContext: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs optional values, filtering out consecutive
|
||||
/// identical database values.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct OneValue<RowDecoder>: ValueReducer
|
||||
where RowDecoder: DatabaseValueConvertible
|
||||
{
|
||||
private let _fetch: (Database) throws -> DatabaseValue?
|
||||
private var previousDbValue: DatabaseValue??
|
||||
private var previousValueWasNil = false
|
||||
|
||||
init(fetch: @escaping (Database) throws -> DatabaseValue?) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
|
||||
public func fetch(_ db: Database) throws -> DatabaseValue? {
|
||||
return try _fetch(db)
|
||||
}
|
||||
|
||||
public mutating func value(_ dbValue: DatabaseValue?) -> RowDecoder?? {
|
||||
if let previousDbValue = previousDbValue, previousDbValue == dbValue {
|
||||
// Don't notify consecutive identical dbValue
|
||||
return nil
|
||||
}
|
||||
self.previousDbValue = dbValue
|
||||
if let dbValue = dbValue,
|
||||
let value = RowDecoder.decodeIfPresent(from: dbValue, conversionContext: nil)
|
||||
{
|
||||
previousValueWasNil = false
|
||||
return .some(value)
|
||||
} else if previousValueWasNil {
|
||||
// Don't notify consecutive nil values
|
||||
return nil
|
||||
} else {
|
||||
previousValueWasNil = true
|
||||
return .some(nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs arrays of optional values, filtering out consecutive
|
||||
/// identical database values.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct AllOptionalValues<RowDecoder>: ValueReducer
|
||||
where RowDecoder: DatabaseValueConvertible
|
||||
{
|
||||
private let _fetch: (Database) throws -> [DatabaseValue]
|
||||
private var previousDbValues: [DatabaseValue]?
|
||||
|
||||
init(fetch: @escaping (Database) throws -> [DatabaseValue]) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
|
||||
public func fetch(_ db: Database) throws -> [DatabaseValue] {
|
||||
return try _fetch(db)
|
||||
}
|
||||
|
||||
public mutating func value(_ dbValues: [DatabaseValue]) -> [RowDecoder?]? {
|
||||
if let previousDbValues = previousDbValues, previousDbValues == dbValues {
|
||||
// Don't notify consecutive identical dbValue arrays
|
||||
return nil
|
||||
}
|
||||
self.previousDbValues = dbValues
|
||||
return dbValues.map {
|
||||
RowDecoder.decodeIfPresent(from: $0, conversionContext: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs optional values, filtering out consecutive
|
||||
/// identical database values.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct DatabaseValueReducer<RowDecoder>: ValueReducer
|
||||
where RowDecoder: DatabaseValueConvertible
|
||||
{
|
||||
private let _fetch: (Database) throws -> DatabaseValue?
|
||||
private var previousDbValue: DatabaseValue??
|
||||
private var previousValueWasNil = false
|
||||
|
||||
init(fetch: @escaping (Database) throws -> DatabaseValue?) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
|
||||
public func fetch(_ db: Database) throws -> DatabaseValue? {
|
||||
return try _fetch(db)
|
||||
}
|
||||
@available(*, deprecated, renamed: "ValueReducers.AllValues")
|
||||
public typealias DatabaseValuesReducer<RowDecoder> = ValueReducers.AllValues<RowDecoder> where RowDecoder: DatabaseValueConvertible
|
||||
|
||||
public mutating func value(_ dbValue: DatabaseValue?) -> RowDecoder?? {
|
||||
if let previousDbValue = previousDbValue, previousDbValue == dbValue {
|
||||
// Don't notify consecutive identical dbValue
|
||||
return nil
|
||||
}
|
||||
self.previousDbValue = dbValue
|
||||
if let dbValue = dbValue,
|
||||
let value = RowDecoder.decodeIfPresent(from: dbValue, conversionContext: nil)
|
||||
{
|
||||
previousValueWasNil = false
|
||||
return .some(value)
|
||||
} else if previousValueWasNil {
|
||||
// Don't notify consecutive nil values
|
||||
return nil
|
||||
} else {
|
||||
previousValueWasNil = true
|
||||
return .some(nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs arrays of optional values, filtering out consecutive
|
||||
/// identical database values.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct OptionalDatabaseValuesReducer<RowDecoder>: ValueReducer
|
||||
where RowDecoder: DatabaseValueConvertible
|
||||
{
|
||||
private let _fetch: (Database) throws -> [DatabaseValue]
|
||||
private var previousDbValues: [DatabaseValue]?
|
||||
|
||||
init(fetch: @escaping (Database) throws -> [DatabaseValue]) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
|
||||
public func fetch(_ db: Database) throws -> [DatabaseValue] {
|
||||
return try _fetch(db)
|
||||
}
|
||||
@available(*, deprecated, renamed: "ValueReducers.OneValue")
|
||||
public typealias DatabaseValueReducer<RowDecoder> = ValueReducers.OneValue<RowDecoder> where RowDecoder: DatabaseValueConvertible
|
||||
|
||||
public mutating func value(_ dbValues: [DatabaseValue]) -> [RowDecoder?]? {
|
||||
if let previousDbValues = previousDbValues, previousDbValues == dbValues {
|
||||
// Don't notify consecutive identical dbValue arrays
|
||||
return nil
|
||||
}
|
||||
self.previousDbValues = dbValues
|
||||
return dbValues.map {
|
||||
RowDecoder.decodeIfPresent(from: $0, conversionContext: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
/// :nodoc:
|
||||
@available(*, deprecated, renamed: "ValueReducers.AllOptionalValues")
|
||||
public typealias OptionalDatabaseValuesReducer<RowDecoder> = ValueReducers.AllOptionalValues<RowDecoder> where RowDecoder: DatabaseValueConvertible
|
||||
@@ -25,9 +25,9 @@ extension FetchRequest where RowDecoder: FetchableRecord {
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForAll() -> ValueObservation<FetchableRecordsReducer<RowDecoder>> {
|
||||
public func observationForAll() -> ValueObservation<ValueReducers.AllRecords<RowDecoder>> {
|
||||
return ValueObservation.tracking(self, reducer: { _ in
|
||||
FetchableRecordsReducer { try Row.fetchAll($0, self) }
|
||||
ValueReducers.AllRecords { try Row.fetchAll($0, self) }
|
||||
})
|
||||
}
|
||||
|
||||
@@ -53,9 +53,9 @@ extension FetchRequest where RowDecoder: FetchableRecord {
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForFirst() -> ValueObservation<FetchableRecordReducer<RowDecoder>> {
|
||||
public func observationForFirst() -> ValueObservation<ValueReducers.OneRecord<RowDecoder>> {
|
||||
return ValueObservation.tracking(self, reducer: { _ in
|
||||
FetchableRecordReducer { try Row.fetchOne($0, self) }
|
||||
ValueReducers.OneRecord { try Row.fetchOne($0, self) }
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -86,7 +86,7 @@ extension TableRecord where Self: FetchableRecord {
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public static func observationForAll() -> ValueObservation<FetchableRecordsReducer<Self>> {
|
||||
public static func observationForAll() -> ValueObservation<ValueReducers.AllRecords<Self>> {
|
||||
return all().observationForAll()
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ extension TableRecord where Self: FetchableRecord {
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public static func observationForFirst() -> ValueObservation<FetchableRecordReducer<Self>> {
|
||||
public static func observationForFirst() -> ValueObservation<ValueReducers.OneRecord<Self>> {
|
||||
// TODO: check that limit(1) has no impact on requests like filter(key:)
|
||||
return limit(1).observationForFirst()
|
||||
}
|
||||
@@ -186,63 +186,72 @@ extension ValueObservation where Reducer == Void {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: namespace
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs arrays of records, filtering out consecutive
|
||||
/// identical database rows.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct FetchableRecordsReducer<RowDecoder>: ValueReducer
|
||||
where RowDecoder: FetchableRecord
|
||||
{
|
||||
private let _fetch: (Database) throws -> [Row]
|
||||
private var previousRows: [Row]?
|
||||
|
||||
init(fetch: @escaping (Database) throws -> [Row]) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
|
||||
public func fetch(_ db: Database) throws -> [Row] {
|
||||
return try _fetch(db)
|
||||
}
|
||||
|
||||
public mutating func value(_ rows: [Row]) -> [RowDecoder]? {
|
||||
if let previousRows = previousRows, previousRows == rows {
|
||||
// Don't notify consecutive identical row arrays
|
||||
return nil
|
||||
extension ValueReducers {
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs arrays of records, filtering out consecutive
|
||||
/// identical database rows.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct AllRecords<RowDecoder>: ValueReducer
|
||||
where RowDecoder: FetchableRecord
|
||||
{
|
||||
private let _fetch: (Database) throws -> [Row]
|
||||
private var previousRows: [Row]?
|
||||
|
||||
init(fetch: @escaping (Database) throws -> [Row]) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
|
||||
public func fetch(_ db: Database) throws -> [Row] {
|
||||
return try _fetch(db)
|
||||
}
|
||||
|
||||
public mutating func value(_ rows: [Row]) -> [RowDecoder]? {
|
||||
if let previousRows = previousRows, previousRows == rows {
|
||||
// Don't notify consecutive identical row arrays
|
||||
return nil
|
||||
}
|
||||
self.previousRows = rows
|
||||
return rows.map(RowDecoder.init(row:))
|
||||
}
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs optional records, filtering out consecutive
|
||||
/// identical database rows.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct OneRecord<RowDecoder>: ValueReducer
|
||||
where RowDecoder: FetchableRecord
|
||||
{
|
||||
private let _fetch: (Database) throws -> Row?
|
||||
private var previousRow: Row??
|
||||
|
||||
init(fetch: @escaping (Database) throws -> Row?) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
|
||||
public func fetch(_ db: Database) throws -> Row? {
|
||||
return try _fetch(db)
|
||||
}
|
||||
|
||||
public mutating func value(_ row: Row?) -> RowDecoder?? {
|
||||
if let previousRow = previousRow, previousRow == row {
|
||||
// Don't notify consecutive identical rows
|
||||
return nil
|
||||
}
|
||||
self.previousRow = row
|
||||
return .some(row.map(RowDecoder.init(row:)))
|
||||
}
|
||||
self.previousRows = rows
|
||||
return rows.map(RowDecoder.init(row:))
|
||||
}
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs optional records, filtering out consecutive
|
||||
/// identical database rows.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct FetchableRecordReducer<RowDecoder>: ValueReducer
|
||||
where RowDecoder: FetchableRecord
|
||||
{
|
||||
private let _fetch: (Database) throws -> Row?
|
||||
private var previousRow: Row??
|
||||
|
||||
init(fetch: @escaping (Database) throws -> Row?) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
@available(*, deprecated, renamed: "ValueReducers.AllRecords")
|
||||
public typealias FetchableRecordsReducer<RowDecoder> = ValueReducers.AllRecords<RowDecoder> where RowDecoder: FetchableRecord
|
||||
|
||||
public func fetch(_ db: Database) throws -> Row? {
|
||||
return try _fetch(db)
|
||||
}
|
||||
|
||||
public mutating func value(_ row: Row?) -> RowDecoder?? {
|
||||
if let previousRow = previousRow, previousRow == row {
|
||||
// Don't notify consecutive identical rows
|
||||
return nil
|
||||
}
|
||||
self.previousRow = row
|
||||
return .some(row.map(RowDecoder.init(row:)))
|
||||
}
|
||||
}
|
||||
/// :nodoc:
|
||||
@available(*, deprecated, renamed: "ValueReducers.OneRecord")
|
||||
public typealias FetchableRecordReducer<RowDecoder> = ValueReducers.OneRecord<RowDecoder> where RowDecoder: FetchableRecord
|
||||
@@ -24,9 +24,9 @@ extension FetchRequest where RowDecoder == Row {
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForAll() -> ValueObservation<RowsReducer> {
|
||||
public func observationForAll() -> ValueObservation<ValueReducers.AllRows> {
|
||||
return ValueObservation.tracking(self, reducer: { _ in
|
||||
RowsReducer(fetch: self.fetchAll)
|
||||
ValueReducers.AllRows(fetch: self.fetchAll)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -52,9 +52,9 @@ extension FetchRequest where RowDecoder == Row {
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForFirst() -> ValueObservation<RowReducer> {
|
||||
public func observationForFirst() -> ValueObservation<ValueReducers.OneRow> {
|
||||
return ValueObservation.tracking(self, reducer: { _ in
|
||||
RowReducer(fetch: self.fetchOne)
|
||||
ValueReducers.OneRow(fetch: self.fetchOne)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -126,59 +126,68 @@ extension ValueObservation where Reducer == Void {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: namespace
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs arrays of database rows, filtering out
|
||||
/// consecutive identical arrays.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct RowsReducer: ValueReducer {
|
||||
private let _fetch: (Database) throws -> [Row]
|
||||
private var previousRows: [Row]?
|
||||
|
||||
init(fetch: @escaping (Database) throws -> [Row]) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
|
||||
public func fetch(_ db: Database) throws -> [Row] {
|
||||
return try _fetch(db)
|
||||
}
|
||||
|
||||
public mutating func value(_ rows: [Row]) -> [Row]? {
|
||||
if let previousRows = previousRows, previousRows == rows {
|
||||
// Don't notify consecutive identical row arrays
|
||||
return nil
|
||||
extension ValueReducers {
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs arrays of database rows, filtering out
|
||||
/// consecutive identical arrays.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct AllRows: ValueReducer {
|
||||
private let _fetch: (Database) throws -> [Row]
|
||||
private var previousRows: [Row]?
|
||||
|
||||
init(fetch: @escaping (Database) throws -> [Row]) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
|
||||
public func fetch(_ db: Database) throws -> [Row] {
|
||||
return try _fetch(db)
|
||||
}
|
||||
|
||||
public mutating func value(_ rows: [Row]) -> [Row]? {
|
||||
if let previousRows = previousRows, previousRows == rows {
|
||||
// Don't notify consecutive identical row arrays
|
||||
return nil
|
||||
}
|
||||
self.previousRows = rows
|
||||
return rows
|
||||
}
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs optional records, filtering out consecutive
|
||||
/// identical database rows.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct OneRow: ValueReducer {
|
||||
private let _fetch: (Database) throws -> Row?
|
||||
private var previousRow: Row??
|
||||
|
||||
init(fetch: @escaping (Database) throws -> Row?) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
|
||||
public func fetch(_ db: Database) throws -> Row? {
|
||||
return try _fetch(db)
|
||||
}
|
||||
|
||||
public mutating func value(_ row: Row?) -> Row?? {
|
||||
if let previousRow = previousRow, previousRow == row {
|
||||
// Don't notify consecutive identical rows
|
||||
return nil
|
||||
}
|
||||
self.previousRow = row
|
||||
return row
|
||||
}
|
||||
self.previousRows = rows
|
||||
return rows
|
||||
}
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A reducer which outputs optional records, filtering out consecutive
|
||||
/// identical database rows.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct RowReducer: ValueReducer {
|
||||
private let _fetch: (Database) throws -> Row?
|
||||
private var previousRow: Row??
|
||||
|
||||
init(fetch: @escaping (Database) throws -> Row?) {
|
||||
self._fetch = fetch
|
||||
}
|
||||
|
||||
public func fetch(_ db: Database) throws -> Row? {
|
||||
return try _fetch(db)
|
||||
}
|
||||
|
||||
public mutating func value(_ row: Row?) -> Row?? {
|
||||
if let previousRow = previousRow, previousRow == row {
|
||||
// Don't notify consecutive identical rows
|
||||
return nil
|
||||
}
|
||||
self.previousRow = row
|
||||
return row
|
||||
}
|
||||
}
|
||||
@available(*, deprecated, renamed: "ValueReducers.AllRows")
|
||||
public typealias RowsReducer = ValueReducers.AllRows
|
||||
|
||||
/// :nodoc:
|
||||
@available(*, deprecated, renamed: "ValueReducers.OneRow")
|
||||
public typealias RowReducer = ValueReducers.OneRow
|
||||
@@ -344,7 +344,7 @@ extension ValueObservation where Reducer == Void {
|
||||
public static func tracking<Value>(
|
||||
_ regions: DatabaseRegionConvertible...,
|
||||
fetch: @escaping (Database) throws -> Value)
|
||||
-> ValueObservation<ValueReducers.Passthrough<Value>>
|
||||
-> ValueObservation<ValueReducers.Fetch<Value>>
|
||||
{
|
||||
return ValueObservation.tracking(regions, fetch: fetch)
|
||||
}
|
||||
@@ -377,10 +377,10 @@ extension ValueObservation where Reducer == Void {
|
||||
public static func tracking<Value>(
|
||||
_ regions: [DatabaseRegionConvertible],
|
||||
fetch: @escaping (Database) throws -> Value)
|
||||
-> ValueObservation<ValueReducers.Passthrough<Value>>
|
||||
-> ValueObservation<ValueReducers.Fetch<Value>>
|
||||
{
|
||||
return ValueObservation<ValueReducers.Passthrough<Value>>(
|
||||
return ValueObservation<ValueReducers.Fetch<Value>>(
|
||||
tracking: DatabaseRegion.union(regions),
|
||||
reducer: { _ in ValueReducers.Passthrough(fetch) })
|
||||
reducer: { _ in ValueReducers.Fetch(fetch) })
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ extension ValueReducers {
|
||||
/// A reducer which pass raw fetched values through.
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct Passthrough<Value>: ValueReducer {
|
||||
public struct Fetch<Value>: ValueReducer {
|
||||
private let _fetch: (Database) throws -> Value
|
||||
|
||||
public init(_ fetch: @escaping (Database) throws -> Value) {
|
||||
@@ -22,5 +22,5 @@ extension ValueReducers {
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
@available(*, deprecated, renamed: "ValueReducers.Passthrough")
|
||||
public typealias RawValueReducer<Value> = ValueReducers.Passthrough<Value>
|
||||
@available(*, deprecated, renamed: "ValueReducers.Fetch")
|
||||
public typealias RawValueReducer<Value> = ValueReducers.Fetch<Value>
|
||||
@@ -333,7 +333,7 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase {
|
||||
results.append(names)
|
||||
notificationExpectation.fulfill()
|
||||
}
|
||||
let valueObserver = observer as! ValueObserver<DatabaseValuesReducer<Name>>
|
||||
let valueObserver = observer as! ValueObserver<ValueReducers.AllValues<Name>>
|
||||
XCTAssertEqual(valueObserver.region.description, "t(id,name)") // view is not tracked
|
||||
try withExtendedLifetime(observer) {
|
||||
// Test view observation
|
||||
|
||||
Reference in new issue
Block a user