nesquirk
Version:
Ties Nes + minimongo together for gloryful reactive apps.
168 lines (129 loc) • 6.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.default = createContainer;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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; }
var noData = function noData() {
return {};
};
function createContainer(getData, opts, Comp) {
if (!Comp) {
Comp = opts;
opts = {};
}
opts = opts || {};
getData = getData || noData;
var clientKey = opts.clientKey || 'client';
var Container = function (_Component) {
_inherits(Container, _Component);
function Container() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Container);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Container.__proto__ || Object.getPrototypeOf(Container)).call.apply(_ref, [this].concat(args))), _this), _this.state = { data: {} }, _this.onSubscriptionReady = function () {
return _this.resubscribe(_this.props, _this.context);
}, _this.onCollectionChange = function () {
return _this.resubscribe(_this.props, _this.context);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Container, [{
key: 'componentWillMount',
value: function componentWillMount() {
this.subscribe(this.props, this.context);
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps, nextContext) {
this.resubscribe(nextProps, nextContext);
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.unsubscribe();
}
}, {
key: 'subscribe',
value: function subscribe(props, context) {
var _this2 = this;
var client = opts.client || context[clientKey];
var subs = [];
var ctx = {
subscribe: function subscribe() {
var sub = client.subscribe.apply(client, arguments);
subs.push(sub);
return sub;
}
};
var data = getData.call(ctx, props);
// Listen for changes for the collections that belongs to these subs
this.getCollections(subs).forEach(function (c) {
c.on('change', _this2.onCollectionChange);
});
subs.forEach(function (s) {
return s.on('ready', _this2.onSubscriptionReady);
});
this._subs = subs;
this.setState({ data: data });
}
}, {
key: 'resubscribe',
value: function resubscribe(props, context) {
var _this3 = this;
var prevSubs = this._subs;
this.getCollections(prevSubs).forEach(function (c) {
c.removeListener('change', _this3.onCollectionChange);
});
this.subscribe(props, context);
prevSubs.forEach(function (s) {
s.removeListener('ready', _this3.onSubscriptionReady);
s.stop();
});
}
}, {
key: 'unsubscribe',
value: function unsubscribe() {
var _this4 = this;
var subs = this._subs;
this.getCollections(subs).forEach(function (c) {
c.removeListener('change', _this4.onCollectionChange);
});
this._subs = [];
subs.forEach(function (s) {
s.removeListener('ready', _this4.onSubscriptionReady);
s.stop();
});
}
}, {
key: 'getCollections',
value: function getCollections(subs) {
return subs.reduce(function (collections, s) {
if (collections.includes(s.collection)) return collections;
return collections.concat(s.collection);
}, []);
}
}, {
key: 'render',
value: function render() {
var props = this.props;
return _react2.default.createElement(Comp, _extends({}, props, this.state.data));
}
}]);
return Container;
}(_react.Component);
Container.contextTypes = _defineProperty({}, clientKey, _propTypes2.default.object.isRequired);
return Container;
}