From 4e8b231c42bace6ce0d637de996241a24da0aa69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 22 Jun 2020 14:23:33 +0200 Subject: [PATCH] Rename handleEvents arguments --- GRDB/Core/DatabasePool.swift | 4 +- GRDB/Core/DatabaseReader.swift | 4 +- GRDB/Core/DatabaseWriter.swift | 2 +- GRDB/ValueObservation/ValueObservation.swift | 76 ++++---- GRDB/ValueObservation/ValueObserver.swift | 10 +- .../ValueObservation/ValueReducer/Trace.swift | 8 +- README.md | 24 +-- .../ValueObservationPrintTests.swift | 164 +++++++++--------- ...ValueObservationRegionRecordingTests.swift | 4 +- Tests/GRDBTests/ValueObservationTests.swift | 2 +- 10 files changed, 149 insertions(+), 149 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index c87f68af4..8cace9c57 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -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) } diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index 5311ed1ac..845d79f30 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -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) } } } diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index de32fdef9..6ad451b4b 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -337,7 +337,7 @@ extension DatabaseWriter { onChange(initialValue) } catch { observer.complete() - observation.events.onError?(error) + observation.events.didFail?(error) } } else { _weakAsyncWriteWithoutTransaction { db in diff --git a/GRDB/ValueObservation/ValueObservation.swift b/GRDB/ValueObservation/ValueObservation.swift index ad154f74d..0dff83c2f 100644 --- a/GRDB/ValueObservation/ValueObservation.swift +++ b/GRDB/ValueObservation/ValueObservation.swift @@ -41,11 +41,11 @@ public struct ValueObservation { } 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> { 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 diff --git a/GRDB/ValueObservation/ValueObserver.swift b/GRDB/ValueObservation/ValueObserver.swift index f5169a759..5eb3320f0 100644 --- a/GRDB/ValueObservation/ValueObserver.swift +++ b/GRDB/ValueObservation/ValueObserver.swift @@ -8,11 +8,11 @@ final class ValueObserver { 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 @@ -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) } } } diff --git a/GRDB/ValueObservation/ValueReducer/Trace.swift b/GRDB/ValueObservation/ValueReducer/Trace.swift index 80e749b37..8628f2e5d 100644 --- a/GRDB/ValueObservation/ValueReducer/Trace.swift +++ b/GRDB/ValueObservation/ValueReducer/Trace.swift @@ -4,12 +4,12 @@ extension ValueReducers { /// :nodoc: public struct Trace: _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 } } diff --git a/README.md b/README.md index 67507ccbd..5d336b464 100644 --- a/README.md +++ b/README.md @@ -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. }) ``` diff --git a/Tests/GRDBTests/ValueObservationPrintTests.swift b/Tests/GRDBTests/ValueObservationPrintTests.swift index bd221d842..87fe78c3e 100644 --- a/Tests/GRDBTests/ValueObservationPrintTests.swift +++ b/Tests/GRDBTests/ValueObservationPrintTests.swift @@ -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"]) } } diff --git a/Tests/GRDBTests/ValueObservationRegionRecordingTests.swift b/Tests/GRDBTests/ValueObservationRegionRecordingTests.swift index 6593a82f2..e18414a8f 100644 --- a/Tests/GRDBTests/ValueObservationRegionRecordingTests.swift +++ b/Tests/GRDBTests/ValueObservationRegionRecordingTests.swift @@ -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, diff --git a/Tests/GRDBTests/ValueObservationTests.swift b/Tests/GRDBTests/ValueObservationTests.swift index 0b75a7695..a3da023a5 100644 --- a/Tests/GRDBTests/ValueObservationTests.swift +++ b/Tests/GRDBTests/ValueObservationTests.swift @@ -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() })