epitath
Version:
Compose HOCs imperatively like async/await. No callback hell!
61 lines (46 loc) • 2.67 kB
JavaScript
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import React from 'react';
import immutagen from 'immutagen';
var compose = function compose(_ref) {
var next = _ref.next,
value = _ref.value;
return next ? React.cloneElement(value, null, function (values) {
return compose(next(values));
}) : value;
};
export default (function (Component) {
var _class, _temp;
var original = Component.prototype.render;
var displayName = 'EpitathContainer(' + (Component.displayName || 'anonymous') + ')';
if (!original) {
var generator = immutagen(Component);
return Object.assign(function Epitath(props) {
return compose(generator(props));
}, { displayName: displayName });
}
Component.prototype.render = function render() {
// Since we are calling a new function to be called from here instead of
// from a component class, we need to ensure that the render method is
// invoked against `this`. We only need to do this binding and creation of
// this function once, so we cache it by adding it as a property to this
// new render method which avoids keeping the generator outside of this
// method's scope.
if (!render.generator) {
render.generator = immutagen(original.bind(this));
}
return compose(render.generator(this.props));
};
return _temp = _class = function (_React$Component) {
_inherits(EpitathContainer, _React$Component);
function EpitathContainer() {
_classCallCheck(this, EpitathContainer);
return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
}
EpitathContainer.prototype.render = function render() {
return React.createElement(Component, this.props);
};
return EpitathContainer;
}(React.Component), _class.displayName = displayName, _temp;
});