Skip to content

Instantly share code, notes, and snippets.

@Laimiux
Last active March 27, 2018 06:20
Show Gist options
  • Save Laimiux/ffcb6e3641721abab8558feb577b6a30 to your computer and use it in GitHub Desktop.
Save Laimiux/ffcb6e3641721abab8558feb577b6a30 to your computer and use it in GitHub Desktop.
fun dataStream(
textChangedEvents: Flowable<TextChangedEvent>,
submitCommentEvents: Flowable<SubmitCommentEvent>
): Flowable<CommentFormData> {
// We need to start with something
val initialFormState = CommentFormData(comment = "", isCommentValid = false, submitRequest = null)
// RxJava scan operator takes initial state and a reduce function
// This operator manages the state for us, where it will keep track
// of the latest state and provide it for next incoming event
return textChangedEvents.scan(initialFormState) { event, currentState ->
reduce(event, currentState)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment