forked from purescript-contrib/purescript-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReact.purs
More file actions
318 lines (293 loc) · 11.1 KB
/
Copy pathReact.purs
File metadata and controls
318 lines (293 loc) · 11.1 KB
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
module React where
import Debug.Trace
import Control.Monad.Eff
foreign import data DOM :: !
foreign import data UI :: *
foreign import data UIRef :: # ! -> *
foreign import data EventHandler :: * -> *
foreign import data Disallowed :: !
foreign import data ReadAllowed :: !
foreign import data WriteAllowed :: !
foreign import data ReactState :: # ! -> * -> !
foreign import data ReactProps :: * -> !
foreign import data ReactRefs :: * -> !
foreign import noop0
"function noop0() { return null; }"
:: forall eff result. Eff ( eff ) result
foreign import noop1
"var noop1 = noop0"
:: forall a eff result. a -> Eff ( eff ) result
foreign import noop2
"var noop2 = noop0"
:: forall a b eff result. a -> b -> Eff ( eff ) result
type Render props refs state eff =
Eff (
props :: ReactProps props,
refs :: Disallowed,
state :: ReactState (read :: ReadAllowed) state
| eff
) UI
type UISpec props refs state eff1 eff2 eff3 eff4 eff5 eff6 eff7 eff8 =
{ getInitialState
:: Eff (
props :: ReactProps props,
state :: Disallowed,
refs :: Disallowed
| eff1
) state
, componentWillMount
:: Eff (
props :: ReactProps props,
state :: ReactState (read :: ReadAllowed, write :: WriteAllowed) state,
refs :: Disallowed
| eff2
) Unit
, componentDidMount
:: Eff (
props :: ReactProps props,
state :: ReactState (read :: ReadAllowed, write :: WriteAllowed) state,
refs :: ReactRefs refs
| eff3
) Unit
, componentWillReceiveProps
:: props
-> Eff (
props :: ReactProps props,
state :: ReactState (read :: ReadAllowed, write :: WriteAllowed) state,
refs :: ReactRefs refs
| eff4
) Unit
, shouldComponentUpdate
:: props
-> state
-> Eff (
props :: ReactProps props,
state :: ReactState (read :: ReadAllowed, write :: WriteAllowed) state,
refs :: ReactRefs refs
| eff5
) Boolean
, componentWillUpdate
:: props
-> state
-> Eff (
props :: ReactProps props,
state :: ReactState (read :: ReadAllowed, write :: WriteAllowed) state,
refs :: ReactRefs refs
| eff6
) Unit
, componentDidUpdate
:: props
-> state
-> Eff (
props :: ReactProps props,
state :: ReactState (read :: ReadAllowed) state,
refs :: ReactRefs refs
| eff7
) Unit
, componentWillUnmount
:: Eff (
props :: ReactProps props,
state :: ReactState (read :: ReadAllowed) state,
refs :: ReactRefs refs
| eff8
) Unit
}
spec
:: forall props refs state eff1 eff2 eff3 eff4 eff5 eff6 eff7 eff8.
UISpec props refs state eff1 eff2 eff3 eff4 eff5 eff6 eff7 eff8
spec =
{ getInitialState: noop0
, componentWillMount: noop0
, componentDidMount: noop0
, componentWillReceiveProps: noop1
, shouldComponentUpdate: updateAlways
, componentWillUpdate: noop2
, componentDidUpdate: noop2
, componentWillUnmount: noop0
}
where
updateAlways
:: forall props refs state. props
-> state
-> forall eff. Eff (
props :: ReactProps props,
state :: ReactState (read :: ReadAllowed, write :: WriteAllowed) state,
refs :: ReactRefs refs
| eff
) Boolean
updateAlways props state = return true
foreign import getProps
" function getProps() { \
\ return __current.props; \
\ }"
:: forall props eff.
Eff (props :: ReactProps props | eff) props
foreign import getRefs
" function getRefs() { \
\ return __current.refs; \
\ }"
:: forall refs eff.
Eff (refs :: ReactRefs refs | eff) refs
foreign import writeState
" function writeState(state) { \
\ __current.replaceState({state: state}); \
\ return function() { return state; } \
\ }"
:: forall state statePerms eff.
state
-> Eff (state :: ReactState (read :: ReadAllowed, write :: WriteAllowed | statePerms) state | eff) state
foreign import readState
" function readState() { \
\ return __current.state.state; \
\ }"
:: forall state statePerms eff.
Eff (state :: ReactState (read :: ReadAllowed | statePerms) state | eff) state
transformState f = do
state <- readState
writeState $ f state
foreign import getSelf
" function getSelf() { \
\ return __current; \
\ }"
:: forall eff.
Eff (eff) (UIRef eff)
foreign import runUI
" function runUI(ref) { \
\ return function(action) { \
\ return function() { \
\ if (ref.isMounted()) { \
\ __current = ref; \
\ try { \
\ return action(); \
\ } finally { \
\ __current = null; \
\ } \
\ } \
\ } \
\ } \
\ }"
:: forall refEff eff result. UIRef refEff -> Eff (refEff) result -> Eff (eff) result
foreign import mkUI
" var __current; \
\ function mkUI(ss) { \
\ return function(render) { \
\ var specs = {}; \
\ for (var s in ss) { \
\ if (ss.hasOwnProperty(s)) { \
\ specs[s] = (function(impl) { \
\ return function() { \
\ __current = this; \
\ try { \
\ return impl.apply(this, arguments); \
\ } finally { \
\ __current = null; \
\ } \
\ } \
\ })(ss[s]); \
\ } \
\ } \
\ specs.getInitialState= function() { \
\ __current = this; \
\ try { \
\ return {state: ss.getInitialState()}; \
\ } finally { \
\ __current = null; \
\ } \
\ }; \
\ specs.render = function() { \
\ __current = this; \
\ try { \
\ var ui = render.call(this); \
\ } finally { \
\ __current = null; \
\ } \
\ return ui; \
\ }; \
\ return React.createClass(specs); \
\ } \
\ }"
:: forall props refs state eff eff1 eff2 eff3 eff4 eff5 eff6 eff7 eff8.
UISpec props refs state eff1 eff2 eff3 eff4 eff5 eff6 eff7 eff8
-> Render props refs state eff
-> (props -> UI)
type DOMEvent = forall attrs. { | attrs}
type DOMEventTarget = forall attrs. { | attrs }
type Event = { bubbles :: Boolean
, cancelable :: Boolean
, currentTarget :: DOMEventTarget
, defaultPrevented :: Boolean
, eventPhase :: Number
, isTrusted :: Boolean
, nativeEvent :: DOMEvent
, preventDefault :: {} -> {}
, stopPropagation :: {} -> {}
, target :: DOMEventTarget
, timeStamp :: Number
, eventType :: String
}
type MouseEvent = { pageX :: Number, pageY :: Number }
type KeyboardEvent = { altKey :: Boolean
, ctrlKey :: Boolean
, charCode :: Number
, key :: String
, keyCode :: Number
, locale :: String
, location :: Number
, metaKey :: Boolean
, repeat :: Boolean
, shiftKey :: Boolean
, which :: Number
}
type EventHandlerContext eff props refs state result = forall statePerms. Eff (
props :: ReactProps props,
refs :: ReactRefs refs,
state :: ReactState (read :: ReadAllowed, write :: WriteAllowed | statePerms) state
| eff
) result
foreign import handle
" function handle(f) { \
\ var component = __current; \
\ return function(e) { \
\ __current = component; \
\ try { \
\ var res = f.call(__current, e)(); \
\ } finally { \
\ __current = null; \
\ } \
\ return res; \
\ } \
\ }"
:: forall eff ev props refs state result.
(ev -> EventHandlerContext eff props refs state result)
-> EventHandler ev
foreign import renderToString
"var renderToString = React.renderComponentToString"
:: UI -> String
foreign import renderToBody
" function renderToBody(component) { \
\ return function() { \
\ return React.renderComponent(component, document.body); \
\ } \
\ }"
:: forall eff. UI -> Eff (dom :: DOM | eff) UI
foreign import renderToElementById
" function renderToElementById(id) { \
\ return function(component) { \
\ return function() { \
\ return React.renderComponent(component, document.getElementById(id)); \
\ } \
\ } \
\ }"
:: forall eff. String -> UI -> Eff (dom :: DOM | eff) UI
foreign import deferred
"function deferred(action) {\
\ var component = __current;\
\ return function() {\
\ __current = component;\
\ try {\
\ return action();\
\ } finally {\
\ __current = null;\
\ }\
\ };\
\}" :: forall a eff. Eff eff a -> Eff eff a