Rename handleEvents arguments

This commit is contained in:
Gwendal Roué committed 2020-06-22 14:23:33 +02:00
1 parent 54adc18988
commit 4e8b231c42
10 files changed
+149 -149

No files matched your search

+2 -2
View File
@@ -898,7 +898,7 @@ extension DatabasePool: DatabaseReader {
add(observer: observer, from: initialSnapshot)
} catch {
observer.complete()
observation.events.onError?(error)
observation.events.didFail?(error)
}
} else {
let label = configuration.identifier(
@@ -957,7 +957,7 @@ extension DatabasePool: DatabaseReader {
}
if fetchNeeded {
observer.events.onDatabaseChange?()
observer.events.databaseDidChange?()
if let value = try observer.fetchValue(db) {
observer.notifyChange(value)
}
+2 -2
View File
@@ -305,7 +305,7 @@ extension DatabaseReader {
let value = try unsafeReentrantRead(observation.fetchValue)
onChange(value)
} catch {
observation.events.onError?(error)
observation.events.didFail?(error)
}
return AnyDatabaseCancellable(cancel: { })
} else {
@@ -325,7 +325,7 @@ extension DatabaseReader {
do {
try onChange(result.get())
} catch {
observation.events.onError?(error)
observation.events.didFail?(error)
}
}
}
+1 -1
View File
@@ -337,7 +337,7 @@ extension DatabaseWriter {
onChange(initialValue)
} catch {
observer.complete()
observation.events.onError?(error)
observation.events.didFail?(error)
}
} else {
_weakAsyncWriteWithoutTransaction { db in
+38 -38
View File
@@ -41,11 +41,11 @@ public struct ValueObservation<Reducer: _ValueReducer> {
}
struct ValueObservationEvents: Refinable {
var onStart: (() -> Void)?
var onTrackedRegion: ((DatabaseRegion) -> Void)?
var onDatabaseChange: (() -> Void)?
var onError: ((Error) -> Void)?
var onCancel: (() -> Void)?
var willStart: (() -> Void)?
var willTrackRegion: ((DatabaseRegion) -> Void)?
var databaseDidChange: (() -> Void)?
var didFail: ((Error) -> Void)?
var didCancel: (() -> Void)?
}
extension ValueObservation: Refinable {
@@ -100,9 +100,9 @@ extension ValueObservation: Refinable {
onChange: @escaping (Reducer.Value) -> Void) -> DatabaseCancellable
{
let observation = map(\.events) { events in
events.map(\.onError) { concat($0, onError) }
events.map(\.didFail) { concat($0, onError) }
}
observation.events.onStart?()
observation.events.willStart?()
return reader._add(
observation: observation,
scheduling: scheduler,
@@ -114,52 +114,52 @@ extension ValueObservation: Refinable {
/// Performs the specified closures when ValueObservation events occur.
///
/// - parameters:
/// - onStart: A closure that executes when the observation starts.
/// - willStart: A closure that executes when the observation starts.
/// Defaults to `nil`.
/// - onTrackedRegion: A closure that executes when the observation
/// - willFetch: A closure that executes when the observed value is
/// about to be fetched. Defaults to `nil`.
/// - willTrackRegion: A closure that executes when the observation
/// starts tracking a database region. Defaults to `nil`.
/// - onDatabaseChange: A closure that executes after the observation
/// - databaseDidChange: A closure that executes after the observation
/// was impacted by a database change. Defaults to `nil`.
/// - onFetch: A closure that executes when the observed value is
/// fetched. Defaults to `nil`.
/// - onValue: A closure that executes on fresh values. Defaults
/// - didReceiveValue: A closure that executes on fresh values. Defaults
/// to `nil`.
///
/// NOTE: This closure runs on an unspecified DispatchQueue.
/// - onError: A closure that executes when the observation fails.
/// - didFail: A closure that executes when the observation fails.
/// Defaults to `nil`.
/// - onCancel: A closure that executes when the observation is
/// - didCancel: A closure that executes when the observation is
/// cancelled. Defaults to `nil`.
/// - returns: A `ValueObservation` that performs the specified closures
/// when ValueObservation events occur.
public func handleEvents(
onStart: (() -> Void)? = nil,
onTrackedRegion: ((DatabaseRegion) -> Void)? = nil,
onDatabaseChange: (() -> Void)? = nil,
onFetch: (() -> Void)? = nil,
onValue: ((Reducer.Value) -> Void)? = nil,
onError: ((Error) -> Void)? = nil,
onCancel: (() -> Void)? = nil)
willStart: (() -> Void)? = nil,
willFetch: (() -> Void)? = nil,
willTrackRegion: ((DatabaseRegion) -> Void)? = nil,
databaseDidChange: (() -> Void)? = nil,
didReceiveValue: ((Reducer.Value) -> Void)? = nil,
didFail: ((Error) -> Void)? = nil,
didCancel: (() -> Void)? = nil)
-> ValueObservation<ValueReducers.Trace<Reducer>>
{
self
.mapReducer({ reducer in
ValueReducers.Trace(
base: reducer,
// Adding the onFetch handler to the reducer is handy: we
// Adding the willFetch handler to the reducer is handy: we
// are sure not to miss any fetch.
onFetch: onFetch ?? { },
// Adding the onValue handler to the reducer is necessary:
willFetch: willFetch ?? { },
// Adding the didReceiveValue handler to the reducer is necessary:
// the type of the value may change with the `map` operator.
onValue: onValue ?? { _ in })
didReceiveValue: didReceiveValue ?? { _ in })
})
.map(\.events, { events in
events
.map(\.onStart) { concat($0, onStart) }
.map(\.onTrackedRegion) { concat($0, onTrackedRegion) }
.map(\.onDatabaseChange) { concat($0, onDatabaseChange) }
.map(\.onError) { concat($0, onError) }
.map(\.onCancel) { concat($0, onCancel) }
.map(\.willStart) { concat($0, willStart) }
.map(\.willTrackRegion) { concat($0, willTrackRegion) }
.map(\.databaseDidChange) { concat($0, databaseDidChange) }
.map(\.didFail) { concat($0, didFail) }
.map(\.didCancel) { concat($0, didCancel) }
})
}
@@ -172,13 +172,13 @@ extension ValueObservation: Refinable {
let prefix = prefix.isEmpty ? "" : "\(prefix): "
var stream = stream ?? PrintOutputStream()
return handleEvents(
onStart: { stream.write("\(prefix)start") },
onTrackedRegion: { stream.write("\(prefix)tracked region: \($0)") },
onDatabaseChange: { stream.write("\(prefix)database did change") },
onFetch: { stream.write("\(prefix)fetch") },
onValue: { stream.write("\(prefix)value: \($0)") },
onError: { stream.write("\(prefix)error: \($0)") },
onCancel: { stream.write("\(prefix)cancel") })
willStart: { stream.write("\(prefix)will start") },
willFetch: { stream.write("\(prefix)will fetch") },
willTrackRegion: { stream.write("\(prefix)will track region: \($0)") },
databaseDidChange: { stream.write("\(prefix)database did change") },
didReceiveValue: { stream.write("\(prefix)did receive value: \($0)") },
didFail: { stream.write("\(prefix)did fail: \($0)") },
didCancel: { stream.write("\(prefix)did cancel") })
}
// MARK: - Fetching Values
+5 -5
View File
@@ -8,11 +8,11 @@ final class ValueObserver<Reducer: _ValueReducer> {
private var observedRegion: DatabaseRegion? {
didSet {
if
let onTrackedRegion = events.onTrackedRegion,
let willTrackRegion = events.willTrackRegion,
let region = observedRegion,
region != oldValue
{
onTrackedRegion(region)
willTrackRegion(region)
}
}
}
@@ -87,7 +87,7 @@ extension ValueObserver: TransactionObserver {
isChanged = false
if isCompleted { return }
events.onDatabaseChange?()
events.databaseDidChange?()
// 1. Fetch
let fetchedValue: DatabaseFuture<Reducer.Fetched>
@@ -158,8 +158,8 @@ extension ValueObserver {
func cancel() {
synchronized {
if _isCompleted { return }
events.onCancel?()
complete()
events.didCancel?()
}
}
@@ -183,7 +183,7 @@ extension ValueObserver {
}
}
if shouldNotify {
self.events.onError?(error)
self.events.didFail?(error)
}
}
}
@@ -4,12 +4,12 @@ extension ValueReducers {
/// :nodoc:
public struct Trace<Base: _ValueReducer>: _ValueReducer {
var base: Base
let onFetch: () -> Void
let onValue: (Base.Value) -> Void
let willFetch: () -> Void
let didReceiveValue: (Base.Value) -> Void
public var isSelectedRegionDeterministic: Bool { base.isSelectedRegionDeterministic }
public func fetch(_ db: Database) throws -> Base.Fetched {
onFetch()
willFetch()
return try base.fetch(db)
}
@@ -17,7 +17,7 @@ extension ValueReducers {
guard let value = base.value(fetched) else {
return nil
}
onValue(value)
didReceiveValue(value)
return value
}
}
+12 -12
View File
@@ -5580,27 +5580,27 @@ The `handleEvents` operator lets your application observe the lifetime of a Valu
let observation = ValueObservation
.tracking { db in ... }
.handleEvents(
onStart: {
// The observation starts
willStart: {
// The observation starts.
},
onTrackedRegion: { databaseRegion in
// The observation starts tracking a database region
willFetch: {
// The observation will perform a database fetch.
},
onDatabaseChange: {
willTrackRegion: { databaseRegion in
// The observation starts tracking a database region.
},
databaseDidChange: {
// The observation was impacted by a database change.
},
onFetch: {
// The observation performs a database fetch.
},
onValue: { value in
didReceiveValue: { value in
// A fresh value was observed.
// NOTE: This closure runs on an unspecified DispatchQueue.
},
onError: { error in
didFail: { error in
// The observation completes with an error.
},
onCancel: {
// The observation is cancelled.
didCancel: {
// The observation was cancelled.
})
```
@@ -43,9 +43,9 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger.strings, [
"start",
"fetch",
"value: nil"])
"will start",
"will fetch",
"did receive value: nil"])
}
}
@@ -77,9 +77,9 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger.strings, [
"start",
"fetch",
"value: nil"])
"will start",
"will fetch",
"did receive value: nil"])
}
}
@@ -109,9 +109,9 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger.strings, [
"start",
"fetch",
"error: TestError()"])
"will start",
"will fetch",
"did fail: TestError()"])
}
}
@@ -141,9 +141,9 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger.strings, [
"start",
"fetch",
"error: TestError()"])
"will start",
"will fetch",
"did fail: TestError()"])
}
}
@@ -184,13 +184,13 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger.strings.prefix(7), [
"start",
"fetch",
"value: nil",
"tracked region: \(expectedRegion)",
"will start",
"will fetch",
"did receive value: nil",
"will track region: \(expectedRegion)",
"database did change",
"fetch",
"value: Optional(1)"])
"will fetch",
"did receive value: Optional(1)"])
}
}
@@ -226,13 +226,13 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger.strings.prefix(7), [
"start",
"fetch",
"value: nil",
"tracked region: \(expectedRegion)",
"will start",
"will fetch",
"did receive value: nil",
"will track region: \(expectedRegion)",
"database did change",
"fetch",
"value: Optional(1)"])
"will fetch",
"did receive value: Optional(1)"])
}
}
@@ -257,9 +257,9 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger.strings, [
"start",
"fetch",
"error: SQLite error 1 with statement `SELECT MAX(id) FROM player`: no such table: player"])
"will start",
"will fetch",
"did fail: SQLite error 1 with statement `SELECT MAX(id) FROM player`: no such table: player"])
}
}
@@ -284,9 +284,9 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger.strings, [
"start",
"fetch",
"error: SQLite error 1 with statement `SELECT MAX(id) FROM player`: no such table: player"])
"will start",
"will fetch",
"did fail: SQLite error 1 with statement `SELECT MAX(id) FROM player`: no such table: player"])
}
}
@@ -323,13 +323,13 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger.strings, [
"start",
"fetch",
"value: nil",
"tracked region: \(expectedRegion)",
"will start",
"will fetch",
"did receive value: nil",
"will track region: \(expectedRegion)",
"database did change",
"fetch",
"error: SQLite error 1 with statement `SELECT MAX(id) FROM player`: no such table: player"])
"will fetch",
"did fail: SQLite error 1 with statement `SELECT MAX(id) FROM player`: no such table: player"])
}
}
@@ -366,13 +366,13 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger.strings, [
"start",
"fetch",
"value: nil",
"tracked region: \(expectedRegion)",
"will start",
"will fetch",
"did receive value: nil",
"will track region: \(expectedRegion)",
"database did change",
"fetch",
"error: SQLite error 1 with statement `SELECT MAX(id) FROM player`: no such table: player"])
"will fetch",
"did fail: SQLite error 1 with statement `SELECT MAX(id) FROM player`: no such table: player"])
}
}
@@ -419,13 +419,13 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger.strings, [
"start",
"fetch",
"value: nil",
"tracked region: \(expectedRegion)",
"will start",
"will fetch",
"did receive value: nil",
"will track region: \(expectedRegion)",
"database did change",
"fetch",
"value: nil"])
"will fetch",
"did receive value: nil"])
}
}
@@ -466,13 +466,13 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger.strings, [
"start",
"fetch",
"value: nil",
"tracked region: \(expectedRegion)",
"will start",
"will fetch",
"did receive value: nil",
"will track region: \(expectedRegion)",
"database did change",
"fetch",
"value: nil"])
"will fetch",
"did receive value: nil"])
}
}
@@ -518,17 +518,17 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger.strings.prefix(11), [
"start",
"fetch",
"value: nil",
"tracked region: \(expectedRegionA),choice(t)",
"will start",
"will fetch",
"did receive value: nil",
"will track region: \(expectedRegionA),choice(t)",
"database did change",
"fetch",
"tracked region: \(expectedRegionB),choice(t)",
"value: Optional(1)",
"will fetch",
"will track region: \(expectedRegionB),choice(t)",
"did receive value: Optional(1)",
"database did change",
"fetch",
"value: Optional(2)"])
"will fetch",
"did receive value: Optional(2)"])
}
}
@@ -556,13 +556,13 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger1.strings.prefix(3), [
"start",
"fetch",
"value: nil"])
"will start",
"will fetch",
"did receive value: nil"])
XCTAssertEqual(logger2.strings.prefix(3), [
"log: start",
"log: fetch",
"log: value: nil"])
"log: will start",
"log: will fetch",
"log: did receive value: nil"])
}
}
@@ -590,13 +590,13 @@ class ValueObservationPrintTests: GRDBTestCase {
withExtendedLifetime(cancellable) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(logger1.strings.prefix(3), [
"start",
"fetch",
"value: nil"])
"will start",
"will fetch",
"did receive value: nil"])
XCTAssertEqual(logger2.strings.prefix(3), [
"start",
"fetch",
"value: foo"])
"will start",
"will fetch",
"did receive value: foo"])
}
}
@@ -674,37 +674,37 @@ class ValueObservationPrintTests: GRDBTestCase {
do {
let logger = TestStream()
try waitFor(observation.handleEvents(onStart: { logger.write("start") }))
try waitFor(observation.handleEvents(willStart: { logger.write("start") }))
XCTAssertEqual(logger.strings, ["start"])
}
do {
let logger = TestStream()
try waitFor(observation.handleEvents(onTrackedRegion: { _ in logger.write("region") }))
try waitFor(observation.handleEvents(willTrackRegion: { _ in logger.write("region") }))
XCTAssertEqual(logger.strings, ["region"])
}
do {
let logger = TestStream()
try waitFor(observation.handleEvents(onDatabaseChange: { logger.write("change") }))
try waitFor(observation.handleEvents(databaseDidChange: { logger.write("change") }))
XCTAssertEqual(logger.strings.prefix(1), ["change"])
}
do {
let logger = TestStream()
try waitFor(observation.handleEvents(onFetch: { logger.write("fetch") }))
try waitFor(observation.handleEvents(willFetch: { logger.write("fetch") }))
XCTAssertEqual(logger.strings.prefix(2), ["fetch", "fetch"])
}
do {
let logger = TestStream()
try waitFor(observation.handleEvents(onValue: { _ in logger.write("value") }))
try waitFor(observation.handleEvents(didReceiveValue: { _ in logger.write("value") }))
XCTAssertEqual(logger.strings.prefix(2), ["value", "value"])
}
do {
let logger = TestStream()
try waitForError(observation.handleEvents(onError: { _ in logger.write("error") }))
try waitForError(observation.handleEvents(didFail: { _ in logger.write("error") }))
XCTAssertEqual(logger.strings, ["error"])
}
do {
let logger = TestStream()
try waitForCancel(observation.handleEvents(onCancel: { logger.write("cancel") }))
try waitForCancel(observation.handleEvents(didCancel: { logger.write("cancel") }))
XCTAssertEqual(logger.strings, ["cancel"])
}
}
@@ -141,7 +141,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
let table = try String.fetchOne(db, sql: "SELECT name FROM source")!
return try Int.fetchOne(db, sql: "SELECT IFNULL(SUM(value), 0) FROM \(table)")!
})
.handleEvents(onTrackedRegion: { regions.append($0) })
.handleEvents(willTrackRegion: { regions.append($0) })
let observer = observation.start(
in: dbQueue,
@@ -192,7 +192,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
let table = try String.fetchOne(db, sql: "SELECT name FROM source")!
return try Int.fetchOne(db, sql: "SELECT IFNULL(SUM(value), 0) FROM \(table)")!
})
.handleEvents(onTrackedRegion: { regions.append($0) })
.handleEvents(willTrackRegion: { regions.append($0) })
let observer = observation.start(
in: dbQueue,
+1 -1
View File
@@ -94,7 +94,7 @@ class ValueObservationTests: GRDBTestCase {
let expectation = self.expectation(description: "")
let observation = ValueObservation
.tracking(request.fetchAll)
.handleEvents(onTrackedRegion: {
.handleEvents(willTrackRegion: {
region = $0
expectation.fulfill()
})