dimsuz
09/10/2021, 4:22 PMsuspend fun doWork()
and want to also wrap it in "listener-form" like
fun doWork(onWorkDone: () -> Unit) {
someScope.launch { doWork(); onWorkDone() }
}
I fear this might have some thread safety issues. What would be the "correct" way to do something like this?
Why I want this: having a public API, where coroutines are an implementation detail. So I don't want to expose suspend functions or CoroutineScope anywhere.CLOVIS
09/10/2021, 5:04 PMonWorkDone callback might not necessarily (depending on the scope you use) be executed in the same thread that called doWork().Adam Powell
09/10/2021, 5:18 PMsuspend for something like this is a mistake. If you need java compatibility you can offer wrappers for them into Rx Singles, ListenableFutures, or whatever else, but hiding suspend from kotlin users is going to make everyone's lives harderAdam Powell
09/10/2021, 5:20 PMsuspend both for callers and implementersdimsuz
09/10/2021, 10:09 PMCLOVIS
09/10/2021, 10:23 PMsuspend functions, as it removes all these thread safety issues and it helps the caller understand what is going on (otherwise, they'd have to wrap your interface into their own).
If your clients are using JS, then your solution is safe (JS is single-threaded anyway), if they are using Java or another multi-threaded language then your solution isn't unsafe, it just might be a bit surprising that the lambda works in another thread. It could make it easy for users to write unsafe code, though.dimsuz
09/10/2021, 10:38 PM