diff --git a/GRDB/Core/DatabaseRegion.swift b/GRDB/Core/DatabaseRegion.swift index f15f01ce9..613796e9d 100644 --- a/GRDB/Core/DatabaseRegion.swift +++ b/GRDB/Core/DatabaseRegion.swift @@ -312,3 +312,21 @@ extension DatabaseRegion: DatabaseRegionConvertible { return self } } + +/// A type-erased DatabaseRegionConvertible +public struct AnyDatabaseRegionConvertible: DatabaseRegionConvertible { + let _region: (Database) throws -> DatabaseRegion + + public init(_ region: @escaping (Database) throws -> DatabaseRegion) { + _region = region + } + + public init(_ region: DatabaseRegionConvertible) { + _region = { try region.databaseRegion($0) } + } + + /// :nodoc: + public func databaseRegion(_ db: Database) throws -> DatabaseRegion { + return try _region(db) + } +} diff --git a/GRDB/Core/ValueObservation.swift b/GRDB/Core/ValueObservation.swift index ac5bd52d9..bce63c47a 100644 --- a/GRDB/Core/ValueObservation.swift +++ b/GRDB/Core/ValueObservation.swift @@ -185,15 +185,20 @@ public struct ValueObservation { self.reducer = reducer } - /// Creates a ValueObservation which observes *region*, and notifies the - /// values returned by the *fetch* closure whenever the observed region is + /// Returs a ValueObservation which observes *regions*, and notifies the + /// values returned by the *reducer* whenever the observed region is /// impacted by a database transaction. /// + /// This method is the most fundamental way to create a ValueObservation. + /// /// For example: /// + /// let reducer = AnyValueReducer( + /// fetch: { db in try Player.fetchAll(db) }, + /// value: { player in players }) /// let observation = ValueObservation( - /// observing: { db in try Player.all().databaseRegion(db) }, - /// fetch: { db in try Player.fetchAll(db) }) + /// observing: Player.all(), + /// reducer: reducer) /// /// let observer = try dbQueue.add(observation: observation) { player: [Player] in /// print("players have changed") @@ -213,10 +218,16 @@ public struct ValueObservation { /// - parameter region: a closure that returns the observed region. /// - parameter fetch: a closure that fetches a value. public static func observing( - _ region: @escaping (Database) throws -> DatabaseRegion, + _ regions: DatabaseRegionConvertible..., reducer: Reducer) -> ValueObservation { + func region(_ db: Database) throws -> DatabaseRegion { + return try regions.reduce(into: DatabaseRegion()) { union, region in + try union.formUnion(region.databaseRegion(db)) + } + } + return ValueObservation(observing: region, reducer: reducer) } } @@ -391,49 +402,6 @@ public enum ValueReducers { // MARK: - DatabaseRegionConvertible Observation -extension ValueObservation { - /// Creates a ValueObservation which observes *region*, and notifies the - /// values returned by the *fetch* closure whenever the observed region is - /// impacted by a database transaction. - /// - /// For example: - /// - /// let observation = ValueObservation( - /// observing: { db in try Player.all().databaseRegion(db) }, - /// fetch: { db in try Player.fetchAll(db) }) - /// - /// let observer = try dbQueue.add(observation: observation) { player: [Player] in - /// print("players have changed") - /// } - /// - /// The returned observation has the default configuration: - /// - /// - When started with the `add(observation:)` method, a fresh value is - /// immediately notified on the dispatch queue which starts the observation. - /// - Upon subsequent database changes, fresh values are notified on the - /// main queue. - /// - The observation lasts until the observer returned by - /// `add(observation:)` is deallocated. - /// - /// See ValueObservation for more information. - /// - /// - parameter region: a closure that returns the observed region. - /// - parameter fetch: a closure that fetches a value. - public static func observing( - _ regions: DatabaseRegionConvertible..., - reducer: Reducer) - -> ValueObservation - { - func region(_ db: Database) throws -> DatabaseRegion { - return try regions.reduce(into: DatabaseRegion()) { union, region in - try union.formUnion(region.databaseRegion(db)) - } - } - - return ValueObservation(observing: region, reducer: reducer) - } -} - extension ValueObservation where Reducer == Void { /// Creates a ValueObservation which observes *regions*, and notifies the /// values returned by the *fetch* closure whenever one of the observed