Skip to content

Commit 21c4338

Browse files
author
Fabien JUIF
authored
🔨 currying and optimizing (#69)
1 parent 74bbc27 commit 21c4338

File tree

14 files changed

+172
-157
lines changed

14 files changed

+172
-157
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import loader from 'hoc-react-loader'
2020

2121
const Component = ({ data }) => <div>Component {JSON.stringify(data)}</div>
2222

23-
export default loader(Component, { print: ['data'] })
23+
export default loader({ print: ['data'] })(Component)
2424
```
2525
In this case, the loader waits for `this.props.data` to be truthy, then mounts its child component and calls `this.props.load` if it exists. This is useful when the parent has control over the injected data, or when the `Component` is connected with `redux`. `this.props.load` should be injected by the parent component or injected by a `Container` (redux).
2626

@@ -33,7 +33,7 @@ import loader from 'hoc-react-loader'
3333
const MyLoadingIndicator = () => <div>Waiting...</div>
3434
const Component = ({ data }) => <div>Component {data}</div>
3535

36-
export default loader(Component, { print: ['data'], LoadingIndicator: MyLoadingIndicator })
36+
export default loader({ print: ['data'], LoadingIndicator: MyLoadingIndicator })(Component)
3737
```
3838

3939
### Don't wait
@@ -42,7 +42,7 @@ import loader from 'hoc-react-loader'
4242

4343
const Component = ({ data }) => <div>Component {JSON.stringify(data)}</div>
4444

45-
export default loader(Component)
45+
export default loader()(Component)
4646
```
4747
In this example, the loader component doesn't wait for props. `this.props.load` is called once, but the `LoadingIndicator` component isn't displayed.
4848

@@ -52,7 +52,7 @@ import loader from 'hoc-react-loader'
5252

5353
const Component = ({ data }) => <div>Component {JSON.stringify(data)}</div>
5454

55-
export default loader(Component, { load: () => console.log('here') })
55+
export default loader({ load: () => console.log('here') })(Component)
5656
```
5757
In this case, the loader calls `this.props.load` if it exists *AND* the `load` parameter, resulting in `here` to be printed.
5858

@@ -64,7 +64,7 @@ import loader from 'hoc-react-loader'
6464

6565
const Component = ({ data }) => <div>Component {JSON.stringify(data)}</div>
6666

67-
export default loader(Component, { load: 'myLoader' })
67+
export default loader({ load: 'myLoader' })(Component)
6868
```
6969
In this case, the loader calls `this.props.myLoader` if it exists.
7070

build/core.js

Lines changed: 84 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -43,100 +43,109 @@ var getDisplayName = function getDisplayName(c) {
4343
return c.displayName || c.name || 'Component';
4444
};
4545

46-
exports.default = function (ComposedComponent) {
47-
var _class, _temp2;
48-
49-
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
46+
exports.default = function () {
47+
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
5048
LoadingIndicator = _ref.LoadingIndicator,
5149
print = _ref.print,
5250
load = _ref.load;
5351

5452
var loadFunctionName = isString(load) ? load : 'load';
53+
var isPrintArray = Array.isArray(print);
54+
var isPrintFunction = isFunction(print);
55+
var isLoadFunction = isFunction(load);
56+
57+
var isLoaded = function isLoaded(props, context) {
58+
// Print is undefined,
59+
// we rely on 'props.loaded' if present
60+
// if not, we directly print the component
61+
if (print === undefined) {
62+
var loaded = props.loaded;
63+
64+
return loaded === undefined ? true : !!loaded;
65+
}
5566

56-
return _temp2 = _class = function (_Component) {
57-
_inherits(_class, _Component);
67+
// Print is an array
68+
// Implicitly meaning that this is an array of props
69+
if (isPrintArray) {
70+
return print.map(function (p) {
71+
return Boolean(props[p]);
72+
}).reduce(function (allProps, currentProp) {
73+
return allProps && currentProp;
74+
});
75+
}
5876

59-
function _class() {
60-
var _ref2;
77+
// Print is a function
78+
if (isPrintFunction) {
79+
return !!print(props, context);
80+
}
6181

62-
var _temp, _this, _ret;
82+
// Anything else
83+
return !!print;
84+
};
6385

64-
_classCallCheck(this, _class);
86+
return function (ComposedComponent) {
87+
var _class, _temp2;
6588

66-
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
67-
args[_key] = arguments[_key];
68-
}
89+
var displayName = 'Loader(' + getDisplayName(ComposedComponent) + ')';
6990

70-
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = _class.__proto__ || Object.getPrototypeOf(_class)).call.apply(_ref2, [this].concat(args))), _this), _this.state = {
71-
props: {}
72-
}, _this.isLoaded = function () {
73-
// Print is undefined,
74-
// we rely on 'props.loaded' if present
75-
// if not, we directly print the component
76-
if (print === undefined) {
77-
var loaded = _this.props.loaded;
91+
return _temp2 = _class = function (_Component) {
92+
_inherits(_class, _Component);
7893

79-
return loaded === undefined ? true : !!loaded;
80-
}
94+
function _class() {
95+
var _ref2;
8196

82-
// Print is an array
83-
// Implicitly meaning that this is an array of props
84-
if (Array.isArray(print)) {
85-
return print.map(function (p) {
86-
return Boolean(_this.props[p]);
87-
}).reduce(function (allProps, currentProp) {
88-
return allProps && currentProp;
89-
});
90-
}
97+
var _temp, _this, _ret;
9198

92-
// Print is a function
93-
if (isFunction(print)) {
94-
return !!print(_this.props, _this.context);
95-
}
99+
_classCallCheck(this, _class);
96100

97-
// Anything else
98-
return !!print;
99-
}, _this.omitLoadInProps = function (props) {
100-
var isLoadAFunction = isFunction(props[loadFunctionName]);
101-
102-
if (isLoadAFunction) {
103-
_this.setState({
104-
props: _extends({}, props, _defineProperty({}, loadFunctionName, undefined))
105-
});
106-
} else {
107-
_this.setState({ props: props });
101+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
102+
args[_key] = arguments[_key];
108103
}
109104

110-
return isLoadAFunction;
111-
}, _this.componentWillReceiveProps = function (nextProps) {
112-
_this.omitLoadInProps(nextProps);
113-
}, _temp), _possibleConstructorReturn(_this, _ret);
114-
}
105+
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = _class.__proto__ || Object.getPrototypeOf(_class)).call.apply(_ref2, [this].concat(args))), _this), _this.state = {
106+
props: {}
107+
}, _this.omitLoadInProps = function (props) {
108+
var isLoadAFunction = isFunction(props[loadFunctionName]);
109+
110+
if (isLoadAFunction) {
111+
_this.setState({
112+
props: _extends({}, props, _defineProperty({}, loadFunctionName, undefined))
113+
});
114+
} else {
115+
_this.setState({ props: props });
116+
}
117+
118+
return isLoadAFunction;
119+
}, _this.componentWillReceiveProps = function (nextProps) {
120+
_this.omitLoadInProps(nextProps);
121+
}, _temp), _possibleConstructorReturn(_this, _ret);
122+
}
115123

116-
_createClass(_class, [{
117-
key: 'componentWillMount',
118-
value: function componentWillMount() {
119-
// Load from hoc argument
120-
if (isFunction(load)) {
121-
load(this.props, this.context);
124+
_createClass(_class, [{
125+
key: 'componentWillMount',
126+
value: function componentWillMount() {
127+
// Load from hoc argument
128+
if (isLoadFunction) {
129+
load(this.props, this.context);
130+
}
131+
132+
// Load from props
133+
if (this.omitLoadInProps(this.props)) {
134+
this.props[loadFunctionName](this.props, this.context);
135+
}
122136
}
123-
124-
// Load from props
125-
if (this.omitLoadInProps(this.props)) {
126-
this.props[loadFunctionName](this.props, this.context);
137+
}, {
138+
key: 'render',
139+
value: function render() {
140+
if (!isLoaded(this.props, this.context)) {
141+
return _react2.default.createElement(LoadingIndicator, this.state.props);
142+
}
143+
144+
return _react2.default.createElement(ComposedComponent, this.state.props);
127145
}
128-
}
129-
}, {
130-
key: 'render',
131-
value: function render() {
132-
if (!this.isLoaded()) {
133-
return _react2.default.createElement(LoadingIndicator, this.state.props);
134-
}
135-
136-
return _react2.default.createElement(ComposedComponent, this.state.props);
137-
}
138-
}]);
146+
}]);
139147

140-
return _class;
141-
}(_react.Component), _class.displayName = 'Loader(' + getDisplayName(ComposedComponent) + ')', _temp2;
148+
return _class;
149+
}(_react.Component), _class.displayName = displayName, _temp2;
150+
};
142151
};

build/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
1818

1919
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
2020

21-
exports.default = function (ComposedComponent) {
22-
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
21+
exports.default = function () {
22+
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
2323

2424
var _ref$LoadingIndicator = _ref.LoadingIndicator,
2525
LoadingIndicator = _ref$LoadingIndicator === undefined ? _TailSpin2.default : _ref$LoadingIndicator,
2626
rest = _objectWithoutProperties(_ref, ['LoadingIndicator']);
2727

28-
return (0, _core2.default)(ComposedComponent, _extends({}, rest, { LoadingIndicator: LoadingIndicator }));
28+
return (0, _core2.default)(_extends({}, rest, { LoadingIndicator: LoadingIndicator }));
2929
};

examples/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"hoc-react-loader": "file:../",
5252
"lodash": "^4.15.0",
5353
"normalize.css": "~4.2.0",
54+
"prop-types": "^15.5.8",
5455
"react": "15.3.0",
5556
"react-dom": "15.3.0",
5657
"react-highlight": "^0.8.0",

examples/src/components/Examples/Base.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ Base.propTypes = {
99
className: PropTypes.string,
1010
}
1111

12-
export default loader(Base)
12+
export default loader()(Base)

examples/src/components/Examples/DontWait.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ DontWait.propTypes = {
1414
prop: PropTypes.string.isRequired,
1515
}
1616

17-
export default loader(DontWait, { print: true })
17+
export default loader({ print: true })(DontWait)

examples/src/components/Examples/Examples.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const Examples = ({ style, className }) => (
1717
<Example
1818
className={styles.example}
1919
link="Base"
20-
code="export&nbsp;default loader(Base)"
20+
code="export&nbsp;default loader()(Base)"
2121
buttons={{ 0: true }}
2222
example={<Base />}
2323
>
@@ -39,7 +39,7 @@ const Examples = ({ style, className }) => (
3939
<Example
4040
className={styles.example}
4141
link="OneParam"
42-
code="export&nbsp;default loader(OneParam, { print: ['prop'] })"
42+
code="export&nbsp;default loader({ print: ['prop'] })(OneParam)"
4343
buttons={{ 1: true }}
4444
example={<OneParam />}
4545
>
@@ -56,7 +56,7 @@ const Examples = ({ style, className }) => (
5656
<Example
5757
className={styles.example}
5858
link="TwoParams"
59-
code="export&nbsp;default loader(TwoParams, { print: ['prop', 'prop2'] })"
59+
code="export&nbsp;default loader({ print: ['prop', 'prop2'] })(TwoParams)"
6060
buttons={{ 1: true, 2: true }}
6161
example={<TwoParams />}
6262
>
@@ -73,7 +73,7 @@ const Examples = ({ style, className }) => (
7373
<Example
7474
className={styles.example}
7575
link="DontWait"
76-
code="export&nbsp;default loader(DontWait, { print: true })"
76+
code="export&nbsp;default loader({ print: true })(DontWait)"
7777
buttons={{ 0: true }}
7878
example={<DontWait />}
7979
>
@@ -90,7 +90,7 @@ const Examples = ({ style, className }) => (
9090
<Example
9191
className={styles.example}
9292
link="LoadingIndicator"
93-
code="export&nbsp;default loader(LoadingIndicator, { LoadingIndicator: CustomLoadingIndicator })" // eslint-disable-line
93+
code="export&nbsp;default loader({ LoadingIndicator: CustomLoadingIndicator })(LoadingIndicator)" // eslint-disable-line
9494
buttons={{ 0: true }}
9595
example={<LoadingIndicator />}
9696
>

examples/src/components/Examples/LoadingIndicator.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ LoadingIndicator.propTypes = {
1111
className: PropTypes.string,
1212
}
1313

14-
export default loader(LoadingIndicator, { LoadingIndicator: CustomLoadingIndicator })
14+
export default loader({ LoadingIndicator: CustomLoadingIndicator })(LoadingIndicator)

examples/src/components/Examples/OneParam.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ OneParam.propTypes = {
1414
prop: PropTypes.string.isRequired,
1515
}
1616

17-
export default loader(OneParam, { print: ['prop'] })
17+
export default loader({ print: ['prop'] })(OneParam)

examples/src/components/Examples/TwoParams.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ TwoParams.propTypes = {
1515
prop2: PropTypes.string.isRequired,
1616
}
1717

18-
export default loader(TwoParams, { print: ['prop', 'prop2'] })
18+
export default loader({ print: ['prop', 'prop2'] })(TwoParams)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hoc-react-loader",
3-
"version": "5.0.1",
3+
"version": "6.0.0",
44
"description": "Higher order component to call a load function from props at mount.",
55
"main": "build/index.js",
66
"peerDependencies": {

0 commit comments

Comments
 (0)