Yeah, we should add a function to the library for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module Action =

    let rec Map (wrapMsg: 'ChildMessage -> 'ParentMessage)
                (currentModel: 'ParentModel)
                (wrapModel: 'ParentModel -> 'ChildModel -> 'ParentModel)
                (unwrapModel: 'ParentModel -> 'ChildModel)
                (action: Action<'ChildMessage, 'ChildModel>)
                : Action<'ParentMessage, 'ParentModel> =
        match action with
        | DoNothing -> DoNothing
        | SetModel childModel -> UpdateModel (fun parent -> wrapModel parent childModel)
        | UpdateModel f -> UpdateModel (fun parent -> unwrapModel parent |> f |> wrapModel parent)
        | Command cmd -> Command (fun dispatch -> cmd (wrapMsg >> dispatch))
        | CommandAsync cmd -> CommandAsync (fun dispatch -> cmd (wrapMsg >> dispatch))
        | CombinedAction actions -> CombinedAction (List.map (Map wrapMsg currentModel wrapModel unwrapModel) actions)

which allows you to do this:

1
2
3
4
5
6
7
8
let update msg (state: State) : Action<Message, State> =
    match msg with
    | TagListMessage m -> 
        let updatedTagList= Tags.update m state.TagList
        SetModel {state with TagList=updatedTagList}
    | AuthMessage m ->
        Authentication.update m state.UserInfo
        |> Action.Map AuthMessage state (fun s i -> { s with UserInfo = i }) (fun s -> s.UserInfo)
By on 7/22/2019 11:18 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper