vertx-lang-kotlin icon indicating copy to clipboard operation
vertx-lang-kotlin copied to clipboard

Support suspending functions in setPeriodic

Open AlexeySoshin opened this issue 3 years ago • 6 comments

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.

AlexeySoshin avatar Oct 27 '20 10:10 AlexeySoshin

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

vietj avatar Oct 28 '20 17:10 vietj

@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 for toChannel()
  • Has to be invoke inside a suspending function or a coroutine

AlexeySoshin avatar Oct 29 '20 11:10 AlexeySoshin

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

asad-awadia avatar Apr 07 '21 14:04 asad-awadia

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

AlexeySoshin avatar Oct 20 '21 22:10 AlexeySoshin

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 avatar Oct 21 '21 06:10 vietj

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

vietj avatar Oct 21 '21 09:10 vietj

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

kamilmodest avatar Sep 20 '22 07:09 kamilmodest

thanks @AlexeySoshin

vietj avatar Sep 20 '22 08:09 vietj