榆树划分订阅?

时间:2016-12-31 15:35:14

标签: elm

我正在玩Elm和WebRTC,所以我创建了一个侦听端口,它从js获取一些消息:

type alias Message = 
    { channel : String
    , data : String
    }
port listen : (Message -> msg) -> Sub msg

现在我希望能够将消息分成我应用的不同部分。例如,聊天使用"聊天"频道,而游戏逻辑使用"游戏"。

是否可以创建listenTo String订阅,使用正确的频道过滤掉消息(仅返回数据)?或者可能是一种不同的做法?

更新

我目前拥有的是这样的:

在我的main.elm中,我有一个看起来像这样的更新。它可以接收消息(来自rtc),并发送消息进行聊天。 (我稍后会添加一个" ForGame"然后呢)

type Msg = Received WebRTC.Message | ForChat Chat.Msg

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
    case msg of
        Received message -> 
            let 
                _ = Debug.log ("Received message on \"" ++ message.channel ++ "\": " ++ message.data)
            in
                ( model
                , Cmd.none
                )
        ForChat msg ->
            let 
                (chatModel, chatCmd) = Chat.update msg model.chat
            in  
                ({ model | chat = chatModel}, Cmd.map ForChat chatCmd)

然后我订阅了所有订阅的订阅:

subscriptions : Model -> Sub Msg
subscriptions model = 
    Sub.batch 
        [ WebRTC.listen Received
        , Sub.map ForChat <| Chat.subscriptions model.chat
        ]

在Chat.elm中,我有一个类似的结构,有一个处理它的消息的更新。聊天的订阅会侦听来自WebRTC的所有消息,但只过滤具有频道聊天的消息:

subscriptions : Model -> Sub Msg
subscriptions model = WebRTC.listen forChatMessages

forChatMessages : WebRTC.Message -> Msg
forChatMessages webrtcMessage =
    if webrtcMessage.channel == "chat"
    then
        let
            message = decodeMessage webrtcMessage.data
        in
            case message of
                Ok msg -> Receive msg
                Err error -> Debug.log ("Received unreadable message on chat channel \"" ++ toString webrtcMessage.data ++ "\" with error \"" ++ error ++ "\"") Ignore
    else
        Ignore

(忽略是聊天的消息,它不执行任何操作case msg of Ignore -> (model, Cmd.none)decodeMessage使用解码器解码消息decodeMessage : String -> Result String Message。)

我对此非常满意,因为聊天的所有逻辑都在Chat.elm中。所以main.elm不需要知道聊天使用的频道。聊天只是遵循标准结构(消息,更新,视图,订阅)和主要转发所有内容。

唯一仍然不太好的是,在Chat.elm中我有forChatMessages功能。使用如:subscriptions model = WebRTC.listen forChatMessages。我想让它更易于重复使用,所以它会变得像:

subscriptions model = WebRTC.listen for "chat" decodeMessage Receive Ignore

然后游戏可以重复使用:

subscriptions model = WebRTC.listen for "game" decodeGameInfo UpdateInfo Ignore

更新2:

我设法将forChatMessages函数概括为:

for : String -> (String -> Result String d) -> (d -> msg) -> msg -> Message -> msg
for channel decoder good bad webrtcMessage =
    if 
        webrtcMessage.channel == channel
    then
        let
            decoded = decoder webrtcMessage.data
        in
            case decoded of
                Ok data -> good data
                Err error -> Debug.log ("Failed decoding message on " ++ channel ++ "channel \"" ++ toString webrtcMessage.data ++ "\" with error \"" ++ error ++ "\"") bad
    else
        bad

所以我想我自己找到了解决方案。除非有人对此发表评论。也许有更清洁/更好/更好的方法来做同样的事情?

2 个答案:

答案 0 :(得分:2)

我们假设您有以下Msg定义:

type Msg
    = Listen Message
    | GameChannel String
    | ChatChannel String

您的update函数可以对channel值执行操作,并使用正确的渠道再次致电update,忽略除channel以外的所有"game""chat"

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        Listen message ->
            case message.channel of
                "game" ->
                    update (GameChannel message.data) model

                "chat" ->
                    update (ChatChannel message.data) model

                _ ->
                    model ! []

        GameChannel data ->
            ...

        ChatChannel data ->
            ...

您的订阅功能如下所示:

subscriptions : Model -> Sub Msg
subscriptions model =
    listen Listen

答案 1 :(得分:1)

我自己找到了解决方案,并将其添加到原始问题中。

为清楚起见,这是简短版本:

在我的main.elm中:

type Msg = Received WebRTC.Message | ForChat Chat.Msg

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
    case msg of
        Received message -> 
            let 
                _ = Debug.log ("Received message on \"" ++ message.channel ++ "\": " ++ message.data)
            in
                ( model
                , Cmd.none
                )
        ForChat msg ->
            let 
                (chatModel, chatCmd) = Chat.update msg model.chat
            in  
                ({ model | chat = chatModel}, Cmd.map ForChat chatCmd)


subscriptions : Model -> Sub Msg
subscriptions model = 
    Sub.batch 
        [ WebRTC.listen Received
        , Sub.map ForChat <| Chat.subscriptions model.chat
        ]

在Chat.elm中:

subscriptions : Model -> Sub Msg
subscriptions model = WebRTC.listen <| for "game" decodeGameInfo UpdateInfo Ignore

在WebRTC.elm中:

type alias Message = 
    { channel : String
    , data : String
    }
port listen : (Message -> msg) -> Sub msg

for : String -> (String -> Result String d) -> (d -> msg) -> msg -> Message -> msg
for channel decoder good bad webrtcMessage =
    if 
        webrtcMessage.channel == channel
    then
        let
            decoded = decoder webrtcMessage.data
        in
            case decoded of
                Ok data -> good data
                Err error -> Debug.log ("Failed decoding message on " ++ channel ++ "channel \"" ++ toString webrtcMessage.data ++ "\" with error \"" ++ error ++ "\"") bad
    else
        bad