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 | function _inheritsLoose(subClass, superClass) {
|
27 | subClass.prototype = Object.create(superClass.prototype);
|
28 | subClass.prototype.constructor = subClass;
|
29 | subClass.__proto__ = superClass;
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | var 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 |
|
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);
|
75 |
|
76 |
|
77 | Function.prototype.apply.call(console.error, console, argsWithFormat);
|
78 | }
|
79 |
|
80 | try {
|
81 |
|
82 |
|
83 |
|
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 |
|
93 | var warningWithoutStack$1 = warningWithoutStack;
|
94 |
|
95 | function 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 |
|
102 | var Subscription =
|
103 |
|
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();
|
150 |
|
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 |
|
172 | if (value === state.value) {
|
173 | return null;
|
174 | }
|
175 |
|
176 |
|
177 | if (source !== state.source) {
|
178 | return null;
|
179 | }
|
180 |
|
181 | return {
|
182 | value: value
|
183 | };
|
184 | });
|
185 | };
|
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 | }
|
195 |
|
196 |
|
197 |
|
198 | this._unsubscribe = unsubscribe;
|
199 |
|
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 |
|
225 | exports.createSubscription = createSubscription;
|
226 | })();
|
227 | }
|