Repository URL to install this package:
|
Version:
1.2.12 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _invariant = _interopRequireDefault(require("invariant"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* A <Subscriber> pulls the value for a channel off of context.broadcasts
* and passes it to its children function.
*/
class Subscriber extends _react.default.Component {
constructor(...args) {
var _temp;
return _temp = super(...args), Object.defineProperty(this, "state", {
configurable: true,
enumerable: true,
writable: true,
value: {
value: undefined
}
}), _temp;
}
getBroadcast() {
const broadcasts = this.context.broadcasts || {};
const broadcast = broadcasts[this.props.channel];
(0, _invariant.default)(this.props.quiet || broadcast, '<Subscriber channel="%s"> must be rendered in the context of a <Broadcast channel="%s">', this.props.channel, this.props.channel);
return broadcast;
}
componentWillMount() {
const broadcast = this.getBroadcast();
if (broadcast) {
this.setState({
value: broadcast.getValue()
});
}
}
componentDidMount() {
const broadcast = this.getBroadcast();
if (broadcast) {
this.unsubscribe = broadcast.subscribe(value => {
this.setState({
value
});
});
}
}
componentWillUnmount() {
if (this.unsubscribe) this.unsubscribe();
}
render() {
const children = this.props.children;
return children ? children(this.state.value) : null;
}
}
Object.defineProperty(Subscriber, "propTypes", {
configurable: true,
enumerable: true,
writable: true,
value: {
channel: _propTypes.default.string.isRequired,
children: _propTypes.default.func,
quiet: _propTypes.default.bool
}
});
Object.defineProperty(Subscriber, "defaultProps", {
configurable: true,
enumerable: true,
writable: true,
value: {
quiet: false
}
});
Object.defineProperty(Subscriber, "contextTypes", {
configurable: true,
enumerable: true,
writable: true,
value: {
broadcasts: _propTypes.default.object
}
});
var _default = Subscriber;
exports.default = _default;