mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Remove public ValueObservation factory methods that accept a _ValueReducer
This commit is contained in:
1 parent
28c32c6af8
commit
b96d71d937
10 files changed
+116
-370
No files matched your search
@@ -1,4 +1,5 @@
|
||||
// Fixits for changes introduced by GRDB 5.0.0
|
||||
// swiftlint:disable all
|
||||
|
||||
#if os(iOS)
|
||||
import UIKit
|
||||
@@ -142,6 +143,16 @@ extension Statement {
|
||||
{ preconditionFailure() }
|
||||
}
|
||||
|
||||
extension ValueObservation {
|
||||
@available(*, unavailable, message: "Custom reducers are no longer supported")
|
||||
public static func tracking(_ regions: DatabaseRegionConvertible..., reducer: @escaping (Database) throws -> Reducer) -> ValueObservation
|
||||
{ preconditionFailure() }
|
||||
|
||||
@available(*, unavailable, message: "Custom reducers are no longer supported")
|
||||
public static func tracking(_ regions: [DatabaseRegionConvertible], reducer: @escaping (Database) throws -> Reducer) -> ValueObservation
|
||||
{ preconditionFailure() }
|
||||
}
|
||||
|
||||
extension ValueObservation where Reducer == Never {
|
||||
@available(*, unavailable, message: "Use request.observationForCount() instead")
|
||||
public static func trackingCount<Request: FetchRequest>(_ request: Request)
|
||||
|
||||
@@ -26,9 +26,9 @@ extension FetchRequest where RowDecoder: DatabaseValueConvertible {
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForAll() -> ValueObservation<ValueReducers.AllValues<RowDecoder>> {
|
||||
ValueObservation.tracking(self, reducer: { _ in
|
||||
ValueReducers.AllValues { try DatabaseValue.fetchAll($0, self) }
|
||||
})
|
||||
ValueObservation(
|
||||
baseRegion: databaseRegion,
|
||||
makeReducer: { _ in ValueReducers.AllValues { try DatabaseValue.fetchAll($0, self) } })
|
||||
}
|
||||
|
||||
/// Creates a ValueObservation which observes *request*, and notifies a
|
||||
@@ -55,9 +55,9 @@ extension FetchRequest where RowDecoder: DatabaseValueConvertible {
|
||||
/// - parameter request: the observed request.
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForFirst() -> ValueObservation<ValueReducers.OneValue<RowDecoder>> {
|
||||
ValueObservation.tracking(self, reducer: { _ in
|
||||
ValueReducers.OneValue { try DatabaseValue.fetchOne($0, self) }
|
||||
})
|
||||
ValueObservation(
|
||||
baseRegion: databaseRegion,
|
||||
makeReducer: { _ in ValueReducers.OneValue { try DatabaseValue.fetchOne($0, self) } })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,9 +89,9 @@ extension FetchRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped:
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForAll() -> ValueObservation<ValueReducers.AllOptionalValues<RowDecoder._Wrapped>> {
|
||||
ValueObservation.tracking(self, reducer: { _ in
|
||||
ValueReducers.AllOptionalValues { try DatabaseValue.fetchAll($0, self) }
|
||||
})
|
||||
ValueObservation(
|
||||
baseRegion: databaseRegion,
|
||||
makeReducer: { _ in ValueReducers.AllOptionalValues { try DatabaseValue.fetchAll($0, self) } })
|
||||
}
|
||||
|
||||
/// Creates a ValueObservation which observes *request*, and notifies
|
||||
@@ -118,9 +118,9 @@ extension FetchRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped:
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForFirst() -> ValueObservation<ValueReducers.OneValue<RowDecoder._Wrapped>> {
|
||||
ValueObservation.tracking(self, reducer: { _ in
|
||||
ValueReducers.OneValue { try DatabaseValue.fetchOne($0, self) }
|
||||
})
|
||||
ValueObservation(
|
||||
baseRegion: databaseRegion,
|
||||
makeReducer: { _ in ValueReducers.OneValue { try DatabaseValue.fetchOne($0, self) } })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,9 +26,9 @@ extension FetchRequest where RowDecoder: FetchableRecord {
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForAll() -> ValueObservation<ValueReducers.AllRecords<RowDecoder>> {
|
||||
ValueObservation.tracking(self, reducer: { _ in
|
||||
ValueReducers.AllRecords { try Row.fetchAll($0, self) }
|
||||
})
|
||||
ValueObservation(
|
||||
baseRegion: databaseRegion,
|
||||
makeReducer: { _ in ValueReducers.AllRecords { try Row.fetchAll($0, self) } })
|
||||
}
|
||||
|
||||
/// Creates a ValueObservation which observes *request*, and notifies a
|
||||
@@ -54,9 +54,9 @@ extension FetchRequest where RowDecoder: FetchableRecord {
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForFirst() -> ValueObservation<ValueReducers.OneRecord<RowDecoder>> {
|
||||
ValueObservation.tracking(self, reducer: { _ in
|
||||
ValueReducers.OneRecord { try Row.fetchOne($0, self) }
|
||||
})
|
||||
ValueObservation(
|
||||
baseRegion: databaseRegion,
|
||||
makeReducer: { _ in ValueReducers.OneRecord { try Row.fetchOne($0, self) } })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,9 +25,9 @@ extension FetchRequest where RowDecoder == Row {
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForAll() -> ValueObservation<ValueReducers.AllRows> {
|
||||
ValueObservation.tracking(self, reducer: { _ in
|
||||
ValueReducers.AllRows(fetch: self.fetchAll)
|
||||
})
|
||||
ValueObservation(
|
||||
baseRegion: databaseRegion,
|
||||
makeReducer: { _ in ValueReducers.AllRows(fetch: self.fetchAll) })
|
||||
}
|
||||
|
||||
/// Creates a ValueObservation which observes *request*, and notifies a
|
||||
@@ -53,9 +53,9 @@ extension FetchRequest where RowDecoder == Row {
|
||||
///
|
||||
/// - returns: a ValueObservation.
|
||||
public func observationForFirst() -> ValueObservation<ValueReducers.OneRow> {
|
||||
ValueObservation.tracking(self, reducer: { _ in
|
||||
ValueReducers.OneRow(fetch: self.fetchOne)
|
||||
})
|
||||
ValueObservation(
|
||||
baseRegion: databaseRegion,
|
||||
makeReducer: { _ in ValueReducers.OneRow(fetch: self.fetchOne) })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ public struct ValueObservation<Reducer: _ValueReducer> {
|
||||
|
||||
/// If true, the region selected by the reducer is observed as well
|
||||
/// as `baseRegion`.
|
||||
var observesSelectedRegion: Bool
|
||||
var observesSelectedRegion: Bool = false
|
||||
|
||||
/// The reducer is created when observation starts, and is triggered upon
|
||||
/// each database change in *observedRegion*.
|
||||
@@ -91,11 +91,11 @@ public struct ValueObservation<Reducer: _ValueReducer> {
|
||||
///
|
||||
/// Don't set this flag to true unless you really need it. A read/write
|
||||
/// observation is less efficient than a read-only observation.
|
||||
public var requiresWriteAccess: Bool
|
||||
public var requiresWriteAccess: Bool = false
|
||||
|
||||
/// `scheduling` controls how fresh values are notified. Default
|
||||
/// is `.mainQueue`.
|
||||
public var scheduling: ValueObservationScheduling
|
||||
public var scheduling: ValueObservationScheduling = .mainQueue
|
||||
}
|
||||
|
||||
extension ValueObservation {
|
||||
@@ -147,98 +147,6 @@ extension ValueObservation {
|
||||
}
|
||||
}
|
||||
|
||||
extension ValueObservation {
|
||||
|
||||
// MARK: - Creating ValueObservation from _ValueReducer
|
||||
|
||||
/// Returns a ValueObservation which observes *regions*, and notifies the
|
||||
/// values returned by the *reducer* whenever one of the observed
|
||||
/// regions is modified by a database transaction.
|
||||
///
|
||||
/// This method is the most fundamental way to create a ValueObservation.
|
||||
///
|
||||
/// For example, this observation counts the number of a times the player
|
||||
/// table is modified:
|
||||
///
|
||||
/// var count = 0
|
||||
/// let reducer = AnyValueReducer(
|
||||
/// fetch: { _ in /* don't fetch anything */ },
|
||||
/// value: { _ -> Int? in
|
||||
/// defer { count += 1 }
|
||||
/// return count })
|
||||
/// let observation = ValueObservation.tracking(Player.all(), reducer: { db in reducer })
|
||||
/// let observer = observation.start(in: dbQueue) { count: Int in
|
||||
/// print("Players have been modified \(count) times.")
|
||||
/// }
|
||||
///
|
||||
/// The returned observation has the default configuration:
|
||||
///
|
||||
/// - When started with the `start(in:onError:onChange:)` method, a fresh
|
||||
/// value is immediately notified on the main queue.
|
||||
/// - Upon subsequent database changes, fresh values are notified on the
|
||||
/// main queue.
|
||||
/// - The observation lasts until the observer returned by
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - parameter regions: A list of observed regions.
|
||||
/// - parameter reducer: A reducer that turns database changes in the
|
||||
/// modified regions into fresh values. Currently only reducers that adopt
|
||||
/// the _ValueReducer protocol are supported.
|
||||
public static func tracking(
|
||||
_ regions: DatabaseRegionConvertible...,
|
||||
reducer: @escaping (Database) throws -> Reducer)
|
||||
-> ValueObservation
|
||||
{
|
||||
return ValueObservation.tracking(regions, reducer: reducer)
|
||||
}
|
||||
|
||||
/// Returns a ValueObservation which observes *regions*, and notifies the
|
||||
/// values returned by the *reducer* whenever one of the observed
|
||||
/// regions is modified by a database transaction.
|
||||
///
|
||||
/// This method is the most fundamental way to create a ValueObservation.
|
||||
///
|
||||
/// For example, this observation counts the number of a times the player
|
||||
/// table is modified:
|
||||
///
|
||||
/// var count = 0
|
||||
/// let reducer = AnyValueReducer(
|
||||
/// fetch: { _ in /* don't fetch anything */ },
|
||||
/// value: { _ -> Int? in
|
||||
/// defer { count += 1 }
|
||||
/// return count })
|
||||
/// let observation = ValueObservation.tracking([Player.all()], reducer: { db in reducer })
|
||||
/// let observer = observation.start(in: dbQueue) { count: Int in
|
||||
/// print("Players have been modified \(count) times.")
|
||||
/// }
|
||||
///
|
||||
/// The returned observation has the default configuration:
|
||||
///
|
||||
/// - When started with the `start(in:onError:onChange:)` method, a fresh
|
||||
/// value is immediately notified on the main queue.
|
||||
/// - Upon subsequent database changes, fresh values are notified on the
|
||||
/// main queue.
|
||||
/// - The observation lasts until the observer returned by
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - parameter regions: A list of observed regions.
|
||||
/// - parameter reducer: A reducer that turns database changes in the
|
||||
/// modified regions into fresh values. Currently only reducers that adopt
|
||||
/// the _ValueReducer protocol are supported.
|
||||
public static func tracking(
|
||||
_ regions: [DatabaseRegionConvertible],
|
||||
reducer: @escaping (Database) throws -> Reducer)
|
||||
-> ValueObservation
|
||||
{
|
||||
return ValueObservation(
|
||||
baseRegion: DatabaseRegion.union(regions),
|
||||
observesSelectedRegion: false,
|
||||
makeReducer: reducer,
|
||||
requiresWriteAccess: false,
|
||||
scheduling: .mainQueue)
|
||||
}
|
||||
}
|
||||
|
||||
extension ValueObservation where Reducer == Never {
|
||||
|
||||
// MARK: - Creating ValueObservation from Fetch Closures
|
||||
@@ -256,15 +164,6 @@ extension ValueObservation where Reducer == Never {
|
||||
/// print("Players have changed")
|
||||
/// }
|
||||
///
|
||||
/// The returned observation has the default configuration:
|
||||
///
|
||||
/// - When started with the `start(in:onError:onChange:)` method, a fresh
|
||||
/// value is immediately notified on the main queue.
|
||||
/// - Upon subsequent database changes, fresh values are notified on the
|
||||
/// main queue.
|
||||
/// - The observation lasts until the observer returned by
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - parameter value: A closure that fetches a value.
|
||||
public static func tracking<Value>(
|
||||
value: @escaping (Database) throws -> Value)
|
||||
@@ -273,9 +172,7 @@ extension ValueObservation where Reducer == Never {
|
||||
return ValueObservation<ValueReducers.Fetch<Value>>(
|
||||
baseRegion: { _ in DatabaseRegion() },
|
||||
observesSelectedRegion: true,
|
||||
makeReducer: { _ in ValueReducers.Fetch(value) },
|
||||
requiresWriteAccess: false,
|
||||
scheduling: .mainQueue)
|
||||
makeReducer: { _ in ValueReducers.Fetch(value) })
|
||||
}
|
||||
|
||||
/// Creates a ValueObservation which observes *regions*, and notifies the
|
||||
@@ -292,15 +189,6 @@ extension ValueObservation where Reducer == Never {
|
||||
/// print("Players have changed")
|
||||
/// }
|
||||
///
|
||||
/// The returned observation has the default configuration:
|
||||
///
|
||||
/// - When started with the `start(in:onError:onChange:)` method, a fresh
|
||||
/// value is immediately notified on the main queue.
|
||||
/// - Upon subsequent database changes, fresh values are notified on the
|
||||
/// main queue.
|
||||
/// - The observation lasts until the observer returned by
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - parameter regions: A list of observed regions.
|
||||
/// - parameter fetch: A closure that fetches a value.
|
||||
public static func tracking<Value>(
|
||||
@@ -325,15 +213,6 @@ extension ValueObservation where Reducer == Never {
|
||||
/// print("Players have changed")
|
||||
/// }
|
||||
///
|
||||
/// The returned observation has the default configuration:
|
||||
///
|
||||
/// - When started with the `start(in:onError:onChange:)` method, a fresh
|
||||
/// value is immediately notified on the main queue.
|
||||
/// - Upon subsequent database changes, fresh values are notified on the
|
||||
/// main queue.
|
||||
/// - The observation lasts until the observer returned by
|
||||
/// `start` is deallocated.
|
||||
///
|
||||
/// - parameter regions: A list of observed regions.
|
||||
/// - parameter fetch: A closure that fetches a value.
|
||||
public static func tracking<Value>(
|
||||
@@ -343,9 +222,6 @@ extension ValueObservation where Reducer == Never {
|
||||
{
|
||||
return ValueObservation<ValueReducers.Fetch<Value>>(
|
||||
baseRegion: DatabaseRegion.union(regions),
|
||||
observesSelectedRegion: false,
|
||||
makeReducer: { _ in ValueReducers.Fetch(fetch) },
|
||||
requiresWriteAccess: false,
|
||||
scheduling: .mainQueue)
|
||||
makeReducer: { _ in ValueReducers.Fetch(fetch) })
|
||||
}
|
||||
}
|
||||
@@ -21,18 +21,13 @@ class ValueObservationCompactMapTests: GRDBTestCase {
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
// The base reducer
|
||||
// Create an observation
|
||||
var count = 0
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ in /* don't fetch anything */ },
|
||||
value: { _ -> Int? in
|
||||
let observation = ValueObservation
|
||||
.tracking(DatabaseRegion.fullDatabase, fetch: { _ -> Int in
|
||||
count += 1
|
||||
return count
|
||||
})
|
||||
|
||||
// Create an observation
|
||||
let observation = ValueObservation
|
||||
.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
|
||||
})
|
||||
.compactMap { count -> String? in
|
||||
if count % 2 == 0 { return nil }
|
||||
return "\(count)"
|
||||
|
||||
@@ -22,13 +22,8 @@ class ValueObservationExtentTests: GRDBTestCase {
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
// A reducer
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ in },
|
||||
value: { $0 })
|
||||
|
||||
// Create an observation
|
||||
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
|
||||
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { _ in })
|
||||
|
||||
// Start observation and deallocate observer after second change
|
||||
var observer: TransactionObserver?
|
||||
|
||||
@@ -21,18 +21,13 @@ class ValueObservationMapTests: GRDBTestCase {
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 3
|
||||
|
||||
// The base reducer
|
||||
// Create an observation
|
||||
var count = 0
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ in /* don't fetch anything */ },
|
||||
value: { _ -> Int? in
|
||||
let observation = ValueObservation
|
||||
.tracking(DatabaseRegion.fullDatabase, fetch: { _ -> Int in
|
||||
count += 1
|
||||
return count
|
||||
})
|
||||
|
||||
// Create an observation
|
||||
let observation = ValueObservation
|
||||
.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
|
||||
})
|
||||
.map { count -> String in return "\(count)" }
|
||||
|
||||
// Start observation
|
||||
|
||||
@@ -1,26 +1,24 @@
|
||||
import XCTest
|
||||
import Dispatch
|
||||
#if GRDBCUSTOMSQLITE
|
||||
import GRDBCustomSQLite
|
||||
@testable import GRDBCustomSQLite
|
||||
#else
|
||||
#if SWIFT_PACKAGE
|
||||
import CSQLite
|
||||
#else
|
||||
import SQLite3
|
||||
#endif
|
||||
import GRDB
|
||||
@testable import GRDB
|
||||
#endif
|
||||
|
||||
class ValueObservationReducerTests: GRDBTestCase {
|
||||
func testRegionsAPI() {
|
||||
let reducer = AnyValueReducer(fetch: { _ in }, value: { })
|
||||
|
||||
// single region
|
||||
_ = ValueObservation.tracking(DatabaseRegion(), reducer: { _ in reducer })
|
||||
_ = ValueObservation.tracking(DatabaseRegion(), fetch: { _ in })
|
||||
// variadic
|
||||
_ = ValueObservation.tracking(DatabaseRegion(), DatabaseRegion(), reducer: { _ in reducer })
|
||||
_ = ValueObservation.tracking(DatabaseRegion(), DatabaseRegion(), fetch: { _ in })
|
||||
// array
|
||||
_ = ValueObservation.tracking([DatabaseRegion()], reducer: { _ in reducer })
|
||||
_ = ValueObservation.tracking([DatabaseRegion()], fetch: { _ in })
|
||||
}
|
||||
|
||||
func testReducer() throws {
|
||||
@@ -56,7 +54,7 @@ class ValueObservationReducerTests: GRDBTestCase {
|
||||
|
||||
// Create an observation
|
||||
let request = SQLRequest<Void>(sql: "SELECT * FROM t")
|
||||
let observation = ValueObservation.tracking(request, reducer: { _ in reducer })
|
||||
let observation = ValueObservation(baseRegion: request.databaseRegion, makeReducer: { _ in reducer })
|
||||
|
||||
// Start observation
|
||||
let observer = observation.start(
|
||||
@@ -124,13 +122,9 @@ class ValueObservationReducerTests: GRDBTestCase {
|
||||
|
||||
func testInitialErrorWithErrorHandling() throws {
|
||||
func test(_ dbWriter: DatabaseWriter) throws {
|
||||
struct TestError: Error { }
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ in throw TestError() },
|
||||
value: { _ in fatalError() })
|
||||
|
||||
// Create an observation
|
||||
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
|
||||
struct TestError: Error { }
|
||||
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { _ in throw TestError() })
|
||||
|
||||
// Start observation
|
||||
var error: TestError?
|
||||
@@ -157,20 +151,14 @@ class ValueObservationReducerTests: GRDBTestCase {
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 3
|
||||
|
||||
// A reducer which throws an error is requested to do so
|
||||
var nextError: Error? = nil // If not null, reducer throws an error
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ -> Void in
|
||||
if let error = nextError {
|
||||
nextError = nil
|
||||
throw error
|
||||
}
|
||||
},
|
||||
value: { $0 })
|
||||
|
||||
// Create an observation
|
||||
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
|
||||
|
||||
var nextError: Error? = nil // If not null, observation throws an error
|
||||
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { _ -> Void in
|
||||
if let error = nextError {
|
||||
nextError = nil
|
||||
throw error
|
||||
}
|
||||
})
|
||||
|
||||
// Start observation
|
||||
let observer = observation.start(
|
||||
in: dbWriter,
|
||||
@@ -216,12 +204,10 @@ class ValueObservationReducerTests: GRDBTestCase {
|
||||
|
||||
// Create an observation
|
||||
var count = 0
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ in /* don't fetch anything */ },
|
||||
value: { _ -> Int? in
|
||||
defer { count += 1 }
|
||||
return count })
|
||||
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer})
|
||||
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { _ -> Int in
|
||||
defer { count += 1 }
|
||||
return count
|
||||
})
|
||||
|
||||
// Start observation
|
||||
let observer = observation.start(
|
||||
@@ -245,109 +231,6 @@ class ValueObservationReducerTests: GRDBTestCase {
|
||||
try test(makeDatabasePool())
|
||||
}
|
||||
|
||||
func testMapValueReducer() throws {
|
||||
func test(_ dbWriter: DatabaseWriter) throws {
|
||||
// We need something to change
|
||||
try dbWriter.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }
|
||||
|
||||
// Track reducer process
|
||||
var counts: [String] = []
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 3
|
||||
|
||||
// The base reducer
|
||||
var count = 0
|
||||
let baseReducer = AnyValueReducer(
|
||||
fetch: { _ in /* don't fetch anything */ },
|
||||
value: { _ -> Int? in
|
||||
count += 1
|
||||
return count
|
||||
})
|
||||
|
||||
// The mapped reducer
|
||||
let reducer = baseReducer.map { count -> String in
|
||||
return "\(count)"
|
||||
}
|
||||
|
||||
// Create an observation
|
||||
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
|
||||
|
||||
// Start observation
|
||||
let observer = observation.start(
|
||||
in: dbWriter,
|
||||
onError: { error in XCTFail("Unexpected error: \(error)") },
|
||||
onChange: { count in
|
||||
counts.append(count)
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
try withExtendedLifetime(observer) {
|
||||
try dbWriter.writeWithoutTransaction { db in
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(counts, ["1", "2", "3"])
|
||||
}
|
||||
}
|
||||
|
||||
try test(makeDatabaseQueue())
|
||||
try test(makeDatabasePool())
|
||||
}
|
||||
|
||||
func testCompactMapValueReducer() throws {
|
||||
func test(_ dbWriter: DatabaseWriter) throws {
|
||||
// We need something to change
|
||||
try dbWriter.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }
|
||||
|
||||
// Track reducer process
|
||||
var counts: [String] = []
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
// The base reducer
|
||||
var count = 0
|
||||
let baseReducer = AnyValueReducer(
|
||||
fetch: { _ in /* don't fetch anything */ },
|
||||
value: { _ -> Int? in
|
||||
count += 1
|
||||
return count
|
||||
})
|
||||
|
||||
// The mapped reducer
|
||||
let reducer = baseReducer.compactMap { count -> String? in
|
||||
if count % 2 == 0 { return nil }
|
||||
return "\(count)"
|
||||
}
|
||||
|
||||
// Create an observation
|
||||
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
|
||||
|
||||
// Start observation
|
||||
let observer = observation.start(
|
||||
in: dbWriter,
|
||||
onError: { error in XCTFail("Unexpected error: \(error)") },
|
||||
onChange: { count in
|
||||
counts.append(count)
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
try withExtendedLifetime(observer) {
|
||||
try dbWriter.writeWithoutTransaction { db in
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(counts, ["1", "3"])
|
||||
}
|
||||
}
|
||||
|
||||
try test(makeDatabaseQueue())
|
||||
try test(makeDatabasePool())
|
||||
}
|
||||
|
||||
func testValueObservationMap() throws {
|
||||
func test(_ dbWriter: DatabaseWriter) throws {
|
||||
try dbWriter.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }
|
||||
@@ -398,7 +281,9 @@ class ValueObservationReducerTests: GRDBTestCase {
|
||||
}
|
||||
reduceExpectation.fulfill()
|
||||
})
|
||||
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
|
||||
let observation = ValueObservation(
|
||||
baseRegion: { _ in DatabaseRegion.fullDatabase },
|
||||
makeReducer: { _ in reducer })
|
||||
let observer = observation.start(
|
||||
in: dbWriter,
|
||||
onError: { error in XCTFail("Unexpected error: \(error)") },
|
||||
@@ -451,15 +336,17 @@ class ValueObservationReducerTests: GRDBTestCase {
|
||||
var observer: TransactionObserver? = nil
|
||||
_ = observer // Avoid "Variable 'observer' was written to, but never read" warning
|
||||
var shouldStopObservation = false
|
||||
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in
|
||||
AnyValueReducer<Void, Void>(
|
||||
fetch: { _ in
|
||||
if shouldStopObservation {
|
||||
observer = nil /* deallocation */
|
||||
}
|
||||
shouldStopObservation = true
|
||||
},
|
||||
value: { _ in () })
|
||||
var observation = ValueObservation(
|
||||
baseRegion: { _ in DatabaseRegion.fullDatabase },
|
||||
makeReducer: { _ in
|
||||
AnyValueReducer<Void, Void>(
|
||||
fetch: { _ in
|
||||
if shouldStopObservation {
|
||||
observer = nil /* deallocation */
|
||||
}
|
||||
shouldStopObservation = true
|
||||
},
|
||||
value: { _ in () })
|
||||
})
|
||||
observation.scheduling = .unsafe
|
||||
observer = observation.start(
|
||||
@@ -493,16 +380,18 @@ class ValueObservationReducerTests: GRDBTestCase {
|
||||
var observer: TransactionObserver? = nil
|
||||
_ = observer // Avoid "Variable 'observer' was written to, but never read" warning
|
||||
var shouldStopObservation = false
|
||||
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in
|
||||
AnyValueReducer<Void, Void>(
|
||||
fetch: { _ in },
|
||||
value: { _ in
|
||||
if shouldStopObservation {
|
||||
observer = nil /* deallocation right before notification */
|
||||
}
|
||||
shouldStopObservation = true
|
||||
return ()
|
||||
})
|
||||
var observation = ValueObservation(
|
||||
baseRegion: { _ in DatabaseRegion.fullDatabase },
|
||||
makeReducer: { _ in
|
||||
AnyValueReducer<Void, Void>(
|
||||
fetch: { _ in },
|
||||
value: { _ in
|
||||
if shouldStopObservation {
|
||||
observer = nil /* deallocation right before notification */
|
||||
}
|
||||
shouldStopObservation = true
|
||||
return ()
|
||||
})
|
||||
})
|
||||
observation.scheduling = .unsafe
|
||||
observer = observation.start(
|
||||
|
||||
@@ -107,16 +107,13 @@ class ValueObservationSchedulingTests: GRDBTestCase {
|
||||
let key = DispatchSpecificKey<()>()
|
||||
DispatchQueue.main.setSpecific(key: key, value: ())
|
||||
|
||||
var nextError: Error? = nil // If not null, reducer throws an error
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ -> Void in
|
||||
if let error = nextError {
|
||||
nextError = nil
|
||||
throw error
|
||||
}
|
||||
},
|
||||
value: { $0 })
|
||||
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
|
||||
var nextError: Error? = nil // If not null, observation throws an error
|
||||
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { _ -> Void in
|
||||
if let error = nextError {
|
||||
nextError = nil
|
||||
throw error
|
||||
}
|
||||
})
|
||||
|
||||
let observer = observation.start(
|
||||
in: dbWriter,
|
||||
@@ -201,10 +198,7 @@ class ValueObservationSchedulingTests: GRDBTestCase {
|
||||
queue.setSpecific(key: key, value: ())
|
||||
|
||||
struct TestError: Error { }
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ in throw TestError() },
|
||||
value: { $0 })
|
||||
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
|
||||
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { _ in throw TestError() })
|
||||
observation.scheduling = .async(onQueue: queue)
|
||||
|
||||
let observer = observation.start(
|
||||
@@ -241,15 +235,12 @@ class ValueObservationSchedulingTests: GRDBTestCase {
|
||||
|
||||
struct TestError: Error { }
|
||||
var shouldThrow = false
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ in
|
||||
if shouldThrow {
|
||||
throw TestError()
|
||||
}
|
||||
shouldThrow = true
|
||||
},
|
||||
value: { $0 })
|
||||
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
|
||||
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { _ in
|
||||
if shouldThrow {
|
||||
throw TestError()
|
||||
}
|
||||
shouldThrow = true
|
||||
})
|
||||
observation.scheduling = .async(onQueue: queue)
|
||||
|
||||
let observer = observation.start(
|
||||
@@ -374,10 +365,7 @@ class ValueObservationSchedulingTests: GRDBTestCase {
|
||||
notificationExpectation.expectedFulfillmentCount = 1
|
||||
|
||||
struct TestError: Error { }
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ in throw TestError() },
|
||||
value: { $0 })
|
||||
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
|
||||
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { _ in throw TestError() })
|
||||
observation.scheduling = .unsafe
|
||||
|
||||
let observer = observation.start(
|
||||
@@ -409,15 +397,12 @@ class ValueObservationSchedulingTests: GRDBTestCase {
|
||||
|
||||
struct TestError: Error { }
|
||||
var shouldThrow = false
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ in
|
||||
if shouldThrow {
|
||||
throw TestError()
|
||||
}
|
||||
shouldThrow = true
|
||||
},
|
||||
value: { $0 })
|
||||
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
|
||||
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { _ in
|
||||
if shouldThrow {
|
||||
throw TestError()
|
||||
}
|
||||
shouldThrow = true
|
||||
})
|
||||
observation.scheduling = .unsafe
|
||||
|
||||
let observer = observation.start(
|
||||
|
||||
Reference in new issue
Block a user