Last active
August 8, 2020 23:12
-
-
Save nsadeh/18fa83b641951eed8436a6222f9a6ec1 to your computer and use it in GitHub Desktop.
Record of chat sessions
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
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