1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 |
|
13 |
|
14 | if (process.env.NODE_ENV !== "production") {
|
15 | (function() {
|
16 | 'use strict';
|
17 |
|
18 | Object.defineProperty(exports, '__esModule', { value: true });
|
19 |
|
20 | var React = require('react');
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function ReactError(error) {
|
30 | error.name = 'Invariant Violation';
|
31 | return error;
|
32 | }
|
33 |
|
34 | function _inheritsLoose(subClass, superClass) {
|
35 | subClass.prototype = Object.create(superClass.prototype);
|
36 | subClass.prototype.constructor = subClass;
|
37 | subClass.__proto__ = superClass;
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | var 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 |
|
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);
|
83 |
|
84 |
|
85 | Function.prototype.apply.call(console.error, console, argsWithFormat);
|
86 | }
|
87 |
|
88 | try {
|
89 |
|
90 |
|
91 |
|
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 |
|
101 | var warningWithoutStack$1 = warningWithoutStack;
|
102 |
|
103 | function 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 |
|
110 | var Subscription =
|
111 |
|
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();
|
158 |
|
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 |
|
180 | if (value === state.value) {
|
181 | return null;
|
182 | }
|
183 |
|
184 |
|
185 | if (source !== state.source) {
|
186 | return null;
|
187 | }
|
188 |
|
189 | return {
|
190 | value: value
|
191 | };
|
192 | });
|
193 | };
|
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 | })();
|
205 |
|
206 |
|
207 |
|
208 | this._unsubscribe = unsubscribe;
|
209 |
|
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 |
|
235 | exports.createSubscription = createSubscription;
|
236 | })();
|
237 | }
|