hzql
Version:
Easy querying for Horizon and React
181 lines (134 loc) • 6.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.connect = exports.Provider = undefined;
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; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _serialization = require('@horizon/client/lib/serialization');
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; }
var Provider = exports.Provider = function (_Component) {
_inherits(Provider, _Component);
function Provider(props) {
_classCallCheck(this, Provider);
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Provider).call(this, props));
_this.props.horizon.$$__hzql_cache = _this.props.cache ? (0, _serialization.deserialize)(_this.props.cache) : {};
_this.props.horizon.$$__hzql_cache_string = '';
return _this;
}
_createClass(Provider, [{
key: 'getChildContext',
value: function getChildContext() {
return {
horizon: this.props.horizon,
Fiber: this.props.fiber || false
};
}
}, {
key: 'render',
value: function render() {
return _react2.default.Children.only(this.props.children);
}
}]);
return Provider;
}(_react.Component);
Provider.childContextTypes = {
horizon: _react2.default.PropTypes.any,
Fiber: _react2.default.PropTypes.any
};
var nextVersion = 0;
var connect = exports.connect = function connect(query, shouldWait, isLive) {
return function (Consumer) {
var version = nextVersion++;
var Connection = function (_Component2) {
_inherits(Connection, _Component2);
function Connection(props, context) {
_classCallCheck(this, Connection);
var _this2 = _possibleConstructorReturn(this, Object.getPrototypeOf(Connection).call(this, props, context));
_this2.state = { results: null };
_this2.version = version;
_this2.subscription = undefined;
return _this2;
}
_createClass(Connection, [{
key: 'componentWillMount',
value: function componentWillMount() {
var _this3 = this;
if (!query) return;
var q = this.context.horizon.model(query(this.context.horizon))(this.props);
var queryString = q.toString();
if (this.context.horizon.$$__hzql_cache[queryString]) {
this.setState({ results: this.context.horizon.$$__hzql_cache[queryString] });
}
if (isLive) {
q = q.watch();
} else {
q = q.fetch();
}
var fiber = void 0;
var sub = q.subscribe(function (results) {
_this3.setState({ results: results });
if (_this3.context.Fiber) {
_this3.context.horizon.$$__hzql_cache[queryString] = results;
_this3.context.horizon.$$__hzql_cache_string = (0, _serialization.serialize)(_this3.context.horizon.$$__hzql_cache);
fiber.run();
}
});
this.subscription = sub;
if (this.context.Fiber) {
fiber = this.context.Fiber.current;
this.context.Fiber.yield();
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
if (!query) return;
this.subscription.unsubscribe();
}
}, {
key: 'render',
value: function render() {
return shouldWait && !this.state.results ? null : _react2.default.createElement(Consumer, _extends({}, this.state.results, this.props, { horizon: this.context.horizon }));
}
}]);
return Connection;
}(_react.Component);
if (process.env.NODE_ENV !== 'production') {
Connection.prototype.componentWillUpdate = function componentWillUpdate() {
var _this4 = this;
if (!query) return;
if (this.version == version) return;
this.version = version;
var q = this.context.horizon.model(query(this.context.horizon))(this.props);
if (isLive) {
q = q.watch();
} else {
q = q.fetch();
}
q.subscribe(function (results) {
return _this4.setState({ results: results });
});
};
}
Connection.contextTypes = {
horizon: _react2.default.PropTypes.any,
Fiber: _react2.default.PropTypes.any
};
return Connection;
};
};
connect.live = function (query) {
return connect(query, false, true);
};
connect.await = function (query) {
return connect(query, true, false);
};
connect.liveAwait = function (query) {
return connect(query, true, true);
};