-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathHook.purs
229 lines (188 loc) · 4.54 KB
/
Hook.purs
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
module React.Hook
( Hook
, HookInput
, hookInput
, useState
, useStateLazy
, setState
, modifyState
, SetState
, useEffect
, useContext
, useReducer
, useReducerLazy
, dispatch
, Dispatch
, useCallback
, useMemo
, useRef
, useImperativeMethods
, useLayoutEffect
) where
import Prelude
import Data.Function.Uncurried (Fn1, Fn2, Fn3, Fn4, mkFn2, runFn1, runFn2, runFn3, runFn4)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable)
import Data.Nullable as Nullable
import Data.Tuple (Tuple(..))
import Effect (Effect)
import Effect.Uncurried (EffectFn1, runEffectFn1)
import Unsafe.Coerce (unsafeCoerce)
import React.Context (Context)
import React.Ref (Ref)
useState
:: forall a
. a
-> Hook (Tuple a (SetState a))
useState = runFn2 useState_ Tuple
useStateLazy
:: forall a
. (Unit -> a)
-> Hook (Tuple a (SetState a))
useStateLazy = runFn2 useState_ Tuple
setState
:: forall a
. SetState a
-> a
-> Effect Unit
setState k = runEffectFn1 k'
where
k' :: EffectFn1 a Unit
k' = unsafeCoerce k
modifyState
:: forall a
. SetState a
-> (a -> a)
-> Effect Unit
modifyState k = runEffectFn1 k'
where
k' :: EffectFn1 (a -> a) Unit
k' = unsafeCoerce k
foreign import data SetState :: Type -> Type
foreign import useState_
:: forall a b
. Fn2 (b -> SetState b -> Tuple b (SetState b))
a
(Hook (Tuple b (SetState b)))
useEffect
:: forall a
. Effect (Effect a)
-> Maybe (Array HookInput)
-> Hook Unit
useEffect k = runFn2 useEffect_ k <<< Nullable.toNullable
foreign import useEffect_
:: forall a
. Fn2 (Effect (Effect a))
(Nullable (Array HookInput))
(Hook Unit)
useContext :: forall a. Context a -> Hook a
useContext = runFn1 useContext_
foreign import useContext_
:: forall a
. Fn1 (Context a)
(Hook a)
useReducer
:: forall a b
. (a -> b -> a)
-> a
-> Hook (Tuple a (Dispatch a b))
useReducer = runFn3 useReducer_ Tuple <<< mkFn2
useReducerLazy
:: forall a b
. (a -> b -> a)
-> a
-> b
-> Hook (Tuple a (Dispatch a b))
useReducerLazy = runFn4 useReducerLazy_ Tuple <<< mkFn2
dispatch
:: forall a b
. Dispatch a b
-> b
-> Effect Unit
dispatch k = runEffectFn1 k'
where
k' :: EffectFn1 b Unit
k' = unsafeCoerce k
foreign import data Dispatch :: Type -> Type -> Type
foreign import useReducer_
:: forall a b
. Fn3 (a -> Dispatch a b -> Tuple a (Dispatch a b))
(Fn2 a b a)
a
(Hook (Tuple a (Dispatch a b)))
foreign import useReducerLazy_
:: forall a b
. Fn4 (a -> Dispatch a b -> Tuple a (Dispatch a b))
(Fn2 a b a)
a
b
(Hook (Tuple a (Dispatch a b)))
useCallback
:: forall a b
. (a -> b)
-> Maybe (Array HookInput)
-> Hook (a -> b)
useCallback k = runFn2 useCallback_ k <<< Nullable.toNullable
foreign import useCallback_
:: forall a b
. Fn2 (a -> b)
(Nullable (Array HookInput))
(Hook (a -> b))
useMemo
:: forall a
. (Unit -> a)
-> Maybe (Array HookInput)
-> Hook a
useMemo k = runFn2 useMemo_ k <<< Nullable.toNullable
foreign import useMemo_
:: forall a
. Fn2 (Unit -> a)
(Nullable (Array HookInput))
(Hook a)
useRef :: forall a. Maybe a -> Hook (Ref a)
useRef = runFn1 useRef_ <<< Nullable.toNullable
foreign import useRef_
:: forall a
. Fn1 (Nullable a)
(Hook (Ref a))
useImperativeMethods
:: forall a
. Ref a
-> (Unit -> a)
-> Maybe (Array HookInput)
-> Hook Unit
useImperativeMethods a k = runFn3 useImperativeMethods_ a k <<< Nullable.toNullable
foreign import useImperativeMethods_
:: forall a
. Fn3 (Ref a)
(Unit -> a)
(Nullable (Array HookInput))
(Hook Unit)
useLayoutEffect
:: forall a
. Effect (Effect a)
-> Maybe (Array HookInput)
-> Hook Unit
useLayoutEffect k = runFn2 useLayoutEffect_ k <<< Nullable.toNullable
foreign import useLayoutEffect_
:: forall a
. Fn2 (Effect (Effect a))
(Nullable (Array HookInput))
(Hook Unit)
foreign import data Hook :: Type -> Type
instance functorHook :: Functor Hook where
map k = hook <<< k <<< unHook
instance applyHook :: Apply Hook where
apply k fa = hook (unHook k (unHook fa))
instance applicativeHook :: Applicative Hook where
pure = hook
instance bindHook :: Bind Hook where
bind fa k = k (unHook fa)
instance monadHook :: Monad Hook
unHook :: forall a. Hook a -> a
unHook = unsafeCoerce
hook :: forall a. a -> Hook a
hook = unsafeCoerce
foreign import data HookInput :: Type
hookInput :: forall a. a -> HookInput
hookInput = unsafeCoerce