UNPKG

2.71 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("prop-types")):"function"==typeof define&&define.amd?define(["exports","react","prop-types"],e):e(t.statty={},t.React,t.PropTypes)}(this,function(t,e,s){"use strict";s=s&&s.hasOwnProperty("default")?s.default:s;var r=function(s){function t(t,e){s.call(this,t,e),this.broadcast=e.__statty__.broadcast,this.inspect=e.__statty__.inspect,this.state=t.state?t.state:t.select(this.broadcast.getState()),this.update=this.update.bind(this),this.setStateIfNeeded=this.setStateIfNeeded.bind(this)}return s&&(t.__proto__=s),((t.prototype=Object.create(s&&s.prototype)).constructor=t).prototype.update=function(t){if(this.props.state)this.setState(t);else{var e=this.broadcast.getState(),s=t(e);this.inspect&&this.inspect(e,s,t),this.broadcast.setState(s)}},t.prototype.setStateIfNeeded=function(t){var e,s,r=this.state,o=this.props.select(t);s=o,"object"==typeof(e=r)&&"object"==typeof s&&null!==e&&null!==s&&function(t,e){if(t===e)return!0;for(var s in t)if(!(s in e))return!1;for(var s in e)if(t[s]!==e[s])return!1;return!0}(r,o)||this.setState(o)},t.prototype.componentDidMount=function(){this.props.state||(this.subscriptionId=this.broadcast.subscribe(this.setStateIfNeeded),this.setStateIfNeeded(this.broadcast.getState()))},t.prototype.componentWillUnmount=function(){this.subscriptionId&&this.broadcast.unsubscribe(this.subscriptionId)},t.prototype.render=function(){return this.props.render(this.props.select(this.props.state?this.state:this.broadcast.getState()),this.update)||null},t}(e.Component);r.defaultProps={select:function(t){return t},render:function(){return null}},r.contextTypes={__statty__:s.object.isRequired},r.propTypes={state:s.object,render:s.func,select:s.func};var o=function(i){function t(t,e){var s,o,r,n;i.call(this,t,e),this.broadcast=(s=t.state,o={},r=1,n=s,{getState:function(){return n},setState:function(t){n=t;for(var e=Object.keys(o),s=0,r=e.length;s<r;s++)o[e[s]]&&o[e[s]](t)},subscribe:function(t){if("function"!=typeof t)throw new Error("listener must be a function.");var e=r;return o[e]=t,r+=1,e},unsubscribe:function(t){o[t]=void 0}})}return i&&(t.__proto__=i),((t.prototype=Object.create(i&&i.prototype)).constructor=t).prototype.getChildContext=function(){return{__statty__:{broadcast:this.broadcast,inspect:this.props.inspect}}},t.prototype.render=function(){var t=this.props.children;return Array.isArray(t)?t[0]:t},t}(e.Component);o.childContextTypes={__statty__:s.object},o.propTypes={inspect:s.func,state:s.object.isRequired,children:s.node.isRequired},t.Provider=o,t.State=r,Object.defineProperty(t,"__esModule",{value:!0})});
2//# sourceMappingURL=statty.umd.min.js.map