UNPKG

2.05 kBJavaScriptView Raw
1/** @license React v16.10.0
2 * create-subscription.production.min.js
3 *
4 * Copyright (c) Facebook, Inc. and its affiliates.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the root directory of this source tree.
8 */
9
10'use strict';Object.defineProperty(exports,"__esModule",{value:!0});var l=require("react");function m(a){for(var e=a.message,f="https://reactjs.org/docs/error-decoder.html?invariant="+e,h=1;h<arguments.length;h++)f+="&args[]="+encodeURIComponent(arguments[h]);a.message="Minified React error #"+e+"; visit "+f+" for the full message or use the non-minified dev environment for full errors and additional helpful warnings. ";return a}
11function n(a,e){a.prototype=Object.create(e.prototype);a.prototype.constructor=a;a.__proto__=e}
12exports.createSubscription=function(a){var e=a.getCurrentValue,f=a.subscribe;return function(a){function g(){var b=arguments.length;for(var k=Array(b),c=0;c<b;c++)k[c]=arguments[c];b=a.call.apply(a,[this].concat(k))||this;b.state={source:b.props.source,value:null!=b.props.source?e(b.props.source):void 0};b._hasUnmounted=!1;b._unsubscribe=null;return b}n(g,a);g.getDerivedStateFromProps=function(b,a){return b.source!==a.source?{source:b.source,value:null!=b.source?e(b.source):void 0}:null};var d=g.prototype;
13d.componentDidMount=function(){this.subscribe()};d.componentDidUpdate=function(b,a){this.state.source!==a.source&&(this.unsubscribe(),this.subscribe())};d.componentWillUnmount=function(){this.unsubscribe();this._hasUnmounted=!0};d.render=function(){return this.props.children(this.state.value)};d.subscribe=function(){var a=this,d=this.state.source;if(null!=d){var c=f(d,function(b){a._hasUnmounted||a.setState(function(a){return b===a.value||d!==a.source?null:{value:b}})});if("function"!==typeof c)throw m(Error(266));
14this._unsubscribe=c;c=e(this.props.source);c!==this.state.value&&this.setState({value:c})}};d.unsubscribe=function(){"function"===typeof this._unsubscribe&&this._unsubscribe();this._unsubscribe=null};return g}(l.Component)};