Skip to content

Commit

Permalink
Minifier-friendly references to properties (#183)
Browse files Browse the repository at this point in the history
* 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 <jordanalex.martinez@protonmail.com>
  • Loading branch information
sd-yip and JordanMartinez committed Apr 27, 2022
1 parent a28ad82 commit 6b91856
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Bugfixes:

Other improvements:

## [v10.0.1](https://github.com/purescript-contrib/purescript-react/releases/tag/v10.0.1) - 2022-04-27

Other improvements:
- Minifier-friendly refereces to properties (#183 by @sd-yip)

## [v10.0.0](https://github.com/purescript-contrib/purescript-react/releases/tag/v10.0.0) - 2022-04-27

Breaking changes:
Expand Down
64 changes: 26 additions & 38 deletions src/React.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,19 @@
import React from "react";

function createClass(baseClass) {
function bindProperty(instance, prop, value) {
switch (prop) {
case "state":
case "render":
case "componentDidMount":
case "componentWillUnmount":
instance[prop] = value;
break;

case "componentDidCatch":
case "componentWillUpdate":
case "shouldComponentUpdate":
case "getSnapshotBeforeUpdate":
instance[prop] = function (a, b) { return value(a)(b)(); };
break;

case "componentDidUpdate":
instance[prop] = function (a, b, c) { return value(a)(b)(c)(); };
break;

case "unsafeComponentWillMount":
instance["UNSAFE_componentWillMount"] = value;
break;

case "unsafeComponentWillReceiveProps":
instance["UNSAFE_componentWillReceiveProps"] = function (a) { return value(a)(); };
break;

case "unsafeComponentWillUpdate":
instance["UNSAFE_componentWillUpdate"] = function (a, b) { return value(a)(b)(); };
break;

default:
throw new Error("[purescript-react] Not a component property: " + prop);
function invoke1(f) {
return f === undefined ? f : function (a) {
return f(a)()
}
}
function invoke2(f) {
return f === undefined ? f : function (a, b) {
return f(a)(b)()
}
}
function invoke3(f) {
return f === undefined ? f : function (a, b, c) {
return f(a)(b)(c)()
}
}

Expand All @@ -43,10 +22,19 @@ function createClass(baseClass) {
var Constructor = function (props) {
baseClass.call(this, props);
var spec = ctrFn(this)();
// eslint-disable-next-line guard-for-in
for (var k in spec) {
bindProperty(this, k, spec[k]);
}

this.state = spec.state;
this.render = spec.render;
this.componentDidMount = spec.componentDidMount;
this.componentWillUnmount = spec.componentWillUnmount;
this.componentDidCatch = invoke2(spec.componentDidCatch);
this.componentWillUpdate = invoke2(spec.componentWillUpdate);
this.shouldComponentUpdate = invoke2(spec.shouldComponentUpdate);
this.getSnapshotBeforeUpdate = invoke2(spec.getSnapshotBeforeUpdate);
this.componentDidUpdate = invoke3(spec.componentDidUpdate);
this.UNSAFE_componentWillMount = spec.unsafeComponentWillMount;
this.UNSAFE_componentWillReceiveProps = invoke1(spec.unsafeComponentWillReceiveProps);
this.UNSAFE_componentWillUpdate = invoke2(spec.unsafeComponentWillUpdate);
};

Constructor.displayName = displayName;
Expand Down

0 comments on commit 6b91856

Please sign in to comment.