Skip to content

Instantly share code, notes, and snippets.

@MovileGente
Created March 21, 2019 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MovileGente/efe29ee0d4d5a4039730153dfbf84602 to your computer and use it in GitHub Desktop.
Save MovileGente/efe29ee0d4d5a4039730153dfbf84602 to your computer and use it in GitHub Desktop.
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