1
1
module React.Redux
2
2
( ReduxReactClass
3
+ , ReduxReactClass'
3
4
, ReduxEffect
4
5
, REDUX
5
6
, Reducer
@@ -20,8 +21,10 @@ module React.Redux
20
21
, ComponentDidUpdate
21
22
, ComponentWillUnmount
22
23
, createClass
24
+ , createClass'
23
25
, createProviderElement
24
26
, createElement
27
+ , createElement'
25
28
, createStore
26
29
, createStore'
27
30
, reducerOptic
@@ -31,19 +34,22 @@ module React.Redux
31
34
, fromEnhancerForeign
32
35
) where
33
36
34
- import Prelude (Unit , (>>=), (<$ ), const , pure , id , unit )
37
+ import Prelude (Unit , (>>=), (<<< ), const , pure , id , unit )
35
38
36
39
import Control.Monad.Eff (Eff )
37
40
import Control.Monad.Eff.Class (class MonadEff , liftEff )
38
41
39
42
import Data.Either (Either , either )
40
43
import Data.Function.Uncurried (Fn2 , Fn3 , runFn2 , runFn3 )
41
- import Data.Lens (Getter' , Lens' , Prism' , matching , set , view )
44
+ import Data.Lens (Getter' , Lens' , Prism' , matching , set , to , view )
45
+ import Data.Tuple (Tuple (..), fst )
42
46
43
47
import Unsafe.Coerce (unsafeCoerce )
44
48
45
49
import React as React
46
50
51
+ type ReduxReactClass' state props = ReduxReactClass state Unit props
52
+
47
53
type Reducer action state = action -> state -> state
48
54
49
55
type ReducerForeign action state = Fn2 action state state
@@ -106,10 +112,13 @@ spec getInitialState render =
106
112
spec' :: forall props eff f action . Render props Unit eff f action -> Spec props Unit eff f action
107
113
spec' = spec (\_ _ -> pure unit)
108
114
109
- createClass :: forall props state eff f action action' state' . MonadEff (ReduxEffect eff ) f => Getter' action' action -> Getter' state' props -> Spec props state eff f action' -> ReduxReactClass state' props
110
- createClass actionLens stateLens spec_ = connect (view stateLens) reactClass
115
+ createProviderElement :: forall action props state . Store action state -> ReduxReactClass' state props -> React.ReactElement
116
+ createProviderElement store reduxClass = React .createElement providerClass { store: store } [ createElement' reduxClass [] ]
117
+
118
+ createClass :: forall eff f action props props' state state' . MonadEff (ReduxEffect eff ) f => Getter' (Tuple state props ) props' -> Spec props' state' eff f action -> ReduxReactClass state props props'
119
+ createClass slens spec_ = runFn3 connect_ Tuple (view slens) reactClass
111
120
where
112
- reactClass :: React.ReactClass props
121
+ reactClass :: React.ReactClass props'
113
122
reactClass =
114
123
React .createClass { render: \this -> spec_.render (dispatch this) this
115
124
, displayName: spec_.displayName
@@ -123,20 +132,29 @@ createClass actionLens stateLens spec_ = connect (view stateLens) reactClass
123
132
, componentWillUnmount: \this -> spec_.componentWillUnmount (dispatch this) this
124
133
}
125
134
where
126
- dispatch :: React.ReactThis props state -> f action' -> f action'
127
- dispatch this action' = action' >>= \a -> a <$ liftEff ( runFn2 dispatch_ this (view actionLens a))
135
+ dispatch :: React.ReactThis props' state' -> f action -> f action
136
+ dispatch this action = action >>= liftEff <<< runFn2 dispatch_ this
128
137
129
- createProviderElement :: forall props action state' . Store action state' -> ReduxReactClass state' props -> React.ReactElement
130
- createProviderElement store reduxClass = React .createElement providerClass { store: store } [ createElement reduxClass ]
138
+ createClass' :: forall eff f action props state . MonadEff (ReduxEffect eff ) f => Getter' state props -> Spec props Unit eff f action -> ReduxReactClass' state props
139
+ createClass' slens spec_ = createClass slens' spec_
140
+ where
141
+ slens' :: Getter' (Tuple state Unit ) props
142
+ slens' = to (view slens <<< fst)
143
+
144
+ createElement :: forall state props props' . ReduxReactClass state props props' -> props -> Array React.ReactElement -> React.ReactElement
145
+ createElement reduxClass = React .createElement reactClass
146
+ where
147
+ reactClass :: React.ReactClass props
148
+ reactClass = unsafeCoerce reduxClass
131
149
132
- createElement :: forall props state' . ReduxReactClass state' props -> React.ReactElement
133
- createElement reduxClass = React . createElement (unsafeCoerce reduxClass) (unsafeCoerce unit) []
150
+ createElement' :: forall state props . ReduxReactClass' state props -> Array React.ReactElement -> React.ReactElement
151
+ createElement' reduxClass = createElement reduxClass unit
134
152
135
- createStore :: forall eff action state . Reducer action state -> state -> Eff (ReduxEffect eff ) (Store action state )
136
- createStore reducer state = createStore' reducer state id
153
+ createStore :: forall eff action state . Reducer action state -> state -> Enhancer eff action state -> Eff (ReduxEffect eff ) (Store action state )
154
+ createStore = runFn3 createStore_
137
155
138
- createStore' :: forall eff action state . Reducer action state -> state -> Enhancer eff action state -> Eff (ReduxEffect eff ) (Store action state )
139
- createStore' = runFn3 createStore_
156
+ createStore' :: forall eff action state . Reducer action state -> state -> Eff (ReduxEffect eff ) (Store action state )
157
+ createStore' reducer state = createStore reducer state id
140
158
141
159
reducerOptic :: forall state state' action action' . Lens' state state' -> Prism' action action' -> Reducer action' state' -> Reducer action state
142
160
reducerOptic lens prism k action state = either (const state) (\a -> set lens (k a state') state) action'
@@ -151,13 +169,13 @@ foreign import data REDUX :: !
151
169
152
170
foreign import data Store :: * -> * -> *
153
171
154
- foreign import data ReduxReactClass :: * -> * -> *
172
+ foreign import data ReduxReactClass :: * -> * -> * -> *
155
173
156
- foreign import connect :: forall state' props . (state' -> props ) -> React.ReactClass props -> ReduxReactClass state' props
174
+ foreign import connect_ :: forall state props props' . Fn3 (state -> props -> Tuple state props ) ( Tuple state props -> props' ) ( React.ReactClass props' ) ( ReduxReactClass state props props' )
157
175
158
- foreign import dispatch_ :: forall eff props action state . Fn2 (React.ReactThis props state ) action (Eff (ReduxEffect eff ) action )
176
+ foreign import dispatch_ :: forall eff action props state . Fn2 (React.ReactThis props state ) action (Eff (ReduxEffect eff ) action )
159
177
160
- foreign import providerClass :: forall action state' . React.ReactClass { store :: Store action state' }
178
+ foreign import providerClass :: forall action state . React.ReactClass { store :: Store action state }
161
179
162
180
foreign import createStore_ :: forall eff action state . Fn3 (Reducer action state ) state (Enhancer eff action state ) (Eff (ReduxEffect eff ) (Store action state ))
163
181
0 commit comments