vertx-lang-kotlin
vertx-lang-kotlin copied to clipboard
Support suspending functions in setPeriodic
Resolves #171
Motivation:
There was a request to support Kotlin suspending functions in Vertx setPeriodic
function.
My suggestion is to add a new extension method, setPeriodicAwait
, that will receive a suspending block.
Since we are not necessarily in a scope of coroutine, we create a new scope for this periodic task.
This is just a suggestion, and any comments regarding approach and implementation are more than welcome.
If this approach looks fine, we could extend it to setTimer
as well.
could it be achieved using the periodic stream facility that exist already ?
@vietj I looked into that, and we could do:
vertx.periodicStream(100).toChannel(vertx).consumeEach {
// Introduce some kind of suspending function
delay(10)
}
This will work as long as we're inside a suspending function.
There are some downsides to this approach, though:
- Code is more verbose
- Has to keep reference to
vertx
instance fortoChannel()
- Has to be invoke inside a suspending function or a coroutine
with the channel approach i think the code gets 'stuck' inside that loop
I don't want to coroutine launch that loop - I want to attach a suspending handler like the rest of all the vertx code and move on to the next line
@vietj Coming back to this topic, would you like to close this together with the issue or merge that solution?
I think we can work on this PR as indeed it's a valid use case and using vertx periodic stream will not make it better I think
sorry for the late answer @AlexeySoshin , I think we can get this reviewed (I made a comment) for 4.2
@vietj @AlexeySoshin Hey guys, any updates for this PR? I'm looking for a setTimer
that allows using a suspended function inside the handler
thanks @AlexeySoshin