Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Action.Map #5

Open
Tarmil opened this issue Jul 26, 2019 · 0 comments
Open

Add Action.Map #5

Tarmil opened this issue Jul 26, 2019 · 0 comments
Labels
enhancement New feature or request

Comments

@Tarmil
Copy link
Member

Tarmil commented Jul 26, 2019

We need a function to wrap an action from a child update in a parent update, see https://forums.websharper.com/topic/87100:

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)

// Use:
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)

We could also provide a "magic" macro, somewhat similar to Lens, that would allow passing this:

|> Action.MagicMap AuthMessage state.UserInfo

and would expand to the same as above:

|> Action.Map AuthMessage state (fun s i -> { s with UserInfo = i }) (fun s -> s.UserInfo)
@Tarmil Tarmil added the enhancement New feature or request label Jul 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant