-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Program.fs
195 lines (147 loc) · 6.18 KB
/
Program.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
namespace Elmish.WPF.Samples.SubModelStatic
#nowarn "44"
open System
open Serilog
open Serilog.Extensions.Logging
open Elmish
open Elmish.WPF
module Counter =
type Model =
{ Count: int
StepSize: int
History: (int * int) list }
type Msg =
| Increment
| Decrement
| SetStepSize of int
| Reset
let init =
{ Count = 0
StepSize = 1
History = [] }
let canReset = (<>) init
let update msg m =
match msg with
| Increment -> { m with Count = m.Count + m.StepSize; History = (m.Count, m.History.Length) :: m.History }
| Decrement -> { m with Count = m.Count - m.StepSize; History = (m.Count, m.History.Length) :: m.History }
| SetStepSize x -> { m with StepSize = x }
| Reset -> init
type [<AllowNullLiteral>] CounterViewModel (args) =
inherit ViewModelBase<Counter.Model, Counter.Msg>(args)
let stepSizeBinding =
Binding.TwoWayT.id
>> Binding.addLazy (=)
>> Binding.mapModel (fun (m: Counter.Model) -> m.StepSize)
>> Binding.mapMsg Counter.Msg.SetStepSize
new() = CounterViewModel({ Counter.init with History = [ (3,1); (0,0) ] } |> ViewModelArgs.simple)
member _.StepSize
with get() = base.Get() stepSizeBinding
and set(v) = base.Set(v) stepSizeBinding
member _.CounterValue = base.Get() (Binding.OneWayT.id >> Binding.addLazy (=) >> Binding.mapModel (fun m -> m.Count))
member _.Increment = base.Get() (Binding.CmdT.setAlways Counter.Increment)
member _.Decrement = base.Get() (Binding.CmdT.setAlways Counter.Decrement)
member _.Reset = base.Get() (Binding.CmdT.set Counter.canReset Counter.Reset)
member _.History = base.Get() (Binding.OneWaySeqT.id (=) snd >> Binding.mapModel (fun m -> m.History))
module Clock =
type TimeType =
| Utc
| Local
type Model =
{ Time: DateTimeOffset
TimeType: TimeType }
let init () =
{ Time = DateTimeOffset.Now
TimeType = Local }
let getTime m =
match m.TimeType with
| Utc -> m.Time.UtcDateTime
| Local -> m.Time.LocalDateTime
type Msg =
| Tick of DateTimeOffset
| SetTimeType of TimeType
let update msg m =
match msg with
| Tick t -> { m with Time = t }
| SetTimeType t -> { m with TimeType = t }
type [<AllowNullLiteral>] ClockViewModel (args) =
inherit ViewModelBase<Clock.Model, Clock.Msg>(args)
new() = ClockViewModel(Clock.init () |> ViewModelArgs.simple)
member _.Time = base.Get() (Binding.OneWayT.id >> Binding.addLazy (=) >> Binding.mapModel Clock.getTime)
member _.IsLocal = base.Get() (Binding.OneWayT.id >> Binding.addLazy (=) >> Binding.mapModel (fun m -> m.TimeType = Clock.Local))
member _.SetLocal = base.Get() (Binding.CmdT.setAlways (Clock.SetTimeType Clock.Local))
member _.IsUtc = base.Get() (Binding.OneWayT.id >> Binding.addLazy (=) >> Binding.mapModel (fun m -> m.TimeType = Clock.Utc))
member _.SetUtc = base.Get() (Binding.CmdT.setAlways (Clock.SetTimeType Clock.Utc))
module CounterWithClock =
type Model =
{ Counter: Counter.Model
Clock: Clock.Model }
module ModelM =
module Counter =
let get m = m.Counter
module Clock =
let get m = m.Clock
let init () =
{ Counter = Counter.init
Clock = Clock.init () }
type Msg =
| CounterMsg of Counter.Msg
| ClockMsg of Clock.Msg
let update msg m =
match msg with
| CounterMsg msg -> { m with Counter = Counter.update msg m.Counter }
| ClockMsg msg -> { m with Clock = Clock.update msg m.Clock }
type [<AllowNullLiteral>] CounterWithClockViewModel (args) =
inherit ViewModelBase<CounterWithClock.Model, CounterWithClock.Msg>(args)
new() = CounterWithClockViewModel(CounterWithClock.init () |> ViewModelArgs.simple)
member _.Counter = base.Get() (Binding.SubModelT.req CounterViewModel >> Binding.mapModel CounterWithClock.ModelM.Counter.get >> Binding.mapMsg CounterWithClock.CounterMsg)
member _.Clock = base.Get() (Binding.SubModelT.req ClockViewModel >> Binding.mapModel CounterWithClock.ModelM.Clock.get >> Binding.mapMsg CounterWithClock.ClockMsg)
module App2 =
type Model =
{ ClockCounter1: CounterWithClock.Model
ClockCounter2: CounterWithClock.Model }
module ModelM =
module ClockCounter1 =
let get m = m.ClockCounter1
module ClockCounter2 =
let get m = m.ClockCounter2
let init () =
{ ClockCounter1 = CounterWithClock.init ()
ClockCounter2 = CounterWithClock.init () }
type Msg =
| ClockCounter1Msg of CounterWithClock.Msg
| ClockCounter2Msg of CounterWithClock.Msg
let update msg m =
match msg with
| ClockCounter1Msg msg ->
{ m with ClockCounter1 = CounterWithClock.update msg m.ClockCounter1 }
| ClockCounter2Msg msg ->
{ m with ClockCounter2 = CounterWithClock.update msg m.ClockCounter2 }
type [<AllowNullLiteral>] AppViewModel (args) =
inherit ViewModelBase<App2.Model, App2.Msg>(args)
new() = AppViewModel(App2.init () |> ViewModelArgs.simple)
member _.ClockCounter1 = base.Get() (Binding.SubModelT.req CounterWithClockViewModel >> Binding.mapModel App2.ModelM.ClockCounter1.get >> Binding.mapMsg App2.ClockCounter1Msg)
member _.ClockCounter2 = base.Get() (Binding.SubModelT.req CounterWithClockViewModel >> Binding.mapModel App2.ModelM.ClockCounter2.get >> Binding.mapMsg App2.ClockCounter2Msg)
module Program =
let timerTick dispatch =
let timer = new System.Timers.Timer(1000.)
timer.Elapsed.Add (fun _ ->
let clockMsg =
DateTimeOffset.Now
|> Clock.Tick
|> CounterWithClock.ClockMsg
dispatch <| App2.ClockCounter1Msg clockMsg
dispatch <| App2.ClockCounter2Msg clockMsg
)
timer.Start()
let main window =
let logger =
LoggerConfiguration()
.MinimumLevel.Override("Elmish.WPF.Update", Events.LogEventLevel.Verbose)
.MinimumLevel.Override("Elmish.WPF.Bindings", Events.LogEventLevel.Verbose)
.MinimumLevel.Override("Elmish.WPF.Performance", Events.LogEventLevel.Verbose)
.WriteTo.Console()
.CreateLogger()
WpfProgram.mkSimpleT App2.init App2.update AppViewModel
|> WpfProgram.withSubscription (Sub.fromV3Subscription "sub" (fun _ -> Cmd.ofEffect timerTick))
|> WpfProgram.withLogger (new SerilogLoggerFactory(logger))
|> WpfProgram.startElmishLoop window