Files
GRDB.swift/GRDB/ValueObservation/ValueReducer/RemoveDuplicates.swift
T
Gwendal Roué 927fc21f00 NEW: ValueObservation.map accepts a throwing closure
This will allow us to call a throwing FetchableRecord.init(row:) from this operator
2022-08-21 21:22:34 +02:00

51 lines
1.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
extension ValueObservation {
/// Returns a ValueObservation which only publishes elements that don’t
/// match the previous element, as evaluated by a provided closure.
public func removeDuplicates(by predicate: @escaping (Reducer.Value, Reducer.Value) -> Bool)
-> ValueObservation<ValueReducers.RemoveDuplicates<Reducer>>
{
mapReducer { ValueReducers.RemoveDuplicates($0, predicate: predicate) }
}
}
extension ValueObservation where Reducer.Value: Equatable {
/// Returns a ValueObservation which filters out consecutive equal values.
public func removeDuplicates()
-> ValueObservation<ValueReducers.RemoveDuplicates<Reducer>>
{
mapReducer { ValueReducers.RemoveDuplicates($0, predicate: ==) }
}
}
extension ValueReducers {
/// See `ValueObservation.removeDuplicates()`
public struct RemoveDuplicates<Base: ValueReducer>: ValueReducer {
private var base: Base
private var previousValue: Base.Value?
private var predicate: (Base.Value, Base.Value) -> Bool
init(_ base: Base, predicate: @escaping (Base.Value, Base.Value) -> Bool) {
self.base = base
self.predicate = predicate
}
/// :nodoc:
public func _fetch(_ db: Database) throws -> Base.Fetched {
try base._fetch(db)
}
/// :nodoc:
public mutating func _value(_ fetched: Base.Fetched) throws -> Base.Value? {
guard let value = try base._value(fetched) else {
return nil
}
if let previousValue = previousValue, predicate(previousValue, value) {
// Don't notify consecutive identical values
return nil
}
self.previousValue = value
return value
}
}
}