UNPKG

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