Skip to content

Instantly share code, notes, and snippets.

@atimca
Created July 8, 2020 16:25
Show Gist options
  • Save atimca/6f6ea2b83c757349633c9fcf7dae385c to your computer and use it in GitHub Desktop.
Save atimca/6f6ea2b83c757349633c9fcf7dae385c to your computer and use it in GitHub Desktop.
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