UNPKG

6.95 kBJavaScriptView Raw
1/** @license React v16.11.0
2 * create-subscription.development.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';
11
12
13
14if (process.env.NODE_ENV !== "production") {
15 (function() {
16'use strict';
17
18Object.defineProperty(exports, '__esModule', { value: true });
19
20var React = require('react');
21
22// Do not require this module directly! Use normal `invariant` calls with
23// template literal strings. The messages will be replaced with error codes
24// during build.
25
26function _inheritsLoose(subClass, superClass) {
27 subClass.prototype = Object.create(superClass.prototype);
28 subClass.prototype.constructor = subClass;
29 subClass.__proto__ = superClass;
30}
31
32/**
33 * Use invariant() to assert state which your program assumes to be true.
34 *
35 * Provide sprintf-style format (only %s is supported) and arguments
36 * to provide information about what broke and what you were
37 * expecting.
38 *
39 * The invariant message will be stripped in production, but the invariant
40 * will remain to ensure logic does not differ in production.
41 */
42
43/**
44 * Similar to invariant but only logs a warning if the condition is not met.
45 * This can be used to log issues in development environments in critical
46 * paths. Removing the logging code for production environments will keep the
47 * same logic and follow the same code paths.
48 */
49var warningWithoutStack = function () {};
50
51{
52 warningWithoutStack = function (condition, format) {
53 for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
54 args[_key - 2] = arguments[_key];
55 }
56
57 if (format === undefined) {
58 throw new Error('`warningWithoutStack(condition, format, ...args)` requires a warning ' + 'message argument');
59 }
60
61 if (args.length > 8) {
62 // Check before the condition to catch violations early.
63 throw new Error('warningWithoutStack() currently supports at most 8 arguments.');
64 }
65
66 if (condition) {
67 return;
68 }
69
70 if (typeof console !== 'undefined') {
71 var argsWithFormat = args.map(function (item) {
72 return '' + item;
73 });
74 argsWithFormat.unshift('Warning: ' + format); // We intentionally don't use spread (or .apply) directly because it
75 // breaks IE9: https://github.com/facebook/react/issues/13610
76
77 Function.prototype.apply.call(console.error, console, argsWithFormat);
78 }
79
80 try {
81 // --- Welcome to debugging React ---
82 // This error was thrown as a convenience so that you can use this stack
83 // to find the callsite that caused this warning to fire.
84 var argIndex = 0;
85 var message = 'Warning: ' + format.replace(/%s/g, function () {
86 return args[argIndex++];
87 });
88 throw new Error(message);
89 } catch (x) {}
90 };
91}
92
93var warningWithoutStack$1 = warningWithoutStack;
94
95function createSubscription(config) {
96 var getCurrentValue = config.getCurrentValue,
97 _subscribe = config.subscribe;
98 !(typeof getCurrentValue === 'function') ? warningWithoutStack$1(false, 'Subscription must specify a getCurrentValue function') : void 0;
99 !(typeof _subscribe === 'function') ? warningWithoutStack$1(false, 'Subscription must specify a subscribe function') : void 0;
100
101 // Reference: https://gist.github.com/bvaughn/d569177d70b50b58bff69c3c4a5353f3
102 var Subscription =
103 /*#__PURE__*/
104 function (_React$Component) {
105 _inheritsLoose(Subscription, _React$Component);
106
107 function Subscription() {
108 var _this;
109
110 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
111 args[_key] = arguments[_key];
112 }
113
114 _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
115 _this.state = {
116 source: _this.props.source,
117 value: _this.props.source != null ? getCurrentValue(_this.props.source) : undefined
118 };
119 _this._hasUnmounted = false;
120 _this._unsubscribe = null;
121 return _this;
122 }
123
124 Subscription.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, prevState) {
125 if (nextProps.source !== prevState.source) {
126 return {
127 source: nextProps.source,
128 value: nextProps.source != null ? getCurrentValue(nextProps.source) : undefined
129 };
130 }
131
132 return null;
133 };
134
135 var _proto = Subscription.prototype;
136
137 _proto.componentDidMount = function componentDidMount() {
138 this.subscribe();
139 };
140
141 _proto.componentDidUpdate = function componentDidUpdate(prevProps, prevState) {
142 if (this.state.source !== prevState.source) {
143 this.unsubscribe();
144 this.subscribe();
145 }
146 };
147
148 _proto.componentWillUnmount = function componentWillUnmount() {
149 this.unsubscribe(); // Track mounted to avoid calling setState after unmounting
150 // For source like Promises that can't be unsubscribed from.
151
152 this._hasUnmounted = true;
153 };
154
155 _proto.render = function render() {
156 return this.props.children(this.state.value);
157 };
158
159 _proto.subscribe = function subscribe() {
160 var _this2 = this;
161
162 var source = this.state.source;
163
164 if (source != null) {
165 var _callback = function (value) {
166 if (_this2._hasUnmounted) {
167 return;
168 }
169
170 _this2.setState(function (state) {
171 // If the value is the same, skip the unnecessary state update.
172 if (value === state.value) {
173 return null;
174 } // If this event belongs to an old or uncommitted data source, ignore it.
175
176
177 if (source !== state.source) {
178 return null;
179 }
180
181 return {
182 value: value
183 };
184 });
185 }; // Store the unsubscribe method for later (in case the subscribable prop changes).
186
187
188 var unsubscribe = _subscribe(source, _callback);
189
190 if (!(typeof unsubscribe === 'function')) {
191 {
192 throw Error("A subscription must return an unsubscribe function.");
193 }
194 } // It's safe to store unsubscribe on the instance because
195 // We only read or write that property during the "commit" phase.
196
197
198 this._unsubscribe = unsubscribe; // External values could change between render and mount,
199 // In some cases it may be important to handle this case.
200
201 var _value = getCurrentValue(this.props.source);
202
203 if (_value !== this.state.value) {
204 this.setState({
205 value: _value
206 });
207 }
208 }
209 };
210
211 _proto.unsubscribe = function unsubscribe() {
212 if (typeof this._unsubscribe === 'function') {
213 this._unsubscribe();
214 }
215
216 this._unsubscribe = null;
217 };
218
219 return Subscription;
220 }(React.Component);
221
222 return Subscription;
223}
224
225exports.createSubscription = createSubscription;
226 })();
227}