Skip to content

Instantly share code, notes, and snippets.

@Sottti
Created October 10, 2025 22:25
Show Gist options
  • Select an option

  • Save Sottti/04e5db79381168345b1c2d952885c4e7 to your computer and use it in GitHub Desktop.

Select an option

Save Sottti/04e5db79381168345b1c2d952885c4e7 to your computer and use it in GitHub Desktop.

Revisions

  1. Sottti created this gist Oct 10, 2025.
    8 changes: 8 additions & 0 deletions StateInWhileSubscribed.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    context(viewModel: ViewModel)
    public fun <T> Flow<T>.stateInWhileSubscribed(
    initialValue: T,
    ): StateFlow<T> = stateIn(
    scope = viewModel.viewModelScope,
    started = WhileSubscribed(stopTimeoutMillis = 5_000),
    initialValue = initialValue,
    )