Skip to content

Instantly share code, notes, and snippets.

@nsadeh
Last active August 8, 2020 23:12
Show Gist options
  • Save nsadeh/18fa83b641951eed8436a6222f9a6ec1 to your computer and use it in GitHub Desktop.
Save nsadeh/18fa83b641951eed8436a6222f9a6ec1 to your computer and use it in GitHub Desktop.
Record of chat sessions
import akka.actor.typed.{ ActorSystem, SpawnProtocol }
object ChatSessionMap {
// mutable data structure holding all the chat sessions. Definition to follow
private var sessions: Map[String, ChatSession] = Map.empty[String, ChatSession]
// only function of this class is to keep track of chat sessions
def findOrCreate(userId: String)(implicit system: ActorSystem[SpawnProtocol.Command]): ChatSession = sessions.getOrElse(userId, create(userId))
// creates chat sessions. Role of the typed ActorSystem will be explained soon
private def create(userId: String)(implicit system: ActorSystem[SpawnProtocol.Command]) = {
val session = ChatSession(userId)
sessions += userId -> session
session
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment