ursus
02/28/2024, 7:18 PMfun <T : Any> Flow<T?>.validate( validator: (T) -> Status): Flow<Status> =
distinctUntilChanged()
.transformLatest {
val valueAndMetadata = it ?: return@transformLatest <--------------
emit(Pending)
delay(500)
val status = withContext(Dispatchers.Default) { <------------
validator(valueAndMetadata)
}
emit(status)
}
can anyone double check this?
is the usage of withContext
in a transformLatest
operator to offload a synchronous calculation correct?
I faintly remember someone complaining about switching dispatchers manually in flow operatorsCLOVIS
02/29/2024, 8:58 AMemit
from other dispatchers. In this case, you switch temporarily but go back to the original dispatcher before emitting, so I believe it's fine (but I'm no expert)CLOVIS
02/29/2024, 9:00 AMval valueAndMetadata = it ?: return@transformLatest
I would just use .filterNotNull()
before the transformLatest
ursus
02/29/2024, 12:13 PMursus
02/29/2024, 12:13 PM