1
1
module React.Redux
2
2
( ReduxReactClass
3
- , Effects
3
+ , ReduxEffect
4
4
, REDUX
5
5
, Reducer
6
+ , ReducerForeign
6
7
, Enhancer
7
- , Enhancer_
8
+ , EnhancerForeign
9
+ , Middleware
10
+ , MiddlewareAPI
8
11
, Store
9
12
, Spec
10
13
, Render
@@ -24,15 +27,17 @@ module React.Redux
24
27
, reducerOptic
25
28
, spec
26
29
, spec'
30
+ , applyMiddleware
31
+ , fromEnhancerForeign
27
32
) where
28
33
29
- import Prelude (Unit , (<<<), ( >>=), (<$), const , pure , id , unit )
34
+ import Prelude (Unit , (>>=), (<$), const , pure , id , unit )
30
35
31
36
import Control.Monad.Eff (Eff )
32
37
import Control.Monad.Eff.Class (class MonadEff , liftEff )
33
38
34
39
import Data.Either (Either , either )
35
- import Data.Function.Uncurried (Fn2 , Fn3 , mkFn2 , runFn2 , runFn3 )
40
+ import Data.Function.Uncurried (Fn2 , Fn3 , runFn2 , runFn3 )
36
41
import Data.Lens (Getter' , Lens' , Prism' , matching , set , view )
37
42
38
43
import Unsafe.Coerce (unsafeCoerce )
@@ -41,11 +46,17 @@ import React as React
41
46
42
47
type Reducer action state = action -> state -> state
43
48
44
- type Enhancer eff action state = ( Reducer action state -> state -> Eff ( Effects eff ) ( Store action state )) -> ( Reducer action state -> state -> Eff ( Effects eff ) ( Store action state ))
49
+ type ReducerForeign action state = Fn2 action state state
45
50
46
- type Enhancer_ eff action state = (Fn2 ( Reducer action state ) state ( Eff (Effects eff ) (Store action state ))) -> (Fn2 ( Reducer action state ) state ( Eff (Effects eff ) (Store action state ) ))
51
+ type Enhancer eff action state = (Reducer action state -> state -> Eff (ReduxEffect eff ) (Store action state )) -> (Reducer action state -> state -> Eff (ReduxEffect eff ) (Store action state ))
47
52
48
- type Effects eff = (redux :: REDUX | eff )
53
+ type EnhancerForeign action state = (Fn2 (ReducerForeign action state ) state (Store action state )) -> (Fn2 (ReducerForeign action state ) state (Store action state ))
54
+
55
+ type Middleware eff action state result = MiddlewareAPI eff action state result -> (action -> Eff (ReduxEffect eff ) action ) -> action -> Eff (ReduxEffect eff ) result
56
+
57
+ type MiddlewareAPI eff action state result = { getState :: Eff (ReduxEffect eff ) state , dispatch :: action -> Eff (ReduxEffect eff ) result }
58
+
59
+ type ReduxEffect eff = (redux :: REDUX | eff )
49
60
50
61
type Render props state eff f action = (f action -> f action ) -> React.Render props state eff
51
62
@@ -78,11 +89,7 @@ type Spec props state eff f action =
78
89
, componentWillUnmount :: ComponentWillUnmount props state eff f action
79
90
}
80
91
81
- spec ::
82
- forall props state eff f action .
83
- GetInitialState props state eff f action ->
84
- Render props state eff f action ->
85
- Spec props state eff f action
92
+ spec :: forall props state eff f action . GetInitialState props state eff f action -> Render props state eff f action -> Spec props state eff f action
86
93
spec getInitialState render =
87
94
{ render: render
88
95
, displayName: " "
@@ -99,7 +106,7 @@ spec getInitialState render =
99
106
spec' :: forall props eff f action . Render props Unit eff f action -> Spec props Unit eff f action
100
107
spec' = spec (\_ _ -> pure unit)
101
108
102
- createClass :: forall props state eff f action action' state' . MonadEff (Effects eff ) f => Getter' action' action -> Getter' state' props -> Spec props state eff f action' -> ReduxReactClass state' props
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
103
110
createClass actionLens stateLens spec_ = connect (view stateLens) reactClass
104
111
where
105
112
reactClass :: React.ReactClass props
@@ -125,11 +132,11 @@ createProviderElement store reduxClass = React.createElement providerClass { sto
125
132
createElement :: forall props state' . ReduxReactClass state' props -> React.ReactElement
126
133
createElement reduxClass = React .createElement (unsafeCoerce reduxClass) (unsafeCoerce unit) []
127
134
128
- createStore :: forall eff action state . Reducer action state -> state -> Eff (Effects eff ) (Store action state )
135
+ createStore :: forall eff action state . Reducer action state -> state -> Eff (ReduxEffect eff ) (Store action state )
129
136
createStore reducer state = createStore' reducer state id
130
137
131
- createStore' :: forall eff action state . Reducer action state -> state -> Enhancer eff action state -> Eff (Effects eff ) (Store action state )
132
- createStore' reducer state enhancer = runFn3 createStore_ reducer state (mkFn2 <<< enhancer <<< runFn2)
138
+ createStore' :: forall eff action state . Reducer action state -> state -> Enhancer eff action state -> Eff (ReduxEffect eff ) (Store action state )
139
+ createStore' = runFn3 createStore_
133
140
134
141
reducerOptic :: forall state state' action action' . Lens' state state' -> Prism' action action' -> Reducer action' state' -> Reducer action state
135
142
reducerOptic lens prism k action state = either (const state) (\a -> set lens (k a state') state) action'
@@ -148,8 +155,12 @@ foreign import data ReduxReactClass :: * -> * -> *
148
155
149
156
foreign import connect :: forall state' props . (state' -> props ) -> React.ReactClass props -> ReduxReactClass state' props
150
157
151
- foreign import dispatch_ :: forall eff props action state . Fn2 (React.ReactThis props state ) action (Eff (Effects eff ) action )
158
+ foreign import dispatch_ :: forall eff props action state . Fn2 (React.ReactThis props state ) action (Eff (ReduxEffect eff ) action )
152
159
153
160
foreign import providerClass :: forall action state' . React.ReactClass { store :: Store action state' }
154
161
155
- foreign import createStore_ :: forall eff action state . Fn3 (Reducer action state ) state (Enhancer_ eff action state ) (Eff (Effects eff ) (Store action state ))
162
+ foreign import createStore_ :: forall eff action state . Fn3 (Reducer action state ) state (Enhancer eff action state ) (Eff (ReduxEffect eff ) (Store action state ))
163
+
164
+ foreign import applyMiddleware :: forall eff action state result . Array (Middleware eff action state result ) -> Enhancer eff action state
165
+
166
+ foreign import fromEnhancerForeign :: forall eff action state . EnhancerForeign action state -> Enhancer eff action state
0 commit comments