UNPKG

2.02 kBJavaScriptView Raw
1/** @license React v16.11.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 k=require("react");function l(a){for(var d="https://reactjs.org/docs/error-decoder.html?invariant="+a,f=1;f<arguments.length;f++)d+="&args[]="+encodeURIComponent(arguments[f]);return"Minified React error #"+a+"; visit "+d+" for the full message or use the non-minified dev environment for full errors and additional helpful warnings."}
11function m(a,d){a.prototype=Object.create(d.prototype);a.prototype.constructor=a;a.__proto__=d}
12exports.createSubscription=function(a){var d=a.getCurrentValue,f=a.subscribe;return function(a){function g(){var b=arguments.length;for(var h=Array(b),c=0;c<b;c++)h[c]=arguments[c];b=a.call.apply(a,[this].concat(h))||this;b.state={source:b.props.source,value:null!=b.props.source?d(b.props.source):void 0};b._hasUnmounted=!1;b._unsubscribe=null;return b}m(g,a);g.getDerivedStateFromProps=function(b,a){return b.source!==a.source?{source:b.source,value:null!=b.source?d(b.source):void 0}:null};var e=g.prototype;
13e.componentDidMount=function(){this.subscribe()};e.componentDidUpdate=function(b,a){this.state.source!==a.source&&(this.unsubscribe(),this.subscribe())};e.componentWillUnmount=function(){this.unsubscribe();this._hasUnmounted=!0};e.render=function(){return this.props.children(this.state.value)};e.subscribe=function(){var b=this,a=this.state.source;if(null!=a){var c=f(a,function(c){b._hasUnmounted||b.setState(function(b){return c===b.value||a!==b.source?null:{value:c}})});if("function"!==typeof c)throw Error(l(266));
14this._unsubscribe=c;c=d(this.props.source);c!==this.state.value&&this.setState({value:c})}};e.unsubscribe=function(){"function"===typeof this._unsubscribe&&this._unsubscribe();this._unsubscribe=null};return g}(k.Component)};