TMap 在 ZIO RC18-2 上无法正常工作
TMap does not work as I expect with ZIO RC18-2
我尝试按以下方式使用 TMap
:
定义 TMap
:
val chatIdMapSTM: ChatIds = TMap.make(camunda_group -> camundaChatId, "pme123" -> 275469757L)
输入一个条目:
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")
然后尝试获取该值:
def requestChat(chatUserOrGroup: ChatUserOrGroup): UIO[ChatId] =
(for {
chatStm <- chatIdMapSTM
values <- chatStm.values // List(275469757, -319641852)
chatId <- chatStm.getOrElse(chatUserOrGroup, camundaChatId)
} yield chatId).commit
如评论所述,当我请求条目时,新值不存在。
我错过了什么吗?
看来你从来没有提交过聊天地图,所以你总是"starting from scratch"。请参阅以下工作示例:
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
}
我尝试按以下方式使用 TMap
:
定义 TMap
:
val chatIdMapSTM: ChatIds = TMap.make(camunda_group -> camundaChatId, "pme123" -> 275469757L)
输入一个条目:
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")
然后尝试获取该值:
def requestChat(chatUserOrGroup: ChatUserOrGroup): UIO[ChatId] =
(for {
chatStm <- chatIdMapSTM
values <- chatStm.values // List(275469757, -319641852)
chatId <- chatStm.getOrElse(chatUserOrGroup, camundaChatId)
} yield chatId).commit
如评论所述,当我请求条目时,新值不存在。
我错过了什么吗?
看来你从来没有提交过聊天地图,所以你总是"starting from scratch"。请参阅以下工作示例:
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
}