Skip to content

Commit

Permalink
Merge pull request #1 from mruston-heb/properly-cancel-materialize
Browse files Browse the repository at this point in the history
Properly cancel materialize
  • Loading branch information
hebdavepaul0 authored Mar 21, 2022
2 parents 603a3fb + 55d66b6 commit 3d9b139
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/Operators/Materialize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private extension Publishers.Materialize {
}

func cancel() {
sink?.cancelUpstream()
sink = nil
}
}
Expand Down
23 changes: 23 additions & 0 deletions Tests/MaterializeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,28 @@ class MaterializeTests: XCTestCase {
XCTAssertEqual(errors, [.someError])
XCTAssertTrue(completed)
}

/// Test that when a stream is cancelled, the cancel is propagated upstream
func testCancelled() {
let subject = PassthroughSubject<String, MyError>()
var valueCount = 0
var cancelCount = 0

subscription = subject
.handleEvents(receiveCancel: {
cancelCount += 1
})
.materialize()
.failures()
.sink(receiveCompletion: { _ in },
receiveValue: { _ in valueCount += 1 }
)

subscription?.cancel()
subject.send("Hello")

XCTAssertEqual(valueCount, 0, "0 values should be emitted after cancel")
XCTAssertEqual(cancelCount, 1, "Cancel is reported upstream")
}
}
#endif

0 comments on commit 3d9b139

Please sign in to comment.