Skip to content

Commit 6b91856

Browse files
Minifier-friendly references to properties (#183)
* Minifier-friendly references to properties * Update CHANGELOG.md * Fix currying + invocation * Merge remote-tracking branch 'upstream/main' into minifier-friendly Co-authored-by: Jordan Martinez <[email protected]>
1 parent a28ad82 commit 6b91856

File tree

2 files changed

+31
-38
lines changed

2 files changed

+31
-38
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ Bugfixes:
1212

1313
Other improvements:
1414

15+
## [v10.0.1](https://github.com/purescript-contrib/purescript-react/releases/tag/v10.0.1) - 2022-04-27
16+
17+
Other improvements:
18+
- Minifier-friendly refereces to properties (#183 by @sd-yip)
19+
1520
## [v10.0.0](https://github.com/purescript-contrib/purescript-react/releases/tag/v10.0.0) - 2022-04-27
1621

1722
Breaking changes:

src/React.js

+26-38
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,19 @@
11
import React from "react";
22

33
function createClass(baseClass) {
4-
function bindProperty(instance, prop, value) {
5-
switch (prop) {
6-
case "state":
7-
case "render":
8-
case "componentDidMount":
9-
case "componentWillUnmount":
10-
instance[prop] = value;
11-
break;
12-
13-
case "componentDidCatch":
14-
case "componentWillUpdate":
15-
case "shouldComponentUpdate":
16-
case "getSnapshotBeforeUpdate":
17-
instance[prop] = function (a, b) { return value(a)(b)(); };
18-
break;
19-
20-
case "componentDidUpdate":
21-
instance[prop] = function (a, b, c) { return value(a)(b)(c)(); };
22-
break;
23-
24-
case "unsafeComponentWillMount":
25-
instance["UNSAFE_componentWillMount"] = value;
26-
break;
27-
28-
case "unsafeComponentWillReceiveProps":
29-
instance["UNSAFE_componentWillReceiveProps"] = function (a) { return value(a)(); };
30-
break;
31-
32-
case "unsafeComponentWillUpdate":
33-
instance["UNSAFE_componentWillUpdate"] = function (a, b) { return value(a)(b)(); };
34-
break;
35-
36-
default:
37-
throw new Error("[purescript-react] Not a component property: " + prop);
4+
function invoke1(f) {
5+
return f === undefined ? f : function (a) {
6+
return f(a)()
7+
}
8+
}
9+
function invoke2(f) {
10+
return f === undefined ? f : function (a, b) {
11+
return f(a)(b)()
12+
}
13+
}
14+
function invoke3(f) {
15+
return f === undefined ? f : function (a, b, c) {
16+
return f(a)(b)(c)()
3817
}
3918
}
4019

@@ -43,10 +22,19 @@ function createClass(baseClass) {
4322
var Constructor = function (props) {
4423
baseClass.call(this, props);
4524
var spec = ctrFn(this)();
46-
// eslint-disable-next-line guard-for-in
47-
for (var k in spec) {
48-
bindProperty(this, k, spec[k]);
49-
}
25+
26+
this.state = spec.state;
27+
this.render = spec.render;
28+
this.componentDidMount = spec.componentDidMount;
29+
this.componentWillUnmount = spec.componentWillUnmount;
30+
this.componentDidCatch = invoke2(spec.componentDidCatch);
31+
this.componentWillUpdate = invoke2(spec.componentWillUpdate);
32+
this.shouldComponentUpdate = invoke2(spec.shouldComponentUpdate);
33+
this.getSnapshotBeforeUpdate = invoke2(spec.getSnapshotBeforeUpdate);
34+
this.componentDidUpdate = invoke3(spec.componentDidUpdate);
35+
this.UNSAFE_componentWillMount = spec.unsafeComponentWillMount;
36+
this.UNSAFE_componentWillReceiveProps = invoke1(spec.unsafeComponentWillReceiveProps);
37+
this.UNSAFE_componentWillUpdate = invoke2(spec.unsafeComponentWillUpdate);
5038
};
5139

5240
Constructor.displayName = displayName;

0 commit comments

Comments
 (0)