inamiy / RxProperty

A get-only `BehaviorRelay ` that is (almost) equivalent to ReactiveSwift's `Property`
MIT License
85 stars 8 forks source link

Fix `Property.init(unsafeObservable:)` bug that doesn't emit value when `property` is deallocated #6

Closed inamiy closed 5 years ago

inamiy commented 5 years ago

Fixes #4 .

This PR fixes Property.init(unsafeObservable:) bug that doesn't emit value when property is deallocated.

Test Case example

 func test_initWithUnsafe_asObservable() {

     let relay = BehaviorRelay<Int>(value: 0)
     var property: Property<Int>! = .init(unsafeObservable: relay.asObservable())

     var events = [Event<Int>]()

     // `.asObservable()` test
     _ = property.asObservable().subscribe { event in
         events.append(event)
     }

     XCTAssertEqual(events, [.next(0)],
                    "should observe initial value")

     relay.accept(1)
     XCTAssertEqual(events, [.next(0), .next(1)])

     relay.accept(2)
     XCTAssertEqual(events, [.next(0), .next(1), .next(2)])

     property = nil
     XCTAssertEqual(events, [.next(0), .next(1), .next(2)],
                    "`.completed` should NOT be observed when `property` is deallocated")

     relay.accept(3)
-    XCTAssertEqual(events, [.next(0), .next(1), .next(2)],
-                   "BUG: `.next(3)` should be received even when `property` is deallocated.")
+    XCTAssertEqual(events, [.next(0), .next(1), .next(2), .next(3)],
+                   "`property`'s observable should still be alive even when `property` is deallocated.")

 }