Created
March 21, 2019 14:37
-
-
Save MovileGente/efe29ee0d4d5a4039730153dfbf84602 to your computer and use it in GitHub Desktop.
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
sealed class ActorCommand<out T> { | |
class REGISTER<T>(val id: Int, val name: String, val response: SendChannel<T>? = null) : ActorCommand<T>() | |
class REMOVE<T>(val id: Int, val response: SendChannel<T>? = null) : ActorCommand<T>() | |
class QUERY<T>(val response: SendChannel<T>) : ActorCommand<T>() | |
} | |
val actorJob = actor<ActorCommand<Any?>> { | |
val state = mutableMapOf<Int, String>() | |
for (msg in channel) { | |
when (msg) { | |
is ActorCommand.REGISTER -> { | |
val result = state.getOrPut(msg.id) { | |
msg.name | |
} | |
msg.response?.send(result) | |
} | |
is ActorCommand.REMOVE -> { | |
val result = state.remove(msg.id) | |
msg.response?.send(result) | |
} | |
is ActorCommand.QUERY -> msg.response.send(state.toMap()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment