Skip to content

Commit 7f12e47

Browse files
committed
Move render function into spec
1 parent 25081dc commit 7f12e47

File tree

5 files changed

+37
-40
lines changed

5 files changed

+37
-40
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ incrementCounter ctx e = do
3737
val <- readState ctx
3838
writeState ctx (val + 1)
3939
40-
counter = mkUI (spec 0) \ctx -> do
40+
counter = mkUI $ spec 0 \ctx -> do
4141
val <- readState ctx
4242
return $ D.p [ P.className "Counter"
4343
, P.onClick (incrementCounter ctx)

Diff for: docs/React.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,15 @@ A rendering function.
155155
#### `UISpec`
156156

157157
``` purescript
158-
type UISpec props state eff = { getInitialState :: UIRef -> Eff (props :: ReactProps props, state :: ReactState Disallowed state, refs :: ReactRefs Disallowed | eff) state, componentWillMount :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadWrite state, refs :: ReactRefs Disallowed | eff) Unit, componentDidMount :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadWrite state, refs :: ReactRefs ReadOnly | eff) Unit, componentWillReceiveProps :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadWrite state, refs :: ReactRefs ReadOnly | eff) Unit, shouldComponentUpdate :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadWrite state, refs :: ReactRefs ReadOnly | eff) Boolean, componentWillUpdate :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadWrite state, refs :: ReactRefs ReadOnly | eff) Unit, componentDidUpdate :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadOnly state, refs :: ReactRefs ReadOnly | eff) Unit, componentWillUnmount :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadOnly state, refs :: ReactRefs ReadOnly | eff) Unit }
158+
type UISpec props state eff = { render :: Render props state eff, getInitialState :: UIRef -> Eff (props :: ReactProps props, state :: ReactState Disallowed state, refs :: ReactRefs Disallowed | eff) state, componentWillMount :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadWrite state, refs :: ReactRefs Disallowed | eff) Unit, componentDidMount :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadWrite state, refs :: ReactRefs ReadOnly | eff) Unit, componentWillReceiveProps :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadWrite state, refs :: ReactRefs ReadOnly | eff) Unit, shouldComponentUpdate :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadWrite state, refs :: ReactRefs ReadOnly | eff) Boolean, componentWillUpdate :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadWrite state, refs :: ReactRefs ReadOnly | eff) Unit, componentDidUpdate :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadOnly state, refs :: ReactRefs ReadOnly | eff) Unit, componentWillUnmount :: UIRef -> Eff (props :: ReactProps props, state :: ReactState ReadOnly state, refs :: ReactRefs ReadOnly | eff) Unit }
159159
```
160160

161161
A specification of a component.
162162

163163
#### `spec`
164164

165165
``` purescript
166-
spec :: forall props state eff. state -> UISpec props state eff
166+
spec :: forall props state eff. state -> Render props state eff -> UISpec props state eff
167167
```
168168

169169
Create a component specification.
@@ -211,7 +211,7 @@ Transform the component state by applying a function.
211211
#### `mkUI`
212212

213213
``` purescript
214-
mkUI :: forall props state eff. UISpec props state eff -> Render props state eff -> props -> UI
214+
mkUI :: forall props state eff. UISpec props state eff -> props -> UI
215215
```
216216

217217
Create a component from a component spec.

Diff for: src/React.js

+14-19
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,22 @@ exports.readState = function(ctx) {
3535
};
3636

3737
exports.mkUI = function(ss) {
38-
return function(render) {
39-
var specs = {};
40-
for (var s in ss) {
41-
if (ss.hasOwnProperty(s)) {
42-
specs[s] = (function(impl) {
43-
return function() {
44-
return impl(this)();
45-
}
46-
})(ss[s]);
47-
}
38+
var result = {};
39+
for (var s in ss) {
40+
if (ss.hasOwnProperty(s)) {
41+
result[s] = (function(impl) {
42+
return function() {
43+
return impl(this)();
44+
}
45+
})(ss[s]);
4846
}
49-
specs.getInitialState = function() {
50-
return {
51-
state: ss.getInitialState(this)()
52-
};
53-
};
54-
specs.render = function() {
55-
return render(this)();
56-
};
57-
return React.createClass(specs);
5847
}
48+
result.getInitialState = function() {
49+
return {
50+
state: ss.getInitialState(this)()
51+
};
52+
};
53+
return React.createClass(result);
5954
};
6055

6156
exports.handle = function(f) {

Diff for: src/React.purs

+6-5
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ type Render props state eff =
142142

143143
-- | A specification of a component.
144144
type UISpec props state eff =
145-
{ getInitialState
145+
{ render :: Render props state eff
146+
, getInitialState
146147
:: UIRef ->
147148
Eff ( props :: ReactProps props
148149
, state :: ReactState Disallowed state
@@ -201,9 +202,10 @@ type UISpec props state eff =
201202
}
202203

203204
-- | Create a component specification.
204-
spec :: forall props state eff. state -> UISpec props state eff
205-
spec st =
206-
{ getInitialState: \_ -> pure st
205+
spec :: forall props state eff. state -> Render props state eff -> UISpec props state eff
206+
spec st render =
207+
{ render: render
208+
, getInitialState: \_ -> pure st
207209
, componentWillMount: \_ -> return unit
208210
, componentDidMount: \_ -> return unit
209211
, componentWillReceiveProps: \_ -> return unit
@@ -246,7 +248,6 @@ transformState ctx f = do
246248
-- | Create a component from a component spec.
247249
foreign import mkUI :: forall props state eff.
248250
UISpec props state eff ->
249-
Render props state eff ->
250251
props ->
251252
UI
252253

Diff for: test/Main.purs

+13-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ foreign import interval :: forall eff a.
1515
Eff eff a ->
1616
Eff eff Unit
1717

18-
hello = mkUI (spec unit) \ctx -> do
18+
hello = mkUI $ spec unit \ctx -> do
1919
props <- getProps ctx
2020
return $ D.h1 [ P.className "Hello"
2121
, P.style { background: "lightgray" }
@@ -24,23 +24,24 @@ hello = mkUI (spec unit) \ctx -> do
2424
, D.text props.name
2525
]
2626

27-
counter = mkUI counterSpec \ctx -> do
28-
val <- readState ctx
29-
return $ D.button [ P.className "Counter"
30-
, P.onClick \_ -> do
31-
val <- readState ctx
32-
writeState ctx (val + 1)
33-
]
34-
[ D.text (show val)
35-
, D.text " Click me to increment!"
36-
]
27+
counter = mkUI counterSpec
3728
where
38-
counterSpec = (spec 0)
29+
counterSpec = (spec 0 render)
3930
{ componentDidMount = \ctx ->
4031
interval 1000 $ do
4132
val <- readState ctx
4233
print val
4334
}
35+
render ctx = do
36+
val <- readState ctx
37+
return $ D.button [ P.className "Counter"
38+
, P.onClick \_ -> do
39+
val <- readState ctx
40+
writeState ctx (val + 1)
41+
]
42+
[ D.text (show val)
43+
, D.text " Click me to increment!"
44+
]
4445

4546
main = do
4647
let component = D.div' [ hello { name: "World" }, counter unit ]

0 commit comments

Comments
 (0)