fluxx
Version:
Terse application state management
111 lines (79 loc) • 4.28 kB
JavaScript
;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports.__esModule = true;
exports.default = connect;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _shallowEqual = require('./shallowEqual');
var _shallowEqual2 = _interopRequireDefault(_shallowEqual);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
/* Wraps a React Component and re-render it when the Store changes */
function connect(Component, stores, stateSlicer) {
return function (_React$Component) {
_inherits(Connect, _React$Component);
function Connect(props, context) {
_classCallCheck(this, Connect);
var _this = _possibleConstructorReturn(this, _React$Component.call(this, props, context));
_this.state = {};
_this.onStoreChange = _this.onStoreChange.bind(_this);
return _this;
}
Connect.prototype.componentWillMount = function componentWillMount() {
var _this2 = this;
this.propsChanged = true;
if (!Array.isArray(stores)) stores = [stores];
this.stores = stores.map(function (store) {
return isFunction(store) ? store(_this2.props) : store;
});
this.unsubscribe = this.subscribeToStores(this.stores);
// Initial render
this.onStoreChange();
};
Connect.prototype.componentWillUnmount = function componentWillUnmount() {
this.unsubscribe();
};
Connect.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
if (!(0, _shallowEqual2.default)(this.props, nextProps)) this.propsChanged = true;
};
Connect.prototype.subscribeToStores = function subscribeToStores(stores) {
var _this3 = this;
var unsubFns = stores.map(function (store) {
return store.subscribe(_this3.onStoreChange);
});
return function () {
unsubFns.forEach(function (fn) {
return fn();
});
};
};
Connect.prototype.onStoreChange = function onStoreChange() {
var states = this.stores.map(function (store) {
return store.state;
});
var currentSlice = this.state.stateSlice;
var newSlice = stateSlicer.apply(null, states);
if (!currentSlice || !(0, _shallowEqual2.default)(currentSlice, newSlice)) {
this.stateChanged = true;
this.setState({ stateSlice: newSlice });
}
};
Connect.prototype.render = function render() {
// Combine props and state inside render() to have a cohesive view of the system:
// Both the store and the props given by our parents may have changed.
// The below check is equivalent to a delayed shouldComponentUpdate.
if (this.propsChanged || this.stateChanged) {
this.propsChanged = this.stateChanged = false;
var childProps = _extends({}, this.props, this.state.stateSlice);
this.childElement = (0, _react.createElement)(Component, childProps);
}
return this.childElement;
};
return Connect;
}(_react2.default.Component);
};
function isFunction(x) {
return Object.prototype.toString.call(x) === '[object Function]';
}