-
Notifications
You must be signed in to change notification settings - Fork 21
/
common.fs
83 lines (70 loc) · 2.93 KB
/
common.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
namespace Elmish.React
open System
open Fable.Import.React
open Fable.Helpers.React
open Fable.Core
open Elmish
type [<Pojo>] LazyProps<'model> = {
model:'model
render:unit->ReactElement
equal:'model->'model->bool
}
module Components =
type LazyView<'model>(props) =
inherit Component<LazyProps<'model>,obj>(props)
override this.shouldComponentUpdate(nextProps, _nextState) =
not <| this.props.equal this.props.model nextProps.model
override this.render () =
this.props.render ()
[<AutoOpen>]
module Common =
/// Avoid rendering the view unless the model has changed.
/// equal: function to compare the previous and the new states
/// view: function to render the model
/// state: new state to render
let lazyViewWith (equal:'model->'model->bool)
(view:'model->ReactElement)
(state:'model) =
ofType<Components.LazyView<_>,_,_>
{ render = fun () -> view state
equal = equal
model = state }
[]
/// Avoid rendering the view unless the model has changed.
/// equal: function to compare the previous and the new states
/// view: function to render the model using the dispatch
/// state: new state to render
/// dispatch: dispatch function
let lazyView2With (equal:'model->'model->bool)
(view:'model->'msg Dispatch->ReactElement)
(state:'model)
(dispatch:'msg Dispatch) =
ofType<Components.LazyView<_>,_,_>
{ render = fun () -> view state dispatch
equal = equal
model = state }
[]
/// Avoid rendering the view unless the model has changed.
/// equal: function to compare the previous and the new model (a tuple of two states)
/// view: function to render the model using the dispatch
/// state1: new state to render
/// state2: new state to render
/// dispatch: dispatch function
let lazyView3With (equal:_->_->bool) (view:_->_->_->ReactElement) state1 state2 (dispatch:'msg Dispatch) =
ofType<Components.LazyView<_>,_,_>
{ render = fun () -> view state1 state2 dispatch
equal = equal
model = (state1,state2) }
[]
/// Avoid rendering the view unless the model has changed.
/// view: function of model to render the view
let lazyView (view:'model->ReactElement) =
lazyViewWith (=) view
/// Avoid rendering the view unless the model has changed.
/// view: function of two arguments to render the model using the dispatch
let lazyView2 (view:'model->'msg Dispatch->ReactElement) =
lazyView2With (=) view
/// Avoid rendering the view unless the model has changed.
/// view: function of three arguments to render the model using the dispatch
let lazyView3 (view:_->_->_->ReactElement) =
lazyView3With (=) view