-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathReactiveElmishViewModel.fs
421 lines (380 loc) · 19.7 KB
/
ReactiveElmishViewModel.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
namespace ReactiveElmish
open System.ComponentModel
open System.Reactive.Linq
open System
open System.Linq
open System.Collections.Generic
open System.Runtime.CompilerServices
open System.Runtime.InteropServices
open DynamicData
open System.Collections.ObjectModel
open ReactiveUI
[<AutoOpen>]
module private Utils =
let readOnlyCollection<'T>() =
new ReadOnlyObservableCollection<'T>(new ObservableCollection<'T>())
[<AllowNullLiteral>]
type ReactiveElmishViewModel(onPropertyChanged: string -> unit) =
inherit ReactiveUI.ReactiveObject()
let disposables = ResizeArray<IDisposable>()
let propertySubscriptions = Dictionary<string, IDisposable>()
let propertyCollections = Dictionary<string, obj>()
new() as this =
let opc propertyName = this.RaisePropertyChanged(propertyName)
new ReactiveElmishViewModel(opc)
/// Fires the `PropertyChanged` event for the given property name. Uses the caller's name if no property name is given.
member this.OnPropertyChanged([<CallerMemberName; Optional; DefaultParameterValue("")>] ?propertyName: string) =
this.RaisePropertyChanged(propertyName.Value)
/// Binds a VM property to a `modelProjection` value and refreshes the VM property when the `modelProjection` value changes.
member this.Bind<'Model, 'ModelProjection>(
store: IStore<'Model>,
modelProjection: 'Model -> 'ModelProjection,
[<CallerMemberName; Optional; DefaultParameterValue("")>] ?vmPropertyName
) =
let vmPropertyName = vmPropertyName.Value
if not (propertySubscriptions.ContainsKey vmPropertyName) then
// Creates a subscription to the 'Model projection and stores it in a dictionary.
let disposable =
store.Observable
.DistinctUntilChanged(modelProjection)
.Subscribe(fun _ ->
// Alerts the view that the 'Model projection / VM property has changed.
onPropertyChanged(vmPropertyName)
#if DEBUG
printfn $"PropertyChanged: {vmPropertyName}"
#endif
)
propertySubscriptions.Add(vmPropertyName, disposable)
// Returns the initial value from the model projection.
modelProjection store.Model
/// Binds a VM property to a `modelProjection` value and refreshes the VM property when the `onChanged` value changes.
/// The `modelProjection` function will only be called when the `onChanged` value changes.
/// `onChanged` usually returns a property value or a tuple of property values.
member this.BindOnChanged<'Model, 'OnChanged, 'ModelProjection>(
store: IStore<'Model>,
onChanged: 'Model -> 'OnChanged,
modelProjection: 'Model -> 'ModelProjection,
[<CallerMemberName; Optional; DefaultParameterValue("")>] ?vmPropertyName
) =
let vmPropertyName = vmPropertyName.Value
if not (propertySubscriptions.ContainsKey vmPropertyName) then
// Creates a subscription to the 'Model projection and stores it in a dictionary.
let disposable =
store.Observable
.DistinctUntilChanged(onChanged)
.Subscribe(fun x ->
// Alerts the view that the 'Model projection / VM property has changed.
onPropertyChanged(vmPropertyName)
#if DEBUG
printfn $"PropertyChanged: {vmPropertyName}"
#endif
)
propertySubscriptions.Add(vmPropertyName, disposable)
modelProjection store.Model
/// <summary>
/// Binds a model colleciton property to a DynamicData SourceList.
/// </summary>
/// <param name="store">The reactive store to bind to.</param>
/// <param name="modelProjectionSeq">The model projection.</param>
member this.BindList<'Model, 'ModelProjection>(
store: IStore<'Model>,
modelProjectionSeq: 'Model -> 'ModelProjection seq,
?sortBy: Func<'ModelProjection, IComparable>,
[<CallerMemberName; Optional; DefaultParameterValue("")>] ?vmPropertyName
) =
if not (propertyCollections.ContainsKey vmPropertyName.Value) then
let mutable readOnlyList = readOnlyCollection<'ModelProjection>()
let sourceList = SourceList.createFrom (modelProjectionSeq store.Model)
sourceList
.Connect()
|> fun x ->
match sortBy with
| Some sortBy -> x.Sort(DynamicData.Binding.SortExpressionComparer.Ascending(sortBy))
| None -> x.Sort(Comparer.Create(fun _ _ -> 0))
|> _.Bind(&readOnlyList)
.Subscribe()
|> this.AddDisposable
store.Observable
.Select(modelProjectionSeq)
//.DistinctUntilChanged()
.Subscribe(sourceList.EditDiff)
|> this.AddDisposable
this.AddDisposable(sourceList)
propertyCollections.Add(vmPropertyName.Value, readOnlyList)
readOnlyList
else
propertyCollections[vmPropertyName.Value] :?> ReadOnlyObservableCollection<'ModelProjection>
/// <summary>
/// Binds a model colleciton property to a DynamicData SourceList.
/// </summary>
/// <param name="store">The reactive store to bind to.</param>
/// <param name="modelProjectionSeq">The model projection.</param>
/// <param name="map">A function that transforms each item in the collection when it is added to the SourceList.</param>
member this.BindList<'Model, 'ModelProjection, 'Mapped>(
store: IStore<'Model>,
modelProjectionSeq: 'Model -> 'ModelProjection seq,
map: 'ModelProjection -> 'Mapped,
?sortBy: Func<'Mapped, IComparable>,
[<CallerMemberName; Optional; DefaultParameterValue("")>] ?vmPropertyName
) =
let vmPropertyName = vmPropertyName.Value
if not (propertyCollections.ContainsKey vmPropertyName) then
let mutable readOnlyList = readOnlyCollection<'Mapped>()
let sourceList = SourceList.createFrom (modelProjectionSeq store.Model)
sourceList
.Connect()
.Transform(map)
|> fun x ->
match sortBy with
| Some sortBy -> x.Sort(DynamicData.Binding.SortExpressionComparer.Ascending(sortBy))
| None -> x.Sort(Comparer.Create(fun _ _ -> 0))
|> _.Bind(&readOnlyList)
.Subscribe()
|> this.AddDisposable
store.Observable
.Select(modelProjectionSeq)
//.DistinctUntilChanged()
.Subscribe(sourceList.EditDiff)
|> this.AddDisposable
this.AddDisposable(sourceList)
propertyCollections.Add(vmPropertyName, readOnlyList)
readOnlyList
else
propertyCollections[vmPropertyName] :?> ReadOnlyObservableCollection<'Mapped>
/// <summary>
/// Binds a model Map<'Key, 'Value> property to an ObservableCollection.
/// </summary>
/// <param name="store">The reactive store to bind to.</param>
/// <param name="modelProjection">The model projection.</param>
/// <param name="map">A function that transforms the item when it is added.</param>
/// <param name="getKey">A function that returns the identifier of the item.</param>
/// <param name="update">An optional function that updates the transformed item when it is updated in the model. NOTE: This is expensive as it requires all items to be compared.</param>
/// <param name="sortBy">An optional function that returns a sort expression.</param>
member this.BindKeyedList<'Model, 'Key, 'Value, 'Mapped when 'Value : equality and 'Mapped : not struct and 'Key : comparison>(
store: IStore<'Model>,
modelProjection: 'Model -> Map<'Key, 'Value>,
map: 'Value -> 'Mapped,
getKey: 'Mapped -> 'Key,
?update: Action<'Value, 'Mapped>,
?sortBy: Func<'Mapped, IComparable>,
[<CallerMemberName; Optional; DefaultParameterValue("")>] ?vmPropertyName
) =
let vmPropertyName = vmPropertyName.Value
if not (propertyCollections.ContainsKey vmPropertyName) then
let mutable lastModelMap: Map<'Key, 'Value> = Map.empty
let mutable observableCollection = ObservableCollection()
let mapToObservableCollection (currentModelMap: Map<'Key, 'Value>) =
// Apply updates to existing items (excluding new or removed items)
update
|> Option.iter (fun update ->
let existingItemVms =
// Get items from the lastModelMap that exist in the currentModelMap
observableCollection
|> Seq.map (fun item -> getKey item, item)
|> Seq.filter (fun (key, _) ->
// Only include items that exist in the currentModelMap
currentModelMap.ContainsKey key
)
|> Seq.toArray
for idx = existingItemVms.Length - 1 downto 0 do
let (key, itemVM) = existingItemVms[idx]
let newItem = currentModelMap[key]
let oldItem = lastModelMap[key]
if newItem <> oldItem then
update.Invoke(newItem, itemVM)
)
// Get items from the currentModelMap that are missing from the lastModelMap
let newItems =
currentModelMap
|> Map.filter (fun k _ -> not (lastModelMap.ContainsKey k))
|> Map.toSeq
// Add new items to the observableCollection
newItems
|> Seq.map (snd >> map)
|> Seq.iter observableCollection.Add
// Get items that have been removed from the currentModelMap
let removedKeys =
lastModelMap
|> Map.filter (fun k _ -> not (currentModelMap.ContainsKey k))
|> Map.toArray
|> Seq.map fst
|> Set.ofSeq
if removedKeys.Count > 0 then
let indexesToRemove =
observableCollection
|> Seq.mapi (fun idx item -> idx, getKey item)
|> Seq.filter (fun (_, key) -> removedKeys.Contains(key))
|> Seq.map fst
|> Set.ofSeq
for idx = observableCollection.Count - 1 downto 0 do
if indexesToRemove.Contains(idx) then
let removedItem = observableCollection[idx]
observableCollection.RemoveAt(idx)
match removedItem :> obj with
| :? IDisposable as disposable ->
this.AddDisposable(disposable)
| _ -> ()
// Sort the observableCollection
sortBy
|> Option.iter (fun sortBy ->
observableCollection
|> Seq.sortBy sortBy.Invoke
|> Seq.iteri (fun idx item -> observableCollection[idx] <- item)
)
// Finally, update the lastModelMap
lastModelMap <- currentModelMap
// Create an initial observable collection from the modelProjection
modelProjection store.Model |> mapToObservableCollection
// Creates a subscription to a SourceCache and stores it in a dictionary.
let disposable =
store.Observable
.Select(modelProjection)
.DistinctUntilChanged()
.Subscribe(mapToObservableCollection)
this.AddDisposable disposable
propertyCollections.Add(vmPropertyName, observableCollection)
observableCollection
else
propertyCollections[vmPropertyName] :?> ObservableCollection<'Mapped>
/// <summary>
/// Binds a model Map<'Key, 'Value> property to an ObservableCollection.
/// </summary>
/// <param name="store">The reactive store to bind to.</param>
/// <param name="modelProjection">The model projection.</param>
/// <param name="getKey">A function that returns the identifier of the item.</param>
/// <param name="update">An optional function that updates the transformed item when it is updated in the model. NOTE: This is expensive as it requires all items to be compared.</param>
/// <param name="sortBy">A function that returns a sort expression.</param>
member this.BindKeyedList<'Model, 'Key, 'Value when 'Value: equality and 'Value : not struct and 'Key : comparison>(
store: IStore<'Model>,
modelProjection: 'Model -> Map<'Key, 'Value>,
getKey: 'Value -> 'Key,
?update: Action<'Value, 'Value>,
?sortBy,
[<CallerMemberName; Optional; DefaultParameterValue("")>] ?vmPropertyName: string
) =
this.BindKeyedList(store = store, modelProjection = modelProjection, map = id, getKey = getKey, ?update = update, ?sortBy = sortBy, ?vmPropertyName = vmPropertyName)
/// Binds a VM property to a 'Model DynamicData.ISourceList<'T> property.
member this.BindSourceList<'T>(
sourceList: ISourceList<'T>,
[<CallerMemberName; Optional; DefaultParameterValue("")>] ?vmPropertyName
) =
let vmPropertyName = vmPropertyName.Value
if not (propertyCollections.ContainsKey vmPropertyName) then
let mutable readOnlyList = readOnlyCollection<'T>()
// Creates a subscription to a ISourceList<'T> and stores it in a dictionary.
let disposable =
sourceList
.Connect()
.Bind(&readOnlyList)
.Subscribe()
this.AddDisposable disposable
propertyCollections.Add(vmPropertyName, readOnlyList)
readOnlyList
else
propertyCollections[vmPropertyName] :?> ReadOnlyObservableCollection<'T>
/// Binds a VM property to a 'Model DynamicData.ISourceList<'T> property.
member this.BindSourceList<'T, 'Mapped>(
sourceList: ISourceList<'T>,
map: 'T -> 'Mapped,
[<CallerMemberName; Optional; DefaultParameterValue("")>] ?vmPropertyName
) =
let vmPropertyName = vmPropertyName.Value
if not (propertyCollections.ContainsKey vmPropertyName) then
let mutable readOnlyList = readOnlyCollection<'Mapped>()
// Creates a subscription to a ISourceList<'T> and stores it in a dictionary.
let disposable =
sourceList
.Connect()
.Transform(map)
.Bind(&readOnlyList)
.Subscribe()
this.AddDisposable disposable
propertyCollections.Add(vmPropertyName, readOnlyList)
readOnlyList
else
propertyCollections[vmPropertyName] :?> ReadOnlyObservableCollection<'Mapped>
/// Binds a VM property to a 'Model DynamicData.IObservableCache<'Value, 'Key> property.
member this.BindSourceCache<'Value, 'Key>(
sourceCache: IObservableCache<'Value, 'Key>,
?sortBy: Func<'Value, IComparable>,
[<CallerMemberName; Optional; DefaultParameterValue("")>] ?vmPropertyName
) =
let vmPropertyName = vmPropertyName.Value
if not (propertyCollections.ContainsKey vmPropertyName) then
let mutable readOnlyList = readOnlyCollection<'Value>()
// Creates a subscription to a SourceCache and stores it in a dictionary.
let disposable =
sourceCache
.Connect()
|> fun x ->
match sortBy with
| Some sortBy ->
x.SortBy(sortBy)
| None ->
x.Sort(Comparer.Create(fun _ _ -> 0))
|> _.Bind(&readOnlyList)
|> _.Subscribe()
this.AddDisposable disposable
propertyCollections.Add(vmPropertyName, readOnlyList)
readOnlyList
else
propertyCollections[vmPropertyName] :?> ReadOnlyObservableCollection<'Value>
/// <summary>
/// Binds a VM property to a 'Model DynamicData.IObservableCache<'Value, 'Key> property.
/// </summary>
/// <param name="sourceCache">A SourceCache to bind.</param>
/// <param name="map">A function that transforms the item when it is added.</param>
/// <param name="update">An optional function that updates the transformed item when it is updated in the model.</param>
/// <param name="sortBy">An optional function that returns a sort expression.</param>
member this.BindSourceCache<'Value, 'Key, 'Mapped when 'Value : not struct and 'Mapped : not struct>(
sourceCache: IObservableCache<'Value, 'Key>,
map: 'Value -> 'Mapped,
?filter: Func<'Value, bool>,
?update: Action<'Value, 'Mapped>,
?sortBy,
[<CallerMemberName; Optional; DefaultParameterValue("")>] ?vmPropertyName
) =
let vmPropertyName = vmPropertyName.Value
if not (propertyCollections.ContainsKey vmPropertyName) then
let mutable readOnlyList = readOnlyCollection<'Mapped>()
// Creates a subscription to a SourceCache and stores it in a dictionary.
let disposable =
sourceCache
.Connect()
|> fun x ->
match filter with
| Some filter ->
x.Filter(filter)
| None ->
x
|> fun x ->
match update with
| Some update ->
x.TransformWithInlineUpdate(map, fun mapped value -> update.Invoke(value, mapped))
| None ->
x.Transform(map)
|> fun x ->
match sortBy with
| Some sortBy ->
x.SortBy(sortBy)
| None ->
x.Sort(Comparer.Create(fun _ _ -> 0))
|> _.Bind(&readOnlyList)
|> _.Subscribe()
this.AddDisposable disposable
propertyCollections.Add(vmPropertyName, readOnlyList)
readOnlyList
else
propertyCollections[vmPropertyName] :?> ReadOnlyObservableCollection<'Mapped>
/// Subscribes to an IObservable<> and adds the subscription to the list of disposables.
member this.Subscribe(observable: IObservable<'T>, handler: 'T -> unit) =
observable
|> Observable.subscribe handler
|> this.AddDisposable
member this.AddDisposable(disposable: IDisposable) =
disposables.Add(disposable)
interface IDisposable with
member this.Dispose() =
disposables |> Seq.iter _.Dispose()
propertySubscriptions.Values |> Seq.iter _.Dispose()
propertySubscriptions.Clear()