Skip to content

Commit

Permalink
Merge branch 'master' into v4
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyson Williams committed Dec 4, 2020
2 parents ab5646d + 8de9aa8 commit 6836050
Showing 1 changed file with 41 additions and 27 deletions.
68 changes: 41 additions & 27 deletions src/Samples/SubModelSeq/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ open Serilog.Extensions.Logging
open Elmish.WPF


type InOutMsg<'InMsg, 'OutMsg> =
| InMsg of 'InMsg
| OutMsg of 'OutMsg


module Option =

let set a = Option.map (fun _ -> a)
Expand Down Expand Up @@ -168,6 +173,11 @@ module App =
| MoveUp of Guid
| MoveDown of Guid

type SubtreeOutMsg =
| OutRemove
| OutMoveUp
| OutMoveDown

type Msg =
| ToggleGlobalState
| SubtreeMsg of RoseTreeMsg<Guid, SubtreeMsg>
Expand Down Expand Up @@ -217,6 +227,11 @@ module App =
| ToggleGlobalState -> mapSomeGlobalState not
| SubtreeMsg msg -> msg |> RoseTree.update hasId updateSubtree |> mapDummyRoot

let mapOutMsg = function
| OutRemove -> Remove
| OutMoveUp -> MoveUp
| OutMoveDown -> MoveDown


module Bindings =

Expand All @@ -229,57 +244,56 @@ module Bindings =
let moveUpMsg (_, { Parent = p; Self = s }) =
match p.Children |> List.tryHead with
| Some c when c.Data.Id <> s.Data.Id ->
s.Data.Id |> MoveUp |> Some
OutMoveUp |> OutMsg |> Some
| _ -> None

let moveDownMsg (_, { Parent = p; Self = s }) =
match p.Children |> List.tryLast with
| Some c when c.Data.Id <> s.Data.Id ->
s.Data.Id |> MoveDown |> Some
OutMoveDown |> OutMsg |> Some
| _ -> None

let adjustMsgToParent msg =
match msg with
| BranchMsg (pId, LeafMsg (Remove cId)) when pId = cId -> LeafMsg (Remove cId)
| BranchMsg (pId, LeafMsg (MoveUp cId)) when pId = cId -> LeafMsg (MoveUp cId)
| BranchMsg (pId, LeafMsg (MoveDown cId)) when pId = cId -> LeafMsg (MoveDown cId)
| _ -> msg

let rec subtreeBindings () : Binding<Model * SelfWithParent<RoseTree<Identifiable<Counter>>>, RoseTreeMsg<Guid, SubtreeMsg>> list =

let rec subtreeBindings () : Binding<Model * SelfWithParent<RoseTree<Identifiable<Counter>>>, InOutMsg<RoseTreeMsg<Guid, SubtreeMsg>, SubtreeOutMsg>> list =
let counterBindings =
Counter.bindings ()
|> Bindings.mapModel (fun (_, { Self = s }) -> s.Data.Value)
|> Bindings.mapMsg (CounterMsg >> LeafMsg)
let newBindings =
[
"CounterIdText" |> Binding.oneWay(fun (_, { Self = s }) -> s.Data.Id)

"Remove" |> Binding.cmd(fun (_, { Self = s }) -> s.Data.Id |> Remove |> LeafMsg)


let inMsgBindings =
[ "CounterIdText" |> Binding.oneWay(fun (_, { Self = s }) -> s.Data.Id)
"AddChild" |> Binding.cmd(AddChild |> LeafMsg)

"MoveUp" |> Binding.cmdIf(moveUpMsg |> FuncOption.map LeafMsg)
"MoveDown" |> Binding.cmdIf(moveDownMsg |> FuncOption.map LeafMsg)

"GlobalState" |> Binding.oneWay(fun (m, _) -> m.SomeGlobalState)

"ChildCounters" |> Binding.subModelSeq(
(fun (_, { Self = p }) -> p.Children |> Seq.map (fun c -> { Self = c; Parent = p })),
(fun ((m, _), selfAndParent) -> (m, selfAndParent)),
(fun (_, { Self = c }) -> c.Data.Id),
BranchMsg >> adjustMsgToParent,
(fun (cId, inOutMsg) ->
match inOutMsg with
| InMsg msg -> (cId, msg) |> BranchMsg
| OutMsg msg -> cId |> mapOutMsg msg |> LeafMsg),
subtreeBindings)
]
[ counterBindings
newBindings ]
] @ counterBindings
|> Bindings.mapMsg InMsg

let outMsgBindings =
[ "Remove" |> Binding.cmd(OutRemove |> OutMsg)
"MoveUp" |> Binding.cmdIf moveUpMsg
"MoveDown" |> Binding.cmdIf moveDownMsg ]

This comment has been minimized.

Copy link
@cmeeren

cmeeren Dec 4, 2020

Member

@TysonMN Above, you use Bindings.mapMsg, but here (and in moveUpMsg and moveDownMsg) you wrap in OutMsg directly. Isn't it better to consistently use mapMsg?

This comment has been minimized.

Copy link
@TysonMN

TysonMN Dec 4, 2020

Member

Ah, yes. Great catch. This was brought over in the merge from master, and I didn't reconsider it strongly enough.

When working in master, I have to make do without mapModel and mapMsg. Then when I switch to working in v4, I forget to fully utilize them.

I pushed a commit to v4 that makes the change you are suggested.


[ outMsgBindings
inMsgBindings ]
|> List.concat


let rootBindings () : Binding<Model, Msg> list = [
"Counters" |> Binding.subModelSeq(
(fun m -> m.DummyRoot.Children |> Seq.map (fun c -> { Self = c; Parent = m.DummyRoot })),
(fun { Self = c } -> c.Data.Id),
BranchMsg >> adjustMsgToParent >> SubtreeMsg,
(fun (cId, inOutMsg) ->
match inOutMsg with
| InMsg msg -> (cId, msg) |> BranchMsg
| OutMsg msg -> cId |> mapOutMsg msg |> LeafMsg
|> SubtreeMsg),
subtreeBindings)

"ToggleGlobalState" |> Binding.cmd ToggleGlobalState
Expand Down

0 comments on commit 6836050

Please sign in to comment.