Skip to content

Commit c17cdf3

Browse files
author
Michael Trotter
committed
Use Union instead of Record update
1 parent b5ebb89 commit c17cdf3

6 files changed

Lines changed: 114 additions & 127 deletions

File tree

examples/component/src/ToggleButton.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ data Action
1818

1919
toggleButton :: Props -> JSX
2020
toggleButton = make component
21-
{ initialState =
21+
{ initialState:
2222
{ on: false
2323
}
2424

25-
, update = \self -> case _ of
25+
, update: \self -> case _ of
2626
Toggle ->
2727
UpdateAndSideEffects
2828
self.state { on = not self.state.on }
2929
\nextSelf -> do
3030
log $ "next state: " <> show nextSelf.state
3131

32-
, render = \self ->
32+
, render: \self ->
3333
R.button
3434
{ onClick: capture_ self Toggle
3535
, children:

examples/controlled-input/src/ControlledInput.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ data Action
1919

2020
controlledInput :: Props -> JSX
2121
controlledInput = make component
22-
{ initialState =
22+
{ initialState:
2323
{ value: "hello world"
2424
, timestamp: Nothing
2525
}
2626

27-
, update = \self -> case _ of
27+
, update: \self -> case _ of
2828
ValueChanged value timestamp ->
2929
Update self.state
3030
{ value = value
3131
, timestamp = Just timestamp
3232
}
3333

34-
, render = \self ->
34+
, render: \self ->
3535
React.fragment
3636
[ R.input
3737
{ onChange:

examples/counter/src/Counter.purs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Counter where
22

33
import Prelude
44

5+
56
import React.Basic (Component, JSX, StateUpdate(..), capture_, createComponent, make)
67
import React.Basic.DOM as R
78

@@ -16,16 +17,16 @@ data Action
1617
= Increment
1718

1819
counter :: Props -> JSX
19-
counter = make component
20-
{ initialState = { counter: 0, dummy: 0 }
20+
counter = make component { initialState, update, render }
21+
where
22+
initialState = { counter: 0, dummy: 0 }
2123

22-
, update = \self -> case _ of
24+
update self = case _ of
2325
Increment ->
2426
Update self.state { counter = self.state.counter + 1 }
2527

26-
, render = \self ->
28+
render self =
2729
R.button
2830
{ onClick: capture_ self Increment
2931
, children: [ R.text (self.props.label <> ": " <> show self.state.counter) ]
3032
}
31-
}

examples/legacy-v2/src/Compat.purs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ component
2323
-> ReactComponent { | props }
2424
component { displayName, initialState, receiveProps, render } =
2525
toReactComponent identity (createComponent displayName)
26-
{ initialState = initialState
27-
, didMount = receiveProps <<< selfToLegacySelf
28-
, didUpdate = receiveProps <<< selfToLegacySelf
29-
, update = \self stateUpdate -> Update (stateUpdate self.state)
30-
, render = render <<< selfToLegacySelf
26+
{ initialState: initialState
27+
, didMount: receiveProps <<< selfToLegacySelf
28+
, didUpdate: receiveProps <<< selfToLegacySelf
29+
, update: \self stateUpdate -> Update (stateUpdate self.state)
30+
, render: render <<< selfToLegacySelf
3131
}
3232
where
3333
selfToLegacySelf self@{ props, state } =
@@ -45,5 +45,5 @@ stateless
4545
-> ReactComponent { | props }
4646
stateless { displayName, render } =
4747
toReactComponent identity (createComponent displayName)
48-
{ render = \self -> render self.props
48+
{ render: \self -> render self.props
4949
}

src/React/Basic.js

Lines changed: 68 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,7 @@
33
var React = require("react");
44
var Fragment = React.Fragment || "div";
55

6-
exports.createComponent_ = function(defaultUpdate) {
7-
var defaultInitialState = null;
8-
var defaultShouldUpdate = function() {
9-
return function() {
10-
return function() {
11-
return true;
12-
};
13-
};
14-
};
15-
var defaultDidMount = function() {
16-
return function() {};
17-
};
18-
var defaultDidUpdate = function() {
19-
return function() {};
20-
};
21-
var defaultWillUnmount = function() {
22-
return function() {};
23-
};
24-
var defaultRender = function() {
25-
return false;
26-
};
27-
6+
exports.createComponent = (function() {
287
// Begin component prototype functions
298
// (`this`-dependent, defined outside `createComponent`
309
// for a slight performance boost)
@@ -39,7 +18,7 @@ exports.createComponent_ = function(defaultUpdate) {
3918

4019
function shouldComponentUpdate(nextProps, nextState) {
4120
var shouldUpdate = this.$$spec.shouldUpdate;
42-
return shouldUpdate === defaultShouldUpdate
21+
return shouldUpdate === undefined
4322
? true
4423
: shouldUpdate(this.toSelf())(nextProps.$$props)(
4524
nextState === null ? null : nextState.$$state
@@ -48,22 +27,22 @@ exports.createComponent_ = function(defaultUpdate) {
4827

4928
function componentDidMount() {
5029
var didMount = this.$$spec.didMount;
51-
if (didMount !== defaultDidMount) {
52-
this.$$spec.didMount(this.toSelf())();
30+
if (didMount !== undefined) {
31+
didMount(this.toSelf())();
5332
}
5433
}
5534

5635
function componentDidUpdate() {
5736
var didUpdate = this.$$spec.didUpdate;
58-
if (didUpdate !== defaultDidUpdate) {
37+
if (didUpdate !== undefined) {
5938
didUpdate(this.toSelf())();
6039
}
6140
}
6241

6342
function componentWillUnmount() {
6443
this.$$mounted = false;
6544
var willUnmount = this.$$spec.willUnmount;
66-
if (willUnmount !== defaultWillUnmount) {
45+
if (willUnmount !== undefined) {
6746
willUnmount(this.toSelf())();
6847
}
6948
}
@@ -81,40 +60,36 @@ exports.createComponent_ = function(defaultUpdate) {
8160
// React may optimize components with no state,
8261
// so we leave state null if it was left as
8362
// the default value.
84-
this.$$spec.initialState === defaultInitialState
63+
this.$$spec.initialState === undefined
8564
? null
8665
: { $$state: this.$$spec.initialState };
8766
return this;
8867
};
8968

9069
Component.displayName = displayName;
9170
Component.prototype = Object.create(React.Component.prototype);
71+
Component.prototype.constructor = Component;
9272
Component.prototype.toSelf = toSelf;
9373
Component.prototype.shouldComponentUpdate = shouldComponentUpdate;
9474
Component.prototype.componentDidMount = componentDidMount;
9575
Component.prototype.componentDidUpdate = componentDidUpdate;
9676
Component.prototype.componentWillUnmount = componentWillUnmount;
9777
Component.prototype.render = render;
9878

99-
return {
100-
$$type: Component,
101-
initialState: defaultInitialState,
102-
shouldUpdate: defaultShouldUpdate,
103-
didMount: defaultDidMount,
104-
didUpdate: defaultDidUpdate,
105-
willUnmount: defaultWillUnmount,
106-
update: defaultUpdate,
107-
render: defaultRender
108-
};
79+
return Component;
10980
};
110-
};
81+
})();
11182

11283
exports.send_ = function(buildStateUpdate) {
11384
return function(self, action) {
11485
if (!self.instance_.$$mounted) {
11586
exports.warningUnmountedComponentAction(self, action);
11687
return;
11788
}
89+
if (self.instance_.$$spec.update === undefined) {
90+
exports.warningDefaultUpdate(self, action);
91+
return;
92+
}
11893
var sideEffects = null;
11994
self.instance_.setState(
12095
function(s) {
@@ -150,13 +125,26 @@ exports.readState = function(self) {
150125
return state === null ? null : state.$$state;
151126
};
152127

153-
exports.make = function($$spec) {
154-
return function($$props) {
155-
var props = {
156-
$$props: $$props,
157-
$$spec: $$spec
128+
exports.make = function(_unionDict) {
129+
return function($$type) {
130+
return function($$spec) {
131+
var $$specPadded = {
132+
initialState: $$spec.initialState,
133+
update: $$spec.update,
134+
render: $$spec.render,
135+
shouldUpdate: $$spec.shouldUpdate,
136+
didMount: $$spec.didMount,
137+
didUpdate: $$spec.didUpdate,
138+
willUnmount: $$spec.willUnmount
139+
};
140+
return function($$props) {
141+
var props = {
142+
$$props: $$props,
143+
$$spec: $$specPadded
144+
};
145+
return React.createElement($$type, props);
146+
};
158147
};
159-
return React.createElement($$spec.$$type, props);
160148
};
161149
};
162150

@@ -179,32 +167,48 @@ exports.fragment = function(children) {
179167
return React.createElement.apply(null, [Fragment, {}].concat(children));
180168
};
181169

182-
exports.displayNameFromComponentSpec = function($$spec) {
183-
return $$spec.$$type.displayName || "[unknown]";
170+
exports.displayNameFromComponent = function($$type) {
171+
return $$type.displayName || "[unknown]";
184172
};
185173

186174
exports.displayNameFromSelf = function(self) {
187-
return exports.displayNameFromComponentSpec(self.instance_.$$spec);
175+
return exports.displayNameFromComponent(self.instance_.prototype.constructor);
188176
};
189177

190-
exports.toReactComponent_ = function(fromJSProps, $$spec) {
191-
var Component = function constructor() {
192-
return this;
193-
};
194-
195-
Component.prototype = Object.create(React.Component.prototype);
196-
197-
Component.displayName = $$spec.$$type.displayName + " (Wrapper)";
198-
199-
Component.prototype.render = function() {
200-
var props = {
201-
$$props: fromJSProps(this.props),
202-
$$spec: $$spec
178+
exports.toReactComponent = function(_unionDict) {
179+
return function(fromJSProps) {
180+
return function($$type) {
181+
return function($$spec) {
182+
var $$specPadded = {
183+
initialState: $$spec.initialState,
184+
update: $$spec.update,
185+
render: $$spec.render,
186+
shouldUpdate: $$spec.shouldUpdate,
187+
didMount: $$spec.didMount,
188+
didUpdate: $$spec.didUpdate,
189+
willUnmount: $$spec.willUnmount
190+
};
191+
192+
var Component = function constructor() {
193+
return this;
194+
};
195+
196+
Component.prototype = Object.create(React.Component.prototype);
197+
198+
Component.displayName = $$type.displayName + " (Wrapper)";
199+
200+
Component.prototype.render = function() {
201+
var props = {
202+
$$props: fromJSProps(this.props),
203+
$$spec: $$specPadded
204+
};
205+
return React.createElement($$type, props);
206+
};
207+
208+
return Component;
209+
};
203210
};
204-
return React.createElement($$spec.$$type, props);
205211
};
206-
207-
return Component;
208212
};
209213

210214
exports.warningDefaultUpdate = function(self, action) {

0 commit comments

Comments
 (0)