Bake uniquing right into ValueObservation: introduce ValueReducer

This commit is contained in:
Gwendal Roué committed 2018-10-27 16:12:42 +02:00
1 parent 697b2af61d
commit de227bdce1
5 files changed
+622 -291

No files matched your search

+1 -7
View File
@@ -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()
}
}
+6 -6
View File
@@ -153,10 +153,10 @@ public protocol DatabaseReader : class {
// MARK: - Value Observation
/// TODO
func add<Value>(
observation: ValueObservation<Value>,
func add<Reducer: ValueReducer>(
observation: ValueObservation<Reducer>,
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<Value>(
observation: ValueObservation<Value>,
public func add<Reducer: ValueReducer>(
observation: ValueObservation<Reducer>,
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)
+14 -8
View File
@@ -101,10 +101,10 @@ extension DatabaseSnapshot {
// MARK: - Value Observation
public func add<Value>(
observation: ValueObservation<Value>,
public func add<Reducer: ValueReducer>(
observation: ValueObservation<Reducer>,
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
+51 -41
View File
@@ -199,14 +199,14 @@ extension DatabaseWriter {
// MARK: - Value Observation
public func add<Value>(
observation: ValueObservation<Value>,
public func add<Reducer: ValueReducer>(
observation: ValueObservation<Reducer>,
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<Value>
let fetch: (Database) -> Future<Reducer.Fetched>
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<Value>: TransactionObserver {
private class ValueObserver<Reducer: ValueReducer>: TransactionObserver {
private let region: DatabaseRegion
private let futureValue: (Database) -> Future<Value>
private var reducer: Reducer
private let fetch: (Database) -> Future<Reducer.Fetched>
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<Value>: TransactionObserver {
init(
qos: DispatchQoS,
region: DatabaseRegion,
futureValue: @escaping (Database) -> Future<Value>,
reducer: Reducer,
fetch: @escaping (Database) -> Future<Reducer.Fetched>,
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<Value>: 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<Value>: 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<Value>(
observation: ValueObservation<Value>,
public func add<Reducer: ValueReducer>(
observation: ValueObservation<Reducer>,
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)
+550 -229
View File
@@ -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<Value> {
public struct ValueObservation<Reducer> {
/// 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<Value> {
///
/// 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<Value> {
/// - 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<Value> {
}
}
// MARK: - Reducers
public enum ValueReducers {
/// TODO
public struct Raw<Value>: 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<Value: Equatable>: 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<Record: FetchableRecord>: 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<Record: FetchableRecord>: 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<Value>(
_ regions: DatabaseRegionConvertible...,
fetch: @escaping (Database) throws -> Value)
-> ValueObservation<ValueReducers.Raw<Value>>
{
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<ValueReducers.Raw<Value>>(
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<Value>(
withUniquing regions: DatabaseRegionConvertible...,
fetch: @escaping (Database) throws -> Value)
-> ValueObservation<ValueReducers.Unique<Value>>
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<ValueReducers.Unique<Value>>(
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: FetchRequest>(_ request: Request)
-> ValueObservation<Int>
-> ValueObservation<ValueReducers.Raw<Int>>
{
return ValueObservation<Int>(
return ValueObservation<ValueReducers.Raw<Int>>(
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<Request: FetchRequest>(withUniquing request: Request)
-> ValueObservation<ValueReducers.Unique<Int>>
{
return ValueObservation<ValueReducers.Unique<Int>>(
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: FetchRequest>(_ request: Request)
-> ValueObservation<[Row]>
-> ValueObservation<ValueReducers.Raw<[Row]>>
where Request.RowDecoder == Row
{
return ValueObservation<[Row]>(
return ValueObservation<ValueReducers.Raw<[Row]>>(
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<Row>("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<Request: FetchRequest>(withUniquing request: Request)
-> ValueObservation<ValueReducers.Unique<[Row]>>
where Request.RowDecoder == Row
{
return ValueObservation<ValueReducers.Unique<[Row]>>(
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<Int>.
public static func forOne<Request: FetchRequest>(_ request: Request)
-> ValueObservation<Row?>
-> ValueObservation<ValueReducers.Raw<Row?>>
where Request.RowDecoder == Row
{
return ValueObservation<Row?>(
return ValueObservation<ValueReducers.Raw<Row?>>(
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<Row>("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<Int>.
public static func forOne<Request: FetchRequest>(withUniquing request: Request)
-> ValueObservation<ValueReducers.Unique<Row?>>
where Request.RowDecoder == Row
{
return ValueObservation<ValueReducers.Unique<Row?>>(
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: FetchRequest>(_ request: Request)
-> ValueObservation<[Request.RowDecoder]>
-> ValueObservation<ValueReducers.Raw<[Request.RowDecoder]>>
where Request.RowDecoder: FetchableRecord
{
return ValueObservation<[Request.RowDecoder]>(
return ValueObservation<ValueReducers.Raw<[Request.RowDecoder]>>(
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<Request: FetchRequest>(withUniquing request: Request)
-> ValueObservation<ValueReducers.UniqueRecords<Request.RowDecoder>>
where Request.RowDecoder: FetchableRecord
{
return ValueObservation<ValueReducers.UniqueRecords<Request.RowDecoder>>(
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: FetchRequest>(_ request: Request) ->
ValueObservation<Request.RowDecoder?>
ValueObservation<ValueReducers.Raw<Request.RowDecoder?>>
where Request.RowDecoder: FetchableRecord
{
return ValueObservation<Request.RowDecoder?>(
return ValueObservation<ValueReducers.Raw<Request.RowDecoder?>>(
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: FetchRequest>(_ 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: FetchRequest>(_ request: Request)
-> ValueObservation<Request.RowDecoder?>
where Request.RowDecoder: DatabaseValueConvertible
public static func forOne<Request: FetchRequest>(withUniquing request: Request) ->
ValueObservation<ValueReducers.UniqueRecord<Request.RowDecoder>>
where Request.RowDecoder: FetchableRecord
{
return ValueObservation<Request.RowDecoder?>(
return ValueObservation<ValueReducers.UniqueRecord<Request.RowDecoder>>(
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: FetchRequest>(_ 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: FetchRequest>(_ request: Request)
-> ValueObservation<Request.RowDecoder?>
where Request.RowDecoder: DatabaseValueConvertible & StatementColumnConvertible
{
return ValueObservation<Request.RowDecoder?>(
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: FetchRequest>(_ 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: FetchRequest>(_ 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: FetchRequest>(_ 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: FetchRequest>(_ request: Request)
// -> ValueObservation<Request.RowDecoder?>
// where Request.RowDecoder: DatabaseValueConvertible
// {
// return ValueObservation<Request.RowDecoder?>(
// 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: FetchRequest>(_ 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: FetchRequest>(_ request: Request)
// -> ValueObservation<Request.RowDecoder?>
// where Request.RowDecoder: DatabaseValueConvertible & StatementColumnConvertible
// {
// return ValueObservation<Request.RowDecoder?>(
// 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: FetchRequest>(_ 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: FetchRequest>(_ 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)
// }
//}