From 1d0a8713a17145e7df64beafb1343222d90bd08b Mon Sep 17 00:00:00 2001 From: ZiZi <44972592+ZiZasaurus@users.noreply.github.com> Date: Wed, 20 Apr 2022 13:35:07 -0400 Subject: [PATCH] Resolve issue where location value is declared after observer is notified (#1287) * declaring value before observer is notified and adding tests --- Sources/MapboxMaps/Foundation/ObservableValue.swift | 2 +- .../Viewport/Helpers/ObservableValueTests.swift | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Sources/MapboxMaps/Foundation/ObservableValue.swift b/Sources/MapboxMaps/Foundation/ObservableValue.swift index 7131ac89f975..a62274111267 100644 --- a/Sources/MapboxMaps/Foundation/ObservableValue.swift +++ b/Sources/MapboxMaps/Foundation/ObservableValue.swift @@ -15,10 +15,10 @@ internal final class ObservableValue where Value: Equatable { guard newValue != value else { return } + value = newValue observers.forEach { (observer) in observer.invokeHandler(with: newValue) } - value = newValue } internal func observe(with handler: @escaping (Value) -> Bool) -> Cancelable { diff --git a/Tests/MapboxMapsTests/Viewport/Helpers/ObservableValueTests.swift b/Tests/MapboxMapsTests/Viewport/Helpers/ObservableValueTests.swift index 4b2e988276e0..73b80a443270 100644 --- a/Tests/MapboxMapsTests/Viewport/Helpers/ObservableValueTests.swift +++ b/Tests/MapboxMapsTests/Viewport/Helpers/ObservableValueTests.swift @@ -56,6 +56,16 @@ final class ObservableValueTests: XCTestCase { XCTAssertEqual(handlerStub.invocations.map(\.parameters), [value]) } + func testValueUpdatedBeforeNotifyingObservers() { + let handlerStub = Stub(defaultReturnValue: true) + handlerStub.defaultSideEffect = { invocation in + XCTAssertEqual(self.observableValue.value, invocation.parameters) + } + _ = observableValue.observe(with: handlerStub.call(with:)) + + update() + } + func testHandlerReturnsTrueToContinueAndFalseToUnsubscribe() { let handlerStub = Stub(defaultReturnValue: false) handlerStub.returnValueQueue = [true, true]