diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index eb940bae1..2da9e72cc 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -575,13 +575,7 @@ extension DatabasePool : DatabaseReader { return Future { // Block the future until results are fetched _ = futureSemaphore.wait(timeout: .distantFuture) - - switch futureResult! { - case .failure(let error): - throw error - case .success(let result): - return result - } + return try futureResult!.unwrap() } } diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index 47bbb4d65..f432cda52 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -153,10 +153,10 @@ public protocol DatabaseReader : class { // MARK: - Value Observation /// TODO - func add( - observation: ValueObservation, + func add( + observation: ValueObservation, onError: ((Error) -> Void)?, - onChange: @escaping (Value) -> Void) + onChange: @escaping (Reducer.Value) -> Void) throws -> TransactionObserver } @@ -241,10 +241,10 @@ public final class AnyDatabaseReader : DatabaseReader { // MARK: - Value Observation /// TODO - public func add( - observation: ValueObservation, + public func add( + observation: ValueObservation, onError: ((Error) -> Void)? = nil, - onChange: @escaping (Value) -> Void) + onChange: @escaping (Reducer.Value) -> Void) throws -> TransactionObserver { return try base.add(observation: observation, onError: onError, onChange: onChange) diff --git a/GRDB/Core/DatabaseSnapshot.swift b/GRDB/Core/DatabaseSnapshot.swift index 070dcc4d1..c417dd667 100644 --- a/GRDB/Core/DatabaseSnapshot.swift +++ b/GRDB/Core/DatabaseSnapshot.swift @@ -101,10 +101,10 @@ extension DatabaseSnapshot { // MARK: - Value Observation - public func add( - observation: ValueObservation, + public func add( + observation: ValueObservation, onError: ((Error) -> Void)? = nil, - onChange: @escaping (Value) -> Void) + onChange: @escaping (Reducer.Value) -> Void) throws -> TransactionObserver { // Deal with initial value @@ -112,13 +112,19 @@ extension DatabaseSnapshot { case .none: break case .deferred: - let value = try unsafeReentrantRead { try observation.fetch($0) } - observation.queue.async { - onChange(value) + var reducer = observation.reducer + let fetchedValue = try unsafeReentrantRead(reducer.fetch) + if let value = reducer.value(fetchedValue) { + observation.queue.async { + onChange(value) + } } case .immediateOnCurrentQueue: - let value = try unsafeReentrantRead { try observation.fetch($0) } - onChange(value) + var reducer = observation.reducer + let fetchedValue = try unsafeReentrantRead(reducer.fetch) + if let value = reducer.value(fetchedValue) { + onChange(value) + } } // Return a dummy observer, because snapshots never change diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index 3adcfd757..c8d49c603 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -199,14 +199,14 @@ extension DatabaseWriter { // MARK: - Value Observation - public func add( - observation: ValueObservation, + public func add( + observation: ValueObservation, onError: ((Error) -> Void)? = nil, - onChange: @escaping (Value) -> Void) + onChange: @escaping (Reducer.Value) -> Void) throws -> TransactionObserver { // Will be set and notified on the caller queue if initialDispatch is .immediateOnCurrentQueue - var immediateValue: Value? = nil + var immediateValue: Reducer.Value? = nil defer { if let immediateValue = immediateValue { onChange(immediateValue) @@ -219,18 +219,40 @@ extension DatabaseWriter { // We use an unsafe reentrant method so that this initializer can be // called from a database queue. return try unsafeReentrantWrite { db in + var reducer = observation.reducer + // The technique to fetch a fresh value after database has changed // depends on the observation.readonly flag: - let futureValue: (Database) -> Future + let fetch: (Database) -> Future if observation.readonly { // Concurrent read so that we don't block the writer queue - futureValue = { [unowned self] _ in - self.concurrentRead { db in try observation.fetch(db) } + fetch = { [unowned self] _ in + self.concurrentRead { db in try reducer.fetch(db) } } } else { // Synchronous fetch from the writer queue - futureValue = { db in - Future(Result { try observation.fetch(db) }) + fetch = { db in + Future(Result { try reducer.fetch(db) }) + } + } + + // Deal with initial value + switch observation.initialDispatch { + case .none: + break + case .immediateOnCurrentQueue: + let fetchedValue = try unsafeReentrantRead(reducer.fetch) + immediateValue = reducer.value(fetchedValue) + case .deferred: + // We're still on the database writer queue. Let's dispatch + // initial value right now, before any future transaction has + // any opportunity to trigger a change notification, and mess + // with the ordering of value notifications. + let fetchedValue = try unsafeReentrantRead(reducer.fetch) + if let value = reducer.value(fetchedValue) { + observation.queue.async { + onChange(value) + } } } @@ -238,29 +260,13 @@ extension DatabaseWriter { let transactionObserver = try ValueObserver( qos: observation.qos, region: observation.observedRegion(db), - futureValue: futureValue, + reducer: reducer, + fetch: fetch, queue: observation.queue, onError: onError, onChange: onChange) db.add(transactionObserver: transactionObserver, extent: observation.extent) - // Deal with initial value - switch observation.initialDispatch { - case .none: - break - case .immediateOnCurrentQueue: - immediateValue = try observation.fetch(db) - case .deferred: - // We're still on the database writer queue. Let's dispatch - // initial value right now, before any future transaction has - // any opportunity to trigger a change notification, and mess - // with the ordering of value notifications. - let initialValue = try observation.fetch(db) - observation.queue.async { - onChange(initialValue) - } - } - return transactionObserver } } @@ -268,11 +274,12 @@ extension DatabaseWriter { /// Support for ValueObservation. /// See DatabaseWriter.add(observation:onError:onChange:) -private class ValueObserver: TransactionObserver { +private class ValueObserver: TransactionObserver { private let region: DatabaseRegion - private let futureValue: (Database) -> Future + private var reducer: Reducer + private let fetch: (Database) -> Future private let queue: DispatchQueue - private let onChange: (Value) -> Void + private let onChange: (Reducer.Value) -> Void private let onError: ((Error) -> Void)? private let notificationQueue: DispatchQueue private var changed = false @@ -280,13 +287,15 @@ private class ValueObserver: TransactionObserver { init( qos: DispatchQoS, region: DatabaseRegion, - futureValue: @escaping (Database) -> Future, + reducer: Reducer, + fetch: @escaping (Database) -> Future, queue: DispatchQueue, onError: ((Error) -> Void)?, - onChange: @escaping (Value) -> Void) + onChange: @escaping (Reducer.Value) -> Void) { self.region = region - self.futureValue = futureValue + self.reducer = reducer + self.fetch = fetch self.queue = queue self.onChange = onChange self.onError = onError @@ -312,7 +321,7 @@ private class ValueObserver: TransactionObserver { changed = false // Fetch future value from the database writer queue - let futureValue = self.futureValue(db) + let fetched = self.fetch(db) // Wait for future value in notificationQueue, in order to guarantee // that notifications have the same ordering as transactions. @@ -320,10 +329,11 @@ private class ValueObserver: TransactionObserver { guard let strongSelf = self else { return } do { - let value = try futureValue.wait() - strongSelf.queue.async { - guard let strongSelf = self else { return } - strongSelf.onChange(value) + if let value = try strongSelf.reducer.value(fetched.wait()) { + strongSelf.queue.async { + guard let strongSelf = self else { return } + strongSelf.onChange(value) + } } } catch { strongSelf.queue.async { @@ -449,10 +459,10 @@ public final class AnyDatabaseWriter : DatabaseWriter { // MARK: - Value Observation - public func add( - observation: ValueObservation, + public func add( + observation: ValueObservation, onError: ((Error) -> Void)? = nil, - onChange: @escaping (Value) -> Void) + onChange: @escaping (Reducer.Value) -> Void) throws -> TransactionObserver { return try base.add(observation: observation, onError: onError, onChange: onChange) diff --git a/GRDB/Core/ValueObservation.swift b/GRDB/Core/ValueObservation.swift index 123ec86f7..2da4237cb 100644 --- a/GRDB/Core/ValueObservation.swift +++ b/GRDB/Core/ValueObservation.swift @@ -21,9 +21,17 @@ public enum InitialDispatch { case deferred } +/// TODO: doc +public protocol ValueReducer { + associatedtype Fetched + associatedtype Value + func fetch(_ db: Database) throws -> Fetched + mutating func value(_ fetched: Fetched) -> Value? +} + /// TODO: doc /// TODO: refresh region after each fetch -public struct ValueObservation { +public struct ValueObservation { /// A closure that is evaluated when the observation starts, and returns /// the observed database region. var observedRegion: (Database) throws -> DatabaseRegion @@ -34,7 +42,7 @@ public struct ValueObservation { /// /// When this closure needs to write in the database, set the *readonly* /// flag to false. - var fetch: (Database) throws -> Value + var reducer: Reducer /// Default is true. Set this property to false when the *fetch* closure /// requires write access, and should be executed inside a savepoint. @@ -98,10 +106,10 @@ public struct ValueObservation { /// - parameter fetch: a closure that fetches a value. public init( observing region: @escaping (Database) throws -> DatabaseRegion, - fetch: @escaping (Database) throws -> Value) + reducer: Reducer) { self.observedRegion = region - self.fetch = fetch + self.reducer = reducer if #available(OSXApplicationExtension 10.10, *) { self.qos = .default } else { @@ -110,17 +118,113 @@ public struct ValueObservation { } } +// MARK: - Reducers + +public enum ValueReducers { + /// TODO + public struct Raw: ValueReducer { + let _fetch: (Database) throws -> Value + + /// TODO + public init(_ fetch: @escaping (Database) throws -> Value) { + self._fetch = fetch + } + + /// :nodoc: + public func fetch(_ db: Database) throws -> Value { + return try _fetch(db) + } + + /// :nodoc: + public func value(_ fetched: Value) -> Value? { + return fetched + } + } + + public struct Unique: ValueReducer { + let _fetch: (Database) throws -> Value + var value: Value?? + + /// TODO + public init(_ fetch: @escaping (Database) throws -> Value) { + self._fetch = fetch + } + + /// :nodoc: + public func fetch(_ db: Database) throws -> Value { + return try _fetch(db) + } + + /// :nodoc: + public mutating func value(_ newValue: Value) -> Value? { + if let value = value, value == newValue { + return nil + } + self.value = newValue + return newValue + } + } + + public struct UniqueRecords: ValueReducer { + let _fetch: (Database) throws -> [Row] + var rows: [Row]? + + /// TODO + public init(_ fetch: @escaping (Database) throws -> [Row]) { + self._fetch = fetch + } + + /// :nodoc: + public func fetch(_ db: Database) throws -> [Row] { + return try _fetch(db) + } + + /// :nodoc: + public mutating func value(_ newRows: [Row]) -> [Record]? { + if let rows = rows, rows == newRows { + return nil + } + self.rows = newRows + return newRows.map(Record.init(row:)) + } + } + + public struct UniqueRecord: ValueReducer { + let _fetch: (Database) throws -> Row? + var row: Row?? + + /// TODO + public init(_ fetch: @escaping (Database) throws -> Row?) { + self._fetch = fetch + } + + /// :nodoc: + public func fetch(_ db: Database) throws -> Row? { + return try _fetch(db) + } + + /// :nodoc: + public mutating func value(_ newRow: Row?) -> Record? { + if let row = row, row == newRow { + return nil + } + self.row = newRow + return newRow.map(Record.init(row:)) + } + } +} + // MARK: - DatabaseRegionConvertible Observation -extension ValueObservation { +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 /// regions is impacted by a database transaction. /// /// For example: /// - /// let observation = ValueObservation( - /// observing: Player.all(), + /// let observation = ValueObservation.observing( + /// Player.all(), /// fetch: { db in return try Player.fetchAll(db) }) /// /// let observer = dbQueue.add(observation: observation) { player: [Player] in @@ -140,9 +244,10 @@ extension ValueObservation { /// /// - parameter regions: one or more observed requests. /// - parameter fetch: a closure that fetches a value. - public init( - observing regions: DatabaseRegionConvertible..., + public static func observing( + _ regions: DatabaseRegionConvertible..., fetch: @escaping (Database) throws -> Value) + -> ValueObservation> { func region(_ db: Database) throws -> DatabaseRegion { return try regions.reduce(into: DatabaseRegion()) { union, region in @@ -150,13 +255,59 @@ extension ValueObservation { } } - self.init(observing: region, fetch: fetch) + return ValueObservation>( + observing: region, + reducer: ValueReducers.Raw(fetch)) + } + + /// Creates a ValueObservation which observes *regions*, and notifies the + /// values returned by the *fetch* closure whenever one of the observed + /// regions is impacted by a database transaction. + /// + /// For example: + /// + /// let observation = ValueObservation.observing( + /// withUniquing: Player.all(), + /// fetch: { db in return try Player.fetchAll(db) }) + /// + /// let observer = 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 regions: one or more observed requests. + /// - parameter fetch: a closure that fetches a value. + public static func observing( + withUniquing regions: DatabaseRegionConvertible..., + fetch: @escaping (Database) throws -> Value) + -> ValueObservation> + where Value: Equatable + { + 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: ValueReducers.Unique(fetch)) } } // MARK: - FetchRequest Observation -extension ValueObservation where Value == Void { +extension ValueObservation where Reducer == Void { /// Creates a ValueObservation which observes *request*, and notifies a /// fresh count whenever the request is impacted by a database transaction. /// @@ -183,17 +334,50 @@ extension ValueObservation where Value == Void { /// - parameter request: the observed request. /// - returns: a ValueObservation. public static func forCount(_ request: Request) - -> ValueObservation + -> ValueObservation> { - return ValueObservation( + return ValueObservation>( observing: request.databaseRegion, - fetch: request.fetchCount) + reducer: ValueReducers.Raw(request.fetchCount)) + } + + /// Creates a ValueObservation which observes *request*, and notifies a + /// fresh count whenever the request is impacted by a database transaction. + /// + /// For example: + /// + /// let request = Player.all() + /// let observation = ValueObservation.forCount(withUniquing: request) + /// + /// let observer = dbQueue.add(observation: observation) { count: Int in + /// print("number of players has 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 request: the observed request. + /// - returns: a ValueObservation. + public static func forCount(withUniquing request: Request) + -> ValueObservation> + { + return ValueObservation>( + observing: request.databaseRegion, + reducer: ValueReducers.Unique(request.fetchCount)) } } // MARK: - Row Observation -extension ValueObservation where Value == Void { +extension ValueObservation where Reducer == Void { /// Creates a ValueObservation which observes *request*, and notifies /// fresh rows whenever the request is impacted by a database transaction. /// @@ -220,14 +404,48 @@ extension ValueObservation where Value == Void { /// - parameter request: the observed request. /// - returns: a ValueObservation. public static func forAll(_ request: Request) - -> ValueObservation<[Row]> + -> ValueObservation> where Request.RowDecoder == Row { - return ValueObservation<[Row]>( + return ValueObservation>( observing: request.databaseRegion, - fetch: request.fetchAll) + reducer: ValueReducers.Raw(request.fetchAll)) } + /// Creates a ValueObservation which observes *request*, and notifies + /// fresh rows whenever the request is impacted by a database transaction. + /// + /// For example: + /// + /// let request = SQLRequest("SELECT * FROM player") + /// let observation = ValueObservation.forAll(withUniquing: request) + /// + /// let observer = dbQueue.add(observation: observation) { rows: [Row] 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 request: the observed request. + /// - returns: a ValueObservation. + public static func forAll(withUniquing request: Request) + -> ValueObservation> + where Request.RowDecoder == Row + { + return ValueObservation>( + observing: request.databaseRegion, + reducer: ValueReducers.Unique(request.fetchAll)) + } + /// Creates a ValueObservation which observes *request*, and notifies a /// fresh row whenever the request is impacted by a database transaction. /// @@ -254,18 +472,52 @@ extension ValueObservation where Value == Void { /// - parameter request: the observed request. /// - returns: a new ValueObservation. public static func forOne(_ request: Request) - -> ValueObservation + -> ValueObservation> where Request.RowDecoder == Row { - return ValueObservation( + return ValueObservation>( observing: request.databaseRegion, - fetch: request.fetchOne) + reducer: ValueReducers.Raw(request.fetchOne)) + } + + /// Creates a ValueObservation which observes *request*, and notifies a + /// fresh row whenever the request is impacted by a database transaction. + /// + /// For example: + /// + /// let request = SQLRequest("SELECT * FROM player WHERE id = ?", arguments: [1]) + /// let observation = ValueObservation.forOne(withUniquing: request) + /// + /// let observer = dbQueue.add(observation: observation) { row: Row? 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 request: the observed request. + /// - returns: a new ValueObservation. + public static func forOne(withUniquing request: Request) + -> ValueObservation> + where Request.RowDecoder == Row + { + return ValueObservation>( + observing: request.databaseRegion, + reducer: ValueReducers.Unique(request.fetchOne)) } } // MARK: - FetchableRecord Observation -extension ValueObservation where Value == Void { +extension ValueObservation where Reducer == Void { /// Creates a ValueObservation which observes *request*, and notifies /// fresh records whenever the request is impacted by a /// database transaction. @@ -293,12 +545,47 @@ extension ValueObservation where Value == Void { /// - parameter request: the observed request. /// - returns: a ValueObservation. public static func forAll(_ request: Request) - -> ValueObservation<[Request.RowDecoder]> + -> ValueObservation> where Request.RowDecoder: FetchableRecord { - return ValueObservation<[Request.RowDecoder]>( + return ValueObservation>( observing: request.databaseRegion, - fetch: request.fetchAll) + reducer: ValueReducers.Raw(request.fetchAll)) + } + + /// Creates a ValueObservation which observes *request*, and notifies + /// fresh records whenever the request is impacted by a + /// database transaction. + /// + /// For example: + /// + /// let request = Player.all() + /// let observation = ValueObservation.forAll(withUniquing: request) + /// + /// let observer = dbQueue.add(observation: observation) { players: [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 request: the observed request. + /// - returns: a ValueObservation. + public static func forAll(withUniquing request: Request) + -> ValueObservation> + where Request.RowDecoder: FetchableRecord + { + return ValueObservation>( + observing: request.databaseRegion, + reducer: ValueReducers.UniqueRecords { try Row.fetchAll($0, request) }) } /// Creates a ValueObservation which observes *request*, and notifies a @@ -327,63 +614,24 @@ extension ValueObservation where Value == Void { /// - parameter request: the observed request. /// - returns: a ValueObservation. public static func forOne(_ request: Request) -> - ValueObservation + ValueObservation> where Request.RowDecoder: FetchableRecord { - return ValueObservation( + return ValueObservation>( observing: request.databaseRegion, - fetch: request.fetchOne) - } -} - -// MARK: - DatabaseValueConvertible Observation - -extension ValueObservation where Value == Void { - /// Creates a ValueObservation which observes *request*, and notifies - /// fresh values whenever the request is impacted by a - /// database transaction. - /// - /// For example: - /// - /// let request = Player.select(Column("name"), as: String.self) - /// let observation = ValueObservation.forAll(request) - /// - /// let observer = dbQueue.add(observation: observation) { names: [String] in - /// print("player name 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 request: the observed request. - /// - returns: a ValueObservation. - public static func forAll(_ request: Request) - -> ValueObservation<[Request.RowDecoder]> - where Request.RowDecoder: DatabaseValueConvertible - { - return ValueObservation<[Request.RowDecoder]>( - observing: request.databaseRegion, - fetch: request.fetchAll) + reducer: ValueReducers.Raw(request.fetchOne)) } /// Creates a ValueObservation which observes *request*, and notifies a - /// fresh value whenever the request is impacted by a database transaction. + /// fresh record whenever the request is impacted by a database transaction. /// /// For example: /// - /// let request = Player.select(max(Column("score")), as: Int.self) - /// let observation = ValueObservation.forOne(request) + /// let request = Player.filter(key: 1) + /// let observation = ValueObservation.forOne(withUniquing: request) /// - /// let observer = dbQueue.add(observation: observation) { maxScore: Int? in - /// print("maximum score has changed") + /// let observer = dbQueue.add(observation: observation) { player: Player? in + /// print("player has changed") /// } /// /// The returned observation has the default configuration: @@ -399,165 +647,238 @@ extension ValueObservation where Value == Void { /// /// - parameter request: the observed request. /// - returns: a ValueObservation. - public static func forOne(_ request: Request) - -> ValueObservation - where Request.RowDecoder: DatabaseValueConvertible + public static func forOne(withUniquing request: Request) -> + ValueObservation> + where Request.RowDecoder: FetchableRecord { - return ValueObservation( + return ValueObservation>( observing: request.databaseRegion, - fetch: request.fetchOne) + reducer: ValueReducers.UniqueRecord { try Row.fetchOne($0, request) }) } } -// MARK: - DatabaseValueConvertible & StatementColumnConvertible Observation - -extension ValueObservation where Value == Void { - /// Creates a ValueObservation which observes *request*, and notifies - /// fresh values whenever the request is impacted by a - /// database transaction. - /// - /// For example: - /// - /// let request = Player.select(Column("name"), as: String.self) - /// let observation = ValueObservation.forAll(request) - /// - /// let observer = dbQueue.add(observation: observation) { names: [String] in - /// print("player name 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 request: the observed request. - /// - returns: a ValueObservation. - public static func forAll(_ request: Request) - -> ValueObservation<[Request.RowDecoder]> - where Request.RowDecoder: DatabaseValueConvertible & StatementColumnConvertible - { - return ValueObservation<[Request.RowDecoder]>( - observing: request.databaseRegion, - fetch: request.fetchAll) - } - - /// Creates a ValueObservation which observes *request*, and notifies a - /// fresh value whenever the request is impacted by a database transaction. - /// - /// For example: - /// - /// let request = Player.select(max(Column("score")), as: Int.self) - /// let observation = ValueObservation.forOne(request) - /// - /// let observer = dbQueue.add(observation: observation) { maxScore: Int? in - /// print("maximum score has 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 request: the observed request. - /// - returns: a ValueObservation. - public static func forOne(_ request: Request) - -> ValueObservation - where Request.RowDecoder: DatabaseValueConvertible & StatementColumnConvertible - { - return ValueObservation( - observing: request.databaseRegion, - fetch: request.fetchOne) - } -} - -// MARK: - Optional DatabaseValueConvertible Observation - -extension ValueObservation where Value == Void { - /// Creates a ValueObservation which observes *request*, and notifies - /// fresh values whenever the request is impacted by a - /// database transaction. - /// - /// For example: - /// - /// let request = Player.select(Column("name"), as: String.self) - /// let observation = ValueObservation.forAll(request) - /// - /// let observer = dbQueue.add(observation: observation) { names: [String] in - /// print("player name 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 request: the observed request. - /// - returns: a ValueObservation. - public static func forAll(_ request: Request) - -> ValueObservation<[Request.RowDecoder._Wrapped?]> - where Request.RowDecoder: _OptionalProtocol, - Request.RowDecoder._Wrapped: DatabaseValueConvertible - { - return ValueObservation<[Request.RowDecoder._Wrapped?]>( - observing: request.databaseRegion, - fetch: request.fetchAll) - } -} - -// MARK: - Optional DatabaseValueConvertible & StatementColumnConvertible Observation - -extension ValueObservation where Value == Void { - /// Creates a ValueObservation which observes *request*, and notifies - /// fresh values whenever the request is impacted by a - /// database transaction. - /// - /// For example: - /// - /// let request = Player.select(Column("name"), as: String.self) - /// let observation = ValueObservation.forAll(request) - /// - /// let observer = dbQueue.add(observation: observation) { names: [String] in - /// print("player name 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 request: the observed request. - /// - returns: a ValueObservation. - public static func forAll(_ request: Request) - -> ValueObservation<[Request.RowDecoder._Wrapped?]> - where Request.RowDecoder: _OptionalProtocol, - Request.RowDecoder._Wrapped: DatabaseValueConvertible & StatementColumnConvertible - { - return ValueObservation<[Request.RowDecoder._Wrapped?]>( - observing: request.databaseRegion, - fetch: request.fetchAll) - } -} +//// MARK: - DatabaseValueConvertible Observation +// +//extension ValueObservation where Value == Void { +// /// Creates a ValueObservation which observes *request*, and notifies +// /// fresh values whenever the request is impacted by a +// /// database transaction. +// /// +// /// For example: +// /// +// /// let request = Player.select(Column("name"), as: String.self) +// /// let observation = ValueObservation.forAll(request) +// /// +// /// let observer = dbQueue.add(observation: observation) { names: [String] in +// /// print("player name 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 request: the observed request. +// /// - returns: a ValueObservation. +// public static func forAll(_ request: Request) +// -> ValueObservation<[Request.RowDecoder]> +// where Request.RowDecoder: DatabaseValueConvertible +// { +// return ValueObservation<[Request.RowDecoder]>( +// observing: request.databaseRegion, +// fetch: request.fetchAll) +// } +// +// /// Creates a ValueObservation which observes *request*, and notifies a +// /// fresh value whenever the request is impacted by a database transaction. +// /// +// /// For example: +// /// +// /// let request = Player.select(max(Column("score")), as: Int.self) +// /// let observation = ValueObservation.forOne(request) +// /// +// /// let observer = dbQueue.add(observation: observation) { maxScore: Int? in +// /// print("maximum score has 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 request: the observed request. +// /// - returns: a ValueObservation. +// public static func forOne(_ request: Request) +// -> ValueObservation +// where Request.RowDecoder: DatabaseValueConvertible +// { +// return ValueObservation( +// observing: request.databaseRegion, +// fetch: request.fetchOne) +// } +//} +// +//// MARK: - DatabaseValueConvertible & StatementColumnConvertible Observation +// +//extension ValueObservation where Value == Void { +// /// Creates a ValueObservation which observes *request*, and notifies +// /// fresh values whenever the request is impacted by a +// /// database transaction. +// /// +// /// For example: +// /// +// /// let request = Player.select(Column("name"), as: String.self) +// /// let observation = ValueObservation.forAll(request) +// /// +// /// let observer = dbQueue.add(observation: observation) { names: [String] in +// /// print("player name 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 request: the observed request. +// /// - returns: a ValueObservation. +// public static func forAll(_ request: Request) +// -> ValueObservation<[Request.RowDecoder]> +// where Request.RowDecoder: DatabaseValueConvertible & StatementColumnConvertible +// { +// return ValueObservation<[Request.RowDecoder]>( +// observing: request.databaseRegion, +// fetch: request.fetchAll) +// } +// +// /// Creates a ValueObservation which observes *request*, and notifies a +// /// fresh value whenever the request is impacted by a database transaction. +// /// +// /// For example: +// /// +// /// let request = Player.select(max(Column("score")), as: Int.self) +// /// let observation = ValueObservation.forOne(request) +// /// +// /// let observer = dbQueue.add(observation: observation) { maxScore: Int? in +// /// print("maximum score has 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 request: the observed request. +// /// - returns: a ValueObservation. +// public static func forOne(_ request: Request) +// -> ValueObservation +// where Request.RowDecoder: DatabaseValueConvertible & StatementColumnConvertible +// { +// return ValueObservation( +// observing: request.databaseRegion, +// fetch: request.fetchOne) +// } +//} +// +//// MARK: - Optional DatabaseValueConvertible Observation +// +//extension ValueObservation where Value == Void { +// /// Creates a ValueObservation which observes *request*, and notifies +// /// fresh values whenever the request is impacted by a +// /// database transaction. +// /// +// /// For example: +// /// +// /// let request = Player.select(Column("name"), as: String.self) +// /// let observation = ValueObservation.forAll(request) +// /// +// /// let observer = dbQueue.add(observation: observation) { names: [String] in +// /// print("player name 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 request: the observed request. +// /// - returns: a ValueObservation. +// public static func forAll(_ request: Request) +// -> ValueObservation<[Request.RowDecoder._Wrapped?]> +// where Request.RowDecoder: _OptionalProtocol, +// Request.RowDecoder._Wrapped: DatabaseValueConvertible +// { +// return ValueObservation<[Request.RowDecoder._Wrapped?]>( +// observing: request.databaseRegion, +// fetch: request.fetchAll) +// } +//} +// +//// MARK: - Optional DatabaseValueConvertible & StatementColumnConvertible Observation +// +//extension ValueObservation where Value == Void { +// /// Creates a ValueObservation which observes *request*, and notifies +// /// fresh values whenever the request is impacted by a +// /// database transaction. +// /// +// /// For example: +// /// +// /// let request = Player.select(Column("name"), as: String.self) +// /// let observation = ValueObservation.forAll(request) +// /// +// /// let observer = dbQueue.add(observation: observation) { names: [String] in +// /// print("player name 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 request: the observed request. +// /// - returns: a ValueObservation. +// public static func forAll(_ request: Request) +// -> ValueObservation<[Request.RowDecoder._Wrapped?]> +// where Request.RowDecoder: _OptionalProtocol, +// Request.RowDecoder._Wrapped: DatabaseValueConvertible & StatementColumnConvertible +// { +// return ValueObservation<[Request.RowDecoder._Wrapped?]>( +// observing: request.databaseRegion, +// fetch: request.fetchAll) +// } +//}