This file contains hidden or 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
private sealed class Event { | |
data class TodoCheckedEvent(val id: Long, val completed: Boolean) : Event() | |
data class NewTodoEvent(val text: String) : Event() | |
data class DbUpdateEvent(val todos: List<TodoEntity>) : Event() | |
} |
This file contains hidden or 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
inline fun <A, C, reified T> Observable<Triple<A, A, C>>.ofEventType(): Observable<Triple<A, A, T>> |
This file contains hidden or 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
@Entity(tableName = "todo") | |
data class TodoEntity( | |
@PrimaryKey(autoGenerate = true) var id: Long, | |
var text: String, | |
var completed: Boolean | |
) | |
private data class State( | |
val todos: List<TodoEntity> | |
) |
This file contains hidden or 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
fun <S, E> createConnectableKnot( | |
initial: S, | |
eventsSource: Observable<E>, | |
reducer: (S, E) -> S, | |
transformer: ObservableTransformer<in Triple<S, S, E>, out E> | |
): ConnectableObservable<E> { | |
return Observable.create<E> { observer -> | |
val state = behaviorSubjectDefault(initial) | |
val events = publishSubject<E>() |
This file contains hidden or 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
fun <S, E> createKnot( | |
initial: S, | |
eventsSource: Observable<E>, | |
reducer: (S, E) -> S, | |
transformer: ObservableTransformer<in Triple<S, S, E>, out E> | |
): Observable<E> { | |
val state = behaviorSubjectDefault(initial) | |
val events = publishSubject<E>() | |
eventsSource.subscribe(events::onNext) | |
events.withLatestFrom(state, BiFunction<E, S, Triple<S, S, E>> { ev, oldState -> |
This file contains hidden or 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
val transformer = store.publish { upstream → | |
Observable.merge( | |
upstream.ofEventType<NameChangeEvent>.switchMap { (_, state, _) → | |
api.validate(state.name) | |
} | |
.map { apiResult → ValidationResult(…) }, | |
upstream.ofEventType<OtherEvent>.map { …. } | |
) | |
} |
This file contains hidden or 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
fun <S, E> createStore( | |
initial: S, | |
eventsSource: Observable<E>, | |
reducer: (S, E) -> S, | |
transformer: ObservableTransformer<in Triple<S, S, E>, out E> | |
): Observable<S> { | |
val state = behaviorSubjectDefault(initial) | |
val events = behaviorSubject<E>() | |
eventsSource.subscribe(events::onNext) | |
events.withLatestFrom(state) { ev, oldState -> |
This file contains hidden or 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
val state = BehaviourSubject.create<State>() | |
nameInput.withLatestFrom(state) { name, state -> | |
state.copy(name = name) | |
} | |
.subscribe(state) | |
checkBoxChecks.withLatestFrom(state) { сbState, state →thLa | |
state.copy(checkBoxCheched = cbState) | |
} |
This file contains hidden or 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
val viewInput = Observable.combineLatest( | |
nameInput.startWith(“”), | |
checkBoxChecks.startWith(false) | |
) { name, selected → State(name, selected) } | |
val dataFromDb = database.dao().loadById(…).map { dataFromDb → State(…) } | |
val state = Observable.merge(viewInput, dataFromDb) |
This file contains hidden or 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
val showToast: Observable<Unit> | |
//... | |
val showToastSubject = PublishSubject.create<Unit>() | |
showToast = showToastSubject | |
val errorEffect = ObservableTransformer<Triple<State, State, Event>, Event> { upstream -> | |
upstream.ofEventType<ServerResponseEvent>.switchMap { (_, _, event) → | |
if (!event.isSuccessful) showToastSubject.onNext(Unit) | |
Observable.empty() | |
} | |
} |