Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
"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 }; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

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 createBroadcast(initialValue) {
  let currentValue = initialValue;
  let subscribers = [];

  const getValue = () => currentValue;

  const publish = state => {
    currentValue = state;
    subscribers.forEach(s => s(currentValue));
  };

  const subscribe = subscriber => {
    subscribers.push(subscriber);
    return () => {
      subscribers = subscribers.filter(s => s !== subscriber);
    };
  };

  return {
    getValue,
    publish,
    subscribe
  };
}
/**
 * A <Broadcast> provides a generic way for descendants to "subscribe"
 * to some value that changes over time, bypassing any intermediate
 * shouldComponentUpdate's in the hierarchy. It puts all subscription
 * functions on context.broadcasts, keyed by "channel".
 *
 * To use it, a subscriber must opt-in to context.broadcasts. See the
 * <Subscriber> component for a reference implementation.
 */


let Broadcast = class Broadcast extends _react.default.Component {
  constructor(...args) {
    var _temp;

    return _temp = super(...args), this.broadcast = createBroadcast(this.props.value), _temp;
  }

  getChildContext() {
    return {
      broadcasts: _objectSpread({}, this.context.broadcasts, {
        [this.props.channel]: this.broadcast
      })
    };
  }

  componentWillReceiveProps(nextProps) {
    (0, _invariant.default)(this.props.channel === nextProps.channel, "You cannot change <Broadcast channel>");

    if (!this.props.compareValues(this.props.value, nextProps.value)) {
      this.broadcast.publish(nextProps.value);
    }
  }

  render() {
    return _react.default.Children.only(this.props.children);
  }

};
Broadcast.propTypes = {
  channel: _propTypes.default.string.isRequired,
  children: _propTypes.default.node.isRequired,
  compareValues: _propTypes.default.func,
  value: _propTypes.default.any
};
Broadcast.defaultProps = {
  compareValues: (prevValue, nextValue) => prevValue === nextValue
};
Broadcast.contextTypes = {
  broadcasts: _propTypes.default.object
};
Broadcast.childContextTypes = {
  broadcasts: _propTypes.default.object.isRequired
};
var _default = Broadcast;
exports.default = _default;