Skip to content

Instantly share code, notes, and snippets.

@blakelee
Created November 14, 2024 19:00
Show Gist options
  • Select an option

  • Save blakelee/9d9f4f263b0ac7ca6a2760c872b45339 to your computer and use it in GitHub Desktop.

Select an option

Save blakelee/9d9f4f263b0ac7ca6a2760c872b45339 to your computer and use it in GitHub Desktop.
A sample of how to use RxJava to debounce a user typing text while also prioritizing a user pressing enter and cancelling whatever inflight debounce there was prior.
val textSubject = PublishSubject.create<String>()
val enterPressed = PublishSubject.create<String>()
val debouncedInput = textSubject.debounce(500, TimeUnit.MILLISECONDS)
.takeUntil(enterPressed)
.repeat()
val debounce = Observable.merge(
debouncedInput.map { AnnotatedString(it, SpanStyle(Color.Red)) },
enterPressed.map { AnnotatedString(it, SpanStyle(Color.Blue)) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment