forked from vert-x3/vertx-lang-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Follows implementation of coroutine router in vert-x3#253 Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
- Loading branch information
1 parent
6ffe3b8
commit 4297619
Showing
6 changed files
with
136 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 0 additions & 54 deletions
54
vertx-lang-kotlin-coroutines/src/main/java/io/vertx/kotlin/coroutines/CoroutineEventBus.kt
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
...ng-kotlin-coroutines/src/main/java/io/vertx/kotlin/coroutines/CoroutineEventBusSupport.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* The Apache License v2.0 is available at | ||
* http://www.opensource.org/licenses/apache2.0.php | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
*/ | ||
package io.vertx.kotlin.coroutines | ||
|
||
import io.vertx.core.eventbus.EventBus | ||
import io.vertx.core.eventbus.Message | ||
import io.vertx.core.eventbus.MessageConsumer | ||
import io.vertx.core.eventbus.ReplyFailure.RECIPIENT_FAILURE | ||
import io.vertx.core.impl.ContextInternal | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.EmptyCoroutineContext | ||
|
||
/** | ||
* Calls the specified function [block] with a [CoroutineEventBusSupport] object as its receiver. | ||
* | ||
* The receiver's scope is the [CoroutineScope] of the caller. | ||
*/ | ||
fun CoroutineScope.coroutineEventBus(block: CoroutineEventBusSupport.() -> Unit) { | ||
val receiver = object : CoroutineEventBusSupport { | ||
override val coroutineContext = this@coroutineEventBus.coroutineContext | ||
} | ||
with(receiver) { | ||
block() | ||
} | ||
} | ||
|
||
/** | ||
* Adds support for suspending functions to the Vert.x [EventBus]. | ||
* | ||
* Objects of this type implement [CoroutineScope] to define a scope for new coroutines. | ||
* Typically, this is the scope of a [CoroutineVerticle]. | ||
*/ | ||
interface CoroutineEventBusSupport : CoroutineScope { | ||
|
||
/** | ||
* Similar to [EventBus.consumer] but using a suspending [handler]. | ||
* | ||
* The coroutine context is inherited from the [CoroutineScope]. | ||
* Additional context elements can be specified with the [context] argument. | ||
* | ||
* @param context additional context elements, [EmptyCoroutineContext] by default | ||
*/ | ||
fun <T> EventBus.coConsumer( | ||
address: String, | ||
context: CoroutineContext = EmptyCoroutineContext, | ||
handler: suspend (Message<T>) -> Unit | ||
): MessageConsumer<T> = consumer<T>(address).coHandler(context, handler) | ||
|
||
/** | ||
* Similar to [MessageConsumer.handler] but using a suspending [handler]. | ||
* | ||
* The coroutine context is inherited from the [CoroutineScope]. | ||
* Additional context elements can be specified with the [context] argument. | ||
* | ||
* @param context additional context elements, [EmptyCoroutineContext] by default | ||
*/ | ||
fun <T> MessageConsumer<T>.coHandler( | ||
context: CoroutineContext = EmptyCoroutineContext, | ||
handler: suspend (Message<T>) -> Unit | ||
): MessageConsumer<T> = handler { | ||
launch((ContextInternal.current()?.dispatcher() ?: EmptyCoroutineContext) + context) { | ||
try { | ||
handler(it) | ||
} catch (e: Exception) { | ||
it.fail(RECIPIENT_FAILURE.toInt(), e.message) | ||
} | ||
} | ||
} | ||
} |
43 changes: 0 additions & 43 deletions
43
...ng-kotlin-coroutines/src/main/java/io/vertx/kotlin/coroutines/CoroutineMessageConsumer.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters