Skip to content

Commit 7818536

Browse files
author
Michael Trotter
committed
More performance tweaks
1 parent d6cdfd9 commit 7818536

2 files changed

Lines changed: 64 additions & 64 deletions

File tree

src/React/Basic.js

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ exports.createComponent_ = function(
88
buildStateUpdate,
99
handler,
1010
composeCancelEventFn,
11-
identityEventFn,
12-
displayName
11+
identityEventFn
1312
) {
1413
var defaultInitialState = null;
1514
var defaultShouldUpdate = function() {
@@ -106,71 +105,73 @@ exports.createComponent_ = function(
106105
return self;
107106
}
108107

109-
var Component = function constructor(props) {
110-
this.$$mounted = true;
111-
this.$$spec = props.$$spec;
112-
this.state =
113-
// React may optimize components with no state,
114-
// so we leave state null if it was left as
115-
// the default value.
116-
this.$$spec.initialState === defaultInitialState
117-
? null
118-
: { $$state: this.$$spec.initialState };
119-
return this;
120-
};
108+
return function(displayName) {
109+
var Component = function constructor(props) {
110+
this.$$mounted = true;
111+
this.$$spec = props.$$spec;
112+
this.state =
113+
// React may optimize components with no state,
114+
// so we leave state null if it was left as
115+
// the default value.
116+
this.$$spec.initialState === defaultInitialState
117+
? null
118+
: { $$state: this.$$spec.initialState };
119+
return this;
120+
};
121121

122-
Component.prototype = Object.create(React.Component.prototype);
122+
Component.prototype = Object.create(React.Component.prototype);
123123

124-
Component.displayName = displayName;
124+
Component.displayName = displayName;
125125

126-
Component.prototype.shouldComponentUpdate = function(nextProps, nextState) {
127-
var shouldUpdate = this.$$spec.shouldUpdate;
128-
return shouldUpdate === defaultShouldUpdate
129-
? true
130-
: shouldUpdate({
131-
props: this.props.$$props,
132-
state: this.state === null ? null : this.state.$$state
133-
})(nextProps.$$props)(nextState === null ? null : nextState.$$state);
134-
};
126+
Component.prototype.shouldComponentUpdate = function(nextProps, nextState) {
127+
var shouldUpdate = this.$$spec.shouldUpdate;
128+
return shouldUpdate === defaultShouldUpdate
129+
? true
130+
: shouldUpdate({
131+
props: this.props.$$props,
132+
state: this.state === null ? null : this.state.$$state
133+
})(nextProps.$$props)(nextState === null ? null : nextState.$$state);
134+
};
135135

136-
Component.prototype.componentDidMount = function() {
137-
var didMount = this.$$spec.didMount;
138-
if (didMount !== defaultDidMount) {
139-
this.$$spec.didMount(contextToSelf(this))();
140-
}
141-
};
136+
Component.prototype.componentDidMount = function() {
137+
var didMount = this.$$spec.didMount;
138+
if (didMount !== defaultDidMount) {
139+
this.$$spec.didMount(contextToSelf(this))();
140+
}
141+
};
142142

143-
Component.prototype.componentDidUpdate = function() {
144-
var didUpdate = this.$$spec.didUpdate;
145-
if (didUpdate !== defaultDidUpdate) {
146-
didUpdate(contextToSelf(this))();
147-
}
148-
};
143+
Component.prototype.componentDidUpdate = function() {
144+
var didUpdate = this.$$spec.didUpdate;
145+
if (didUpdate !== defaultDidUpdate) {
146+
didUpdate(contextToSelf(this))();
147+
}
148+
};
149149

150-
Component.prototype.componentWillUnmount = function() {
151-
this.$$mounted = false;
152-
var willUnmount = this.$$spec.willUnmount;
153-
if (willUnmount !== defaultWillUnmount) {
154-
willUnmount({
155-
props: this.props.$$props,
156-
state: this.state === null ? null : this.state.$$state
157-
})();
158-
}
159-
};
150+
Component.prototype.componentWillUnmount = function() {
151+
this.$$mounted = false;
152+
var willUnmount = this.$$spec.willUnmount;
153+
if (willUnmount !== defaultWillUnmount) {
154+
willUnmount({
155+
props: this.props.$$props,
156+
state: this.state === null ? null : this.state.$$state
157+
})();
158+
}
159+
};
160160

161-
Component.prototype.render = function() {
162-
return this.$$spec.render(contextToSelf(this));
163-
};
161+
Component.prototype.render = function() {
162+
return this.$$spec.render(contextToSelf(this));
163+
};
164164

165-
return {
166-
$$type: Component,
167-
initialState: defaultInitialState,
168-
shouldUpdate: defaultShouldUpdate,
169-
didMount: defaultDidMount,
170-
didUpdate: defaultDidUpdate,
171-
willUnmount: defaultWillUnmount,
172-
update: defaultUpdate,
173-
render: defaultRender
165+
return {
166+
$$type: Component,
167+
initialState: defaultInitialState,
168+
shouldUpdate: defaultShouldUpdate,
169+
didMount: defaultDidMount,
170+
didUpdate: defaultDidUpdate,
171+
willUnmount: defaultWillUnmount,
172+
update: defaultUpdate,
173+
render: defaultRender
174+
};
174175
};
175176
};
176177

src/React/Basic.purs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module React.Basic
2727
import Prelude
2828

2929
import Data.Either (Either(..))
30-
import Data.Function.Uncurried (Fn2, Fn6, runFn2, runFn6)
30+
import Data.Function.Uncurried (Fn2, Fn5, runFn2, runFn5)
3131
import Data.Nullable (Nullable, notNull, null)
3232
import Effect (Effect)
3333
import Effect.Aff (Aff, runAff_)
@@ -201,7 +201,7 @@ createComponent
201201
. String
202202
-> ComponentSpec props state Unit action
203203
createComponent =
204-
runFn6
204+
runFn5
205205
createComponent_
206206
NoUpdate
207207
buildStateUpdate
@@ -211,7 +211,7 @@ createComponent =
211211

212212
foreign import createComponent_
213213
:: forall props state action
214-
. Fn6
214+
. Fn5
215215
(StateUpdate props state action)
216216
(StateUpdate props state action
217217
-> { state :: Nullable state
@@ -223,8 +223,7 @@ foreign import createComponent_
223223
(forall a. EventFn SyntheticEvent a -> EventFn SyntheticEvent a)
224224
-- identityEventFn
225225
(EventFn SyntheticEvent SyntheticEvent)
226-
String
227-
(ComponentSpec props state Unit action)
226+
(String -> ComponentSpec props state Unit action)
228227

229228
-- | An empty node. This is often useful when you would like to conditionally
230229
-- | show something, but you don't want to (or can't) modify the `children` prop

0 commit comments

Comments
 (0)