diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2019-05-08 11:42:05 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2019-05-14 12:55:56 +0200 |
| commit | 1f77d1011577c54e98ad0cbbd898817f98000881 (patch) | |
| tree | 8ee70617f9fa7234f7b78d5056e59677979adc5e /odcsim-core/src | |
| parent | 791aa01938aef966ff3c0f2cd31d2aebdccddb6f (diff) | |
feat: Add support for spawning anonymous children
This change adds support for spawning anonymous children in an
ActorContext. This means a name does not have to be specified when
spawning an actor.
Diffstat (limited to 'odcsim-core/src')
4 files changed, 12 insertions, 2 deletions
diff --git a/odcsim-core/src/main/kotlin/com/atlarge/odcsim/ActorContext.kt b/odcsim-core/src/main/kotlin/com/atlarge/odcsim/ActorContext.kt index 8ea4a09d..499e7df0 100644 --- a/odcsim-core/src/main/kotlin/com/atlarge/odcsim/ActorContext.kt +++ b/odcsim-core/src/main/kotlin/com/atlarge/odcsim/ActorContext.kt @@ -76,6 +76,14 @@ interface ActorContext<T : Any> { fun <U : Any> spawn(behavior: Behavior<U>, name: String): ActorRef<U> /** + * Spawn an anonymous child actor from the given [Behavior]. + * + * @param behavior The behavior of the child actor to spawn. + * @return A reference to the child that has/will be spawned. + */ + fun <U : Any> spawnAnonymous(behavior: Behavior<U>): ActorRef<U> + + /** * Request the specified child actor to be stopped in asynchronous fashion. * * @param child The reference to the child actor to stop. diff --git a/odcsim-core/src/main/kotlin/com/atlarge/odcsim/coroutines/Behavior.kt b/odcsim-core/src/main/kotlin/com/atlarge/odcsim/coroutines/Behavior.kt index b963cdb6..eb26add1 100644 --- a/odcsim-core/src/main/kotlin/com/atlarge/odcsim/coroutines/Behavior.kt +++ b/odcsim-core/src/main/kotlin/com/atlarge/odcsim/coroutines/Behavior.kt @@ -32,8 +32,8 @@ import com.atlarge.odcsim.internal.SuspendingActorContextImpl import com.atlarge.odcsim.internal.SuspendingBehaviorImpl import kotlin.coroutines.Continuation import kotlin.coroutines.CoroutineContext -import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn +import kotlin.coroutines.suspendCoroutine /** * A [Behavior] that allows method calls to suspend execution via Kotlin coroutines. diff --git a/odcsim-core/src/main/kotlin/com/atlarge/odcsim/internal/Coroutines.kt b/odcsim-core/src/main/kotlin/com/atlarge/odcsim/internal/Coroutines.kt index a9e20d0e..13e722fc 100644 --- a/odcsim-core/src/main/kotlin/com/atlarge/odcsim/internal/Coroutines.kt +++ b/odcsim-core/src/main/kotlin/com/atlarge/odcsim/internal/Coroutines.kt @@ -105,6 +105,8 @@ internal class SuspendingBehaviorImpl<T : Any>( override fun <U : Any> spawn(behavior: Behavior<U>, name: String) = actorContext.spawn(behavior, name) + override fun <U : Any> spawnAnonymous(behavior: Behavior<U>) = actorContext.spawnAnonymous(behavior) + override fun stop(child: ActorRef<*>): Boolean = actorContext.stop(child) override fun watch(target: ActorRef<*>) = actorContext.watch(target) diff --git a/odcsim-core/src/test/kotlin/com/atlarge/odcsim/CoroutinesTest.kt b/odcsim-core/src/test/kotlin/com/atlarge/odcsim/CoroutinesTest.kt index d6bf6a74..98486149 100644 --- a/odcsim-core/src/test/kotlin/com/atlarge/odcsim/CoroutinesTest.kt +++ b/odcsim-core/src/test/kotlin/com/atlarge/odcsim/CoroutinesTest.kt @@ -24,8 +24,8 @@ package com.atlarge.odcsim -import com.atlarge.odcsim.coroutines.suspending import com.atlarge.odcsim.coroutines.SuspendingBehavior +import com.atlarge.odcsim.coroutines.suspending import com.atlarge.odcsim.internal.BehaviorInterpreter import com.atlarge.odcsim.internal.EmptyBehavior import com.nhaarman.mockitokotlin2.mock |
