Improves current thread scheduler and solves variable confusing behavior on dealloc.
This commit is contained in:
parent
54ed3af9cb
commit
6bcab4269c
|
|
@ -46,7 +46,13 @@ public class CurrentThreadScheduler : ImmediateSchedulerType {
|
|||
return NSThread.currentThread().threadDictionary[CurrentThreadSchedulerKeyInstance] as? ScheduleQueue
|
||||
}
|
||||
set {
|
||||
NSThread.currentThread().threadDictionary[CurrentThreadSchedulerKeyInstance] = newValue
|
||||
let threadDictionary = NSThread.currentThread().threadDictionary
|
||||
if let newValue = newValue {
|
||||
threadDictionary[CurrentThreadSchedulerKeyInstance] = newValue
|
||||
}
|
||||
else {
|
||||
threadDictionary.removeObjectForKey(CurrentThreadSchedulerKeyInstance)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,8 +12,6 @@ import Foundation
|
|||
Variable is a wrapper for `BehaviorSubject`.
|
||||
|
||||
Unlike `BehaviorSubject` it can't terminate with error.
|
||||
|
||||
Before variable is deallocated, `Completed` event will be sent to all observers.
|
||||
*/
|
||||
public class Variable<Element> : ObservableType {
|
||||
public typealias E = Element
|
||||
|
|
@ -74,8 +72,4 @@ public class Variable<Element> : ObservableType {
|
|||
public func asObservable() -> Observable<E> {
|
||||
return self.subject
|
||||
}
|
||||
|
||||
deinit {
|
||||
self.subject.on(.Completed)
|
||||
}
|
||||
}
|
||||
|
|
@ -49,20 +49,17 @@ class UIRxTests : RxTest {
|
|||
// We have some async Wolfram Alpha API that calculates is number prime.
|
||||
let WolframAlphaIsPrime: (Int) -> Observable<PrimeNumber> = { just(PrimeNumber($0, isPrime($0))) }
|
||||
|
||||
let text = Variable<String>("")
|
||||
let resultText = ""
|
||||
|
||||
let primeTextField = UITextFieldMock()
|
||||
|
||||
let resultLabel = UILabelMock()
|
||||
|
||||
let disposable = primeTextField.rx_text()
|
||||
let _ = primeTextField.rx_text()
|
||||
.map { WolframAlphaIsPrime(Int($0) ?? 0) }
|
||||
.concat()
|
||||
.map { "number \($0.n) is prime? \($0.isPrime)" }
|
||||
.subscribeTextOf(resultLabel)
|
||||
.scopedDispose
|
||||
|
||||
.scopedDispose()
|
||||
|
||||
// this will set resultLabel.text! == "number 43 is prime? true"
|
||||
primeTextField.text = "43"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,9 +19,10 @@ class VariableTest : RxTest {
|
|||
|
||||
var latestValue: Int?
|
||||
|
||||
let subscription = c .subscribeNext { next in
|
||||
latestValue = next
|
||||
}
|
||||
let subscription = c
|
||||
.subscribeNext { next in
|
||||
latestValue = next
|
||||
}
|
||||
|
||||
XCTAssertEqual(latestValue!, 3)
|
||||
|
||||
|
|
@ -39,34 +40,7 @@ class VariableTest : RxTest {
|
|||
|
||||
XCTAssertEqual(latestValue!, 14)
|
||||
}
|
||||
|
||||
func testVariable_Completed() {
|
||||
var a = Variable(1)
|
||||
var b = Variable(2)
|
||||
|
||||
let c = combineLatest(a, b, +)
|
||||
|
||||
var latestValue: Int?
|
||||
var completed = false
|
||||
|
||||
let subscription = c.subscribe(next: { next in
|
||||
latestValue = next
|
||||
}, completed: {
|
||||
completed = true
|
||||
})
|
||||
|
||||
XCTAssertEqual(latestValue!, 3)
|
||||
|
||||
a.value = 5
|
||||
|
||||
XCTAssertEqual(latestValue!, 7)
|
||||
|
||||
XCTAssertTrue(!completed)
|
||||
a = Variable(0)
|
||||
b = Variable(0)
|
||||
XCTAssertTrue(completed)
|
||||
}
|
||||
|
||||
|
||||
func testVariable_READMEExample() {
|
||||
|
||||
// Two simple Rx variables
|
||||
|
|
@ -87,10 +61,12 @@ class VariableTest : RxTest {
|
|||
// because variables have initial values (starting element)
|
||||
var latestValueOfC : Int? = nil
|
||||
// let _ = doesn't retain.
|
||||
let d/*: Disposable*/ = c .subscribeNext { c in
|
||||
//print("Next value of c = \(c)")
|
||||
latestValueOfC = c
|
||||
} .scopedDispose
|
||||
let d/*: Disposable*/ = c
|
||||
.subscribeNext { c in
|
||||
//print("Next value of c = \(c)")
|
||||
latestValueOfC = c
|
||||
}
|
||||
.scopedDispose()
|
||||
|
||||
XCTAssertEqual(latestValueOfC!, 3)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue