myanmarking
02/05/2020, 3:23 PMstreetsofboston
02/05/2020, 3:39 PMfun <T> Flow<T>.timeout(millis: Long) = flow {
    this@timeout.collect {
        withTimeout(millis) {
            emit(it)
        }
    }
}myanmarking
02/05/2020, 3:40 PMmyanmarking
02/05/2020, 3:41 PMfun <T> Flow<T>.timeout(durationInMillis: Long): Flow<T> =
    flow {
        coroutineScope {
            val outerScope = this
            launch {
                try {
                    delay(durationInMillis)
                    outerScope.cancel()
                } catch (e: CancellationException) {
                    outerScope.cancel(e) // cancel outer scope on cancellation exception, too
                }
            }
            collect { emit(it) }
        }
    }myanmarking
02/05/2020, 3:42 PMmyanmarking
02/05/2020, 3:54 PMDominaezzz
02/05/2020, 10:04 PMfun <T> Flow<T>.timeout(millis: Long) = flow {
    withTimeout(millis) {
        this@timeout.collect {
            emit(it)
        }
    }
}myanmarking
02/06/2020, 9:53 AM