Permalink
Browse files

Fix a generic subclassing bug occured in #49

  • Loading branch information...
devxoul committed Nov 17, 2017
1 parent ca4155c commit c902bb38c21575d735dd933c6fe0152288d88232
Showing with 36 additions and 2 deletions.
  1. +9 −2 Sources/ReactorKit/Reactor.swift
  2. +27 −0 Tests/ReactorKitTests/ReactorTests.swift
@@ -82,9 +82,11 @@ extension Reactor {
}
}
public var action: ActionSubject<Action> {
// Creates a state stream automatically
_ = self._state
// It seems that Swift has a bug in associated object when subclassing a generic class. This is
// a temporary solution to bypass the bug. See #30 for details.
_ = self.state
return self._action
}
@@ -93,13 +95,18 @@ extension Reactor {
set { self.setAssociatedObject(newValue, forKey: &currentStateKey) }
}
public var state: Observable<State> {
private var _state: Observable<State> {
if self.stub.isEnabled {
return self.stub.state.asObservable()
} else {
return self.associatedObject(forKey: &stateKey, default: self.createStateStream())
}
}
public var state: Observable<State> {
// It seems that Swift has a bug in associated object when subclassing a generic class. This is
// a temporary solution to bypass the bug. See #30 for details.
return self._state
}
fileprivate var disposeBag: DisposeBag {
get { return self.associatedObject(forKey: &disposeBagKey, default: DisposeBag()) }
@@ -210,6 +210,33 @@ final class ReactorTests: XCTestCase {
let address2 = ObjectIdentifier(reactor.action).hashValue
XCTAssertEqual(address1, address2)
}
func testGenericSubclassing_stateIsCreatedWhenAccessAction() {
class ParentReactor<T>: Reactor {
enum Action {
case foo
}
typealias Mutation = Void
typealias State = Int
let initialState: State = 0
func mutate(action: Action) -> Observable<Mutation> {
return .just(Void())
}
func reduce(state: State, mutation: Mutation) -> State {
return state + 1
}
}
class ChildReactor: ParentReactor<String> {
}
let reactor = ChildReactor()
XCTAssertEqual(reactor.currentState, 0)
reactor.action.onNext(.foo)
XCTAssertEqual(reactor.currentState, 1)
}
}
struct TestError: Error {

0 comments on commit c902bb3

Please sign in to comment.