vert-x3 / vertx-lang-kotlin

Vert.x for Kotlin
Apache License 2.0
296 stars 68 forks source link

Support suspending functions in setPeriodic #184

Closed AlexeySoshin closed 2 years ago

AlexeySoshin commented 4 years ago

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.

vietj commented 4 years ago

could it be achieved using the periodic stream facility that exist already ?

AlexeySoshin commented 4 years ago

@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:

asad-awadia commented 3 years ago

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

AlexeySoshin commented 3 years ago

@vietj Coming back to this topic, would you like to close this together with the issue or merge that solution?

vietj commented 3 years ago

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

vietj commented 3 years ago

sorry for the late answer @AlexeySoshin , I think we can get this reviewed (I made a comment) for 4.2

kamilmodest commented 2 years ago

@vietj @AlexeySoshin Hey guys, any updates for this PR? I'm looking for a setTimer that allows using a suspended function inside the handler

vietj commented 2 years ago

thanks @AlexeySoshin