Skip to content

Commit

Permalink
Resolve issue where location value is declared after observer is noti…
Browse files Browse the repository at this point in the history
…fied (#1287)

* declaring value before observer is notified and adding tests
  • Loading branch information
ZiZasaurus authored Apr 20, 2022
1 parent 8e928e8 commit 1d0a871
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/MapboxMaps/Foundation/ObservableValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ internal final class ObservableValue<Value> 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 {
Expand Down
10 changes: 10 additions & 0 deletions Tests/MapboxMapsTests/Viewport/Helpers/ObservableValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ final class ObservableValueTests: XCTestCase {
XCTAssertEqual(handlerStub.invocations.map(\.parameters), [value])
}

func testValueUpdatedBeforeNotifyingObservers() {
let handlerStub = Stub<Int, Bool>(defaultReturnValue: true)
handlerStub.defaultSideEffect = { invocation in
XCTAssertEqual(self.observableValue.value, invocation.parameters)
}
_ = observableValue.observe(with: handlerStub.call(with:))

update()
}

func testHandlerReturnsTrueToContinueAndFalseToUnsubscribe() {
let handlerStub = Stub<Int, Bool>(defaultReturnValue: false)
handlerStub.returnValueQueue = [true, true]
Expand Down

0 comments on commit 1d0a871

Please sign in to comment.