https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

simon.vergauwen

12/30/2018, 10:56 PM
Is it possible to make the equivalent of an immediate executor but a
CoroutineContext
without depending on
coroutines-core
but only on what can be found in
std-lib
?
b

bdawg.io

12/30/2018, 11:40 PM
Isn't CoroutineContext defined by coroutines-core?
What do you mean by an "immediate executor"?
d

Dico

12/31/2018, 1:40 AM
You have to implement
ContinuationInterceptor
and put it in the context
CoroutineContext
can be found in the std lib, it is not part of kotlinx library.
s

simon.vergauwen

12/31/2018, 9:34 AM
@bdawg.io I need an executor that executes the code on immediately on the current thread. Something like Guava directExecutor.
Copy code
final class DirectExecutor implements Executor {
   public void execute(Runnable r) {
     r.run();
   }
 }
d

Dico

12/31/2018, 9:36 AM
Then I suggest
override fun interceptContinuation(cont: Continuation<*>) = cont
Excuse me if that's not the signature
s

simon.vergauwen

12/31/2018, 9:41 AM
Alright thanks, I’ll look into that 🙂 What are you supposed to do with those keys?
d

Dico

12/31/2018, 9:42 AM
Look at documentation of
CoroutineContext
s

simon.vergauwen

12/31/2018, 9:44 AM
😅 Thanks!
The key that you need is here
s

simon.vergauwen

12/31/2018, 9:49 AM
Could you help clarify how returning the
continuation
like so
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> = continuation
executes it on the immediate thread?
The interception they refer to in the docs, does that mean intercept to coroutine to execute it on some other context? Thus intercepting all resumptions to that new context as well.
d

Dico

12/31/2018, 9:51 AM
The intercepted continuation optionally wraps the original, scheduling it to be resumed on a thread or in some event loop
s

simon.vergauwen

12/31/2018, 9:53 AM
Alright, thanks a lot for the help 🙂
3 Views