UNPKG

react-broadcast

Version:

Reliably communicate state changes to deeply nested React elements

2 lines (1 loc) 6.96 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],e):e(t.ReactBroadcast={},t.React)}(this,function(t,e){"use strict";function r(t){return function(){return t}}e=e&&e.hasOwnProperty("default")?e.default:e;var n=function(){};n.thatReturns=r,n.thatReturnsFalse=r(!1),n.thatReturnsTrue=r(!0),n.thatReturnsNull=r(null),n.thatReturnsThis=function(){return this},n.thatReturnsArgument=function(t){return t};var o=n;var s,i=function(t,e,r,n,o,s,i,a){if(!t){var c;if(void 0===e)c=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var u=[r,n,o,s,i,a],p=0;(c=new Error(e.replace(/%s/g,function(){return u[p++]}))).name="Invariant Violation"}throw c.framesToPop=1,c}},a="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED",c=(function(t){t.exports=function(){function t(t,e,r,n,o,s){s!==a&&i(!1,"Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types")}function e(){return t}t.isRequired=t;var r={array:t,bool:t,func:t,number:t,object:t,string:t,symbol:t,any:t,arrayOf:e,element:t,instanceOf:e,node:t,objectOf:e,oneOf:e,oneOfType:e,shape:e,exact:e};return r.checkPropTypes=o,r.PropTypes=r,r}()}(s={exports:{}},s.exports),s.exports),u=function(t,e,r,n,o,s,i,a){if(!t){var c;if(void 0===e)c=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var u=[r,n,o,s,i,a],p=0;(c=new Error(e.replace(/%s/g,function(){return u[p++]}))).name="Invariant Violation"}throw c.framesToPop=1,c}},p=function(){};var l=p;function f(){var t=!1;return function(e){"production"!==process.env.NODE_ENV&&l(t,e),t=!0}}var h=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},d=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},b=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},v=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},y=f();var m=function(t){function r(){var e,n,o,s,i;h(this,r);for(var a=arguments.length,c=Array(a),u=0;u<a;u++)c[u]=arguments[u];return e=n=v(this,t.call.apply(t,[this].concat(c))),n.broadcast=(o=n.props.value,s=o,i=[],{getValue:function(){return s},publish:function(t){s=t,i.forEach(function(t){return t(s)})},subscribe:function(t){return i.push(t),function(){i=i.filter(function(e){return e!==t})}}}),v(n,e)}return b(r,t),r.prototype.getChildContext=function(){var t;return{broadcasts:d({},this.context.broadcasts,(t={},t[this.props.channel]=this.broadcast,t))}},r.prototype.componentWillMount=function(){y("<Broadcast> is deprecated and will be removed in the next major release. Please use createContext instead. See https://goo.gl/QAF37J for more info.")},r.prototype.componentWillReceiveProps=function(t){this.props.channel!==t.channel&&("production"!==process.env.NODE_ENV?u(!1,"You cannot change <Broadcast channel>"):u(!1)),this.props.compareValues(this.props.value,t.value)||this.broadcast.publish(t.value)},r.prototype.render=function(){return e.Children.only(this.props.children)},r}(e.Component);m.defaultProps={compareValues:function(t,e){return t===e}},m.contextTypes={broadcasts:c.object},m.childContextTypes={broadcasts:c.object.isRequired};var g=f(),x=function(t){function e(){var r,n;h(this,e);for(var o=arguments.length,s=Array(o),i=0;i<o;i++)s[i]=arguments[i];return r=n=v(this,t.call.apply(t,[this].concat(s))),n.state={value:void 0},v(n,r)}return b(e,t),e.prototype.getBroadcast=function(){var t=(this.context.broadcasts||{})[this.props.channel];return this.props.quiet||t||("production"!==process.env.NODE_ENV?u(!1,'<Subscriber channel="%s"> must be rendered in the context of a <Broadcast channel="%s">',this.props.channel,this.props.channel):u(!1)),t},e.prototype.componentWillMount=function(){g("<Subscriber> is deprecated and will be removed in the next major release. Please use createContext instead. See https://goo.gl/QAF37J for more info.");var t=this.getBroadcast();t&&this.setState({value:t.getValue()})},e.prototype.componentDidMount=function(){var t=this,e=this.getBroadcast();e&&(this.unsubscribe=e.subscribe(function(e){t.setState({value:e})}))},e.prototype.componentWillUnmount=function(){this.unsubscribe&&this.unsubscribe()},e.prototype.render=function(){var t=this.props.children;return t?t(this.state.value):null},e}(e.Component);x.defaultProps={quiet:!1},x.contextTypes={broadcasts:c.object};var O=1;t.Broadcast=m,t.Subscriber=x,t.createContext=function(t){var r=O++,n=function(t){function e(){var r,n;h(this,e);for(var o=arguments.length,s=Array(o),i=0;i<o;i++)s[i]=arguments[i];return r=n=v(this,t.call.apply(t,[this].concat(s))),n.subscribers=[],n.publish=function(t){n.subscribers.forEach(function(e){return e(t)})},n.subscribe=function(t){return n.subscribers.push(t),function(){n.subscribers=n.subscribers.filter(function(e){return e!==t})}},n.getValue=function(){return n.props.value},v(n,r)}return b(e,t),e.prototype.getChildContext=function(){var t;return{broadcasts:d({},this.context.broadcasts,(t={},t[r]={subscribe:this.subscribe,getValue:this.getValue},t))}},e.prototype.componentWillReceiveProps=function(t){this.props.value!==t.value&&this.publish(t.value)},e.prototype.render=function(){return this.props.children},e}(e.Component);n.defaultValue=t,n.defaultProps={value:t},n.contextTypes={broadcasts:c.object},n.childContextTypes={broadcasts:c.object.isRequired};var o=function(e){function n(){var o,s;h(this,n);for(var i=arguments.length,a=Array(i),c=0;c<i;c++)a[c]=arguments[c];return o=s=v(this,e.call.apply(e,[this].concat(a))),s.broadcast=s.context.broadcasts&&s.context.broadcasts[r],s.state={value:s.broadcast?s.broadcast.getValue():t},v(s,o)}return b(n,e),n.prototype.componentDidMount=function(){var t=this;this.broadcast?this.unsubscribe=this.broadcast.subscribe(function(e){t.setState({value:e})}):"production"!==process.env.NODE_ENV&&l(this.props.quiet,"<Consumer> was rendered outside the context of its <Provider>")},n.prototype.componentWillUnmount=function(){this.unsubscribe&&this.unsubscribe()},n.prototype.render=function(){var t=this.props.children;return t?t(this.state.value):null},n}(e.Component);return o.contextTypes={broadcasts:c.object},o.defaultProps={quiet:!1},{Provider:n,Consumer:o}},Object.defineProperty(t,"__esModule",{value:!0})});