Created
July 8, 2020 16:25
-
-
Save atimca/6f6ea2b83c757349633c9fcf7dae385c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RxCocoa | |
import RxSwift | |
import RxTest | |
open class TestStore<State: Core.State, Event>: Store<State, Event> where State.Event == Event { | |
private let observer: TestableObserver<State> | |
private let scheduler: TestScheduler | |
private let disposeBag: DisposeBag | |
public init(scheduler: TestScheduler, | |
disposeBag: DisposeBag, | |
initial: State, | |
feedBacks: [SideEffect<State, Event>]) { | |
self.scheduler = scheduler | |
self.disposeBag = disposeBag | |
self.observer = scheduler.createObserver(State.self) | |
super.init(initial: initial, feedBacks: feedBacks, reducer: State.reduce, scheduler: scheduler) | |
stateBus | |
.distinctUntilChanged() | |
.drive(observer) | |
.disposed(by: disposeBag) | |
} | |
public func start(with events: [Recorded<RxSwift.Event<Event>>] = []) -> [Recorded<RxSwift.Event<State>>] { | |
scheduler | |
.createColdObservable(events) | |
.bind(to: eventBus) | |
.disposed(by: disposeBag) | |
scheduler.start() | |
return observer.events | |
} | |
public func start(with events: [Event] = []) -> [State] { | |
start( | |
with: events | |
.enumerated() | |
.map { .next(($0.offset), $0.element) } | |
) | |
.compactMap(\.value.element) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment