| Current Path : /etc/kibana/node_modules/react-apollo/ |
| Current File : //etc/kibana/node_modules/react-apollo/Subscriptions.js |
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import * as React from 'react';
import * as PropTypes from 'prop-types';
var shallowEqual = require('fbjs/lib/shallowEqual');
var invariant = require('invariant');
var Subscription = (function (_super) {
__extends(Subscription, _super);
function Subscription(props, context) {
var _this = _super.call(this, props, context) || this;
_this.initialize = function (props) {
if (_this.queryObservable)
return;
_this.queryObservable = _this.client.subscribe({
query: props.subscription,
variables: props.variables,
});
};
_this.startSubscription = function () {
if (_this.querySubscription)
return;
_this.querySubscription = _this.queryObservable.subscribe({
next: _this.updateCurrentData,
error: _this.updateError,
});
};
_this.getInitialState = function () { return ({
loading: true,
error: undefined,
data: undefined,
}); };
_this.updateCurrentData = function (result) {
_this.setState({
data: result.data,
loading: false,
error: undefined,
});
};
_this.updateError = function (error) {
_this.setState({
error: error,
loading: false,
});
};
_this.endSubscription = function () {
if (_this.querySubscription) {
_this.querySubscription.unsubscribe();
delete _this.querySubscription;
}
};
invariant(!!context.client, "Could not find \"client\" in the context of Subscription. Wrap the root component in an <ApolloProvider>");
_this.client = context.client;
_this.initialize(props);
_this.state = _this.getInitialState();
return _this;
}
Subscription.prototype.componentDidMount = function () {
this.startSubscription();
};
Subscription.prototype.componentWillReceiveProps = function (nextProps, nextContext) {
if (shallowEqual(this.props.variables, nextProps.variables) &&
this.client === nextContext.client &&
this.props.subscription === nextProps.subscription) {
return;
}
var shouldResubscribe = nextProps.shouldResubscribe;
if (typeof shouldResubscribe === 'function') {
shouldResubscribe = !!shouldResubscribe(this.props, nextProps);
}
var shouldNotResubscribe = shouldResubscribe === false;
if (this.client !== nextContext.client) {
this.client = nextContext.client;
}
if (!shouldNotResubscribe) {
this.endSubscription();
delete this.queryObservable;
this.initialize(nextProps);
this.startSubscription();
this.setState(this.getInitialState());
return;
}
this.initialize(nextProps);
this.startSubscription();
};
Subscription.prototype.componentWillUnmount = function () {
this.endSubscription();
};
Subscription.prototype.render = function () {
var result = Object.assign({}, this.state, {
variables: this.props.variables,
});
return this.props.children(result);
};
Subscription.contextTypes = {
client: PropTypes.object.isRequired,
};
Subscription.propTypes = {
subscription: PropTypes.object.isRequired,
variables: PropTypes.object,
children: PropTypes.func.isRequired,
shouldResubscribe: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
};
return Subscription;
}(React.Component));
export default Subscription;
//# sourceMappingURL=Subscriptions.js.map