/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.mozilla.fenix.mvi import io.reactivex.Observable import io.reactivex.Observer import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers abstract class UIComponent( protected val actionEmitter: Observer, protected val changesObservable: Observable ) { abstract var initialState: S abstract val reducer: Reducer open val uiView: UIView by lazy { initView() } abstract fun initView(): UIView open fun getContainerId() = uiView.containerId /** * Render the ViewState to the View through the Reducer */ fun render(reducer: Reducer): Disposable = internalRender(reducer) .subscribe(uiView.updateView()) fun internalRender(reducer: Reducer): Observable = changesObservable .scan(initialState, reducer) .distinctUntilChanged() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) }