port - How to chain port calls?
问题描述
port createNewDocument: Encode.Value -> Cmd msg
port printDocument : () -> Cmd msg
createNewDocument: Document -> Task err msg
printDocument: Task err msg
i want to chain this create
and print
steps, in one step. Because sometimes i need both one after another - some other times i need to create the document
- make some updates
, then print
.
someCmd: Cmd msg
someCmd = createNewDocument |> Task.andThen (\ what? -> -- what to add here? printDocument ) |> Task.attempt (\ result -> some result handler )
how can i chain port
calls? Because ports return Cmd msg
not Task err msg
.
解决方案
如果不引入中间的信息,就无法做到这一点。端口也只是单向的,因此您需要订阅端口才能从外部源返回值。
即:您的第一个命令触发了一个 JavaScript 函数,该函数通过订阅发送消息,并且在您的update
函数中,您通过返回第二个命令来处理该消息。
type Msg
= ...
| CreateNewDocument Encode.Value
| PrintDocument Document
update : Msg -> Model -> (Model, Cmd.model)
update msg model =
case msg of
...
CreateNewDocument value ->
(model, createNewDocument value)
PrintDocument document ->
(model, printDocument document)
sub : Sub Msg
sub =
receiveNewDocument PrintDocument
推荐阅读
- python - 为什么数据框中两行的 datetime = 0 的差异?
- html - 媒体查询和性能与在小屏幕上显示大文件大小的图像
- javascript - Nodejs:当命令不返回任何内容时,child_process.exec 会出错
- python - 如何在python中将字典结构转换为另一个字典
- javascript - 禁用 `sticky` 和 `fixed` CSS(用于完整网页截图)
- python - 用 pygame 制作蛇。当蛇接触到饼干时,难以使蛇变长
- amazon-web-services - AWS Pinpoint SMS - 消息属性/元数据
- azure-sql-database - 授予创建任何数据库权限
- html - 背景颜色更改代码会自行更改
- python - 如何通过字符串获取 chr 的代码?