I try to use TMap
the following way:
Define the TMap
:
val chatIdMapSTM: ChatIds = TMap.make(camunda_group -> camundaChatId, "pme123" -> 275469757L)
Put an entry:
def registerChat(maybeId: Option[ChatUserOrGroup], chatId: ChatId): ZIO[Any, RegisterException, String] =
(for {
chatStm <- chatIdMapSTM
_ <- maybeId.map(id => chatStm.put(id, chatId)).getOrElse(chatIdMapSTM)
values <- chatStm.values // List(1212, 275469757, -319641852)
} yield chatStm).commit
.as("your chat was successful registered")
Then try to get that value:
def requestChat(chatUserOrGroup: ChatUserOrGroup): UIO[ChatId] =
(for {
chatStm <- chatIdMapSTM
values <- chatStm.values // List(275469757, -319641852)
chatId <- chatStm.getOrElse(chatUserOrGroup, camundaChatId)
} yield chatId).commit
As the comments suggests, when I request the entry, the new value is not there.
Do I miss something?
It seems that you've never committed the chat map, hence you're always "starting from scratch". See the following working example:
import zio._
import zio.console._
import zio.stm._
object ChatsExample extends App {
def run(args: List[String]) =
for {
map <- chatIdMapSTM.commit
res <- registerChat(map, "dejan", 123L)
_ <- putStrLn(res)
chat <- requestChat(map, "dejan")
_ <- putStrLn(s"Found id: $chat")
vals <- map.values.commit
_ <- putStrLn(vals.mkString(","))
} yield 0
val camundaChatId = 0L
val chatIdMapSTM = TMap.make("camunda" -> camundaChatId, "pme123" -> 275469757L)
def registerChat(chats: TMap[String, Long], userId: String, chatId: Long) =
(chats.put(userId, chatId).as("your chat was successfully registered")).commit
def requestChat(chats: TMap[String, Long], userId: String) =
chats.getOrElse(userId, camundaChatId).commit
}