|
3 | 3 | Object.defineProperty(exports, "__esModule", { |
4 | 4 | value: true |
5 | 5 | }); |
6 | | -exports.Detector = exports.Offline = exports.Online = undefined; |
7 | | - |
8 | | -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); |
9 | | - |
10 | | -var _react = require("react"); |
11 | | - |
12 | | -var _propTypes = require("prop-types"); |
13 | | - |
14 | | -var _propTypes2 = _interopRequireDefault(_propTypes); |
15 | | - |
16 | | -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
17 | | - |
18 | | -function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } |
19 | | - |
20 | | -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } |
21 | | - |
22 | | -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } |
23 | | - |
24 | | -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } |
25 | | - |
26 | | -var inBrowser = typeof navigator !== "undefined"; |
27 | | - |
28 | | -// these browsers don't fully support navigator.onLine, so we need to use a polling backup |
29 | | -var unsupportedUserAgentsPattern = /Windows.*Chrome|Windows.*Firefox|Linux.*Chrome/; |
30 | | - |
31 | | -var ping = function ping(_ref) { |
32 | | - var url = _ref.url, |
33 | | - timeout = _ref.timeout; |
34 | | - |
35 | | - return new Promise(function (resolve) { |
36 | | - var isOnline = function isOnline() { |
37 | | - return resolve(true); |
38 | | - }; |
39 | | - var isOffline = function isOffline() { |
40 | | - return resolve(false); |
41 | | - }; |
42 | | - |
43 | | - var xhr = new XMLHttpRequest(); |
44 | | - |
45 | | - xhr.onerror = isOffline; |
46 | | - xhr.ontimeout = isOffline; |
47 | | - xhr.onreadystatechange = function () { |
48 | | - if (xhr.readyState === xhr.HEADERS_RECEIVED) { |
49 | | - if (xhr.status) { |
50 | | - isOnline(); |
51 | | - } else { |
52 | | - isOffline(); |
53 | | - } |
54 | | - } |
55 | | - }; |
56 | | - |
57 | | - xhr.open("HEAD", url); |
58 | | - xhr.timeout = timeout; |
59 | | - xhr.send(); |
60 | | - }); |
61 | | -}; |
62 | | - |
63 | | -var propTypes = { |
64 | | - children: _propTypes2.default.node, |
65 | | - onChange: _propTypes2.default.func, |
66 | | - polling: _propTypes2.default.oneOfType([_propTypes2.default.shape({ |
67 | | - url: _propTypes2.default.string, |
68 | | - interval: _propTypes2.default.number, |
69 | | - timeout: _propTypes2.default.number |
70 | | - }), _propTypes2.default.bool]), |
71 | | - wrapperType: _propTypes2.default.string |
72 | | -}; |
73 | | - |
74 | | -var defaultProps = { |
75 | | - polling: true, |
76 | | - wrapperType: "span" |
77 | | -}; |
78 | | - |
79 | | -var defaultPollingConfig = { |
80 | | - enabled: inBrowser && unsupportedUserAgentsPattern.test(navigator.userAgent), |
81 | | - url: "https://ipv4.icanhazip.com/", |
82 | | - timeout: 5000, |
83 | | - interval: 5000 |
84 | | -}; |
85 | | - |
86 | | -// base class that detects offline/online changes |
87 | | - |
88 | | -var Base = function (_Component) { |
89 | | - _inherits(Base, _Component); |
90 | | - |
91 | | - function Base() { |
92 | | - _classCallCheck(this, Base); |
93 | | - |
94 | | - var _this = _possibleConstructorReturn(this, (Base.__proto__ || Object.getPrototypeOf(Base)).call(this)); |
95 | | - |
96 | | - _this.state = { |
97 | | - online: inBrowser && typeof navigator.onLine === "boolean" ? navigator.onLine : true |
98 | | - }; |
99 | | - // bind event handlers |
100 | | - _this.goOnline = _this.goOnline.bind(_this); |
101 | | - _this.goOffline = _this.goOffline.bind(_this); |
102 | | - return _this; |
| 6 | +Object.defineProperty(exports, "Online", { |
| 7 | + enumerable: true, |
| 8 | + get: function get() { |
| 9 | + return _Online["default"]; |
103 | 10 | } |
104 | | - |
105 | | - _createClass(Base, [{ |
106 | | - key: "componentDidMount", |
107 | | - value: function componentDidMount() { |
108 | | - window.addEventListener("online", this.goOnline); |
109 | | - window.addEventListener("offline", this.goOffline); |
110 | | - |
111 | | - if (this.getPollingConfig().enabled) { |
112 | | - this.startPolling(); |
113 | | - } |
114 | | - } |
115 | | - }, { |
116 | | - key: "componentWillUnmount", |
117 | | - value: function componentWillUnmount() { |
118 | | - window.removeEventListener("online", this.goOnline); |
119 | | - window.removeEventListener("offline", this.goOffline); |
120 | | - |
121 | | - if (this.pollingId) { |
122 | | - this.stopPolling(); |
123 | | - } |
124 | | - } |
125 | | - }, { |
126 | | - key: "renderChildren", |
127 | | - value: function renderChildren() { |
128 | | - var _props = this.props, |
129 | | - children = _props.children, |
130 | | - wrapperType = _props.wrapperType; |
131 | | - |
132 | | - // usual case: one child that is a react Element |
133 | | - |
134 | | - if ((0, _react.isValidElement)(children)) { |
135 | | - return children; |
136 | | - } |
137 | | - |
138 | | - // no children |
139 | | - if (!children) { |
140 | | - return null; |
141 | | - } |
142 | | - |
143 | | - // string children, multiple children, or something else |
144 | | - return _react.createElement.apply(undefined, [wrapperType, {}].concat(_toConsumableArray(_react.Children.toArray(children)))); |
145 | | - } |
146 | | - }, { |
147 | | - key: "getPollingConfig", |
148 | | - value: function getPollingConfig() { |
149 | | - switch (this.props.polling) { |
150 | | - case true: |
151 | | - return defaultPollingConfig; |
152 | | - case false: |
153 | | - return { enabled: false }; |
154 | | - default: |
155 | | - return Object.assign({}, defaultPollingConfig, this.props.polling); |
156 | | - } |
157 | | - } |
158 | | - }, { |
159 | | - key: "goOnline", |
160 | | - value: function goOnline() { |
161 | | - if (!this.state.online) { |
162 | | - this.callOnChangeHandler(true); |
163 | | - this.setState({ online: true }); |
164 | | - } |
165 | | - } |
166 | | - }, { |
167 | | - key: "goOffline", |
168 | | - value: function goOffline() { |
169 | | - if (this.state.online) { |
170 | | - this.callOnChangeHandler(false); |
171 | | - this.setState({ online: false }); |
172 | | - } |
173 | | - } |
174 | | - }, { |
175 | | - key: "callOnChangeHandler", |
176 | | - value: function callOnChangeHandler(online) { |
177 | | - if (this.props.onChange) { |
178 | | - this.props.onChange(online); |
179 | | - } |
180 | | - } |
181 | | - }, { |
182 | | - key: "startPolling", |
183 | | - value: function startPolling() { |
184 | | - var _this2 = this; |
185 | | - |
186 | | - var _getPollingConfig = this.getPollingConfig(), |
187 | | - interval = _getPollingConfig.interval; |
188 | | - |
189 | | - this.pollingId = setInterval(function () { |
190 | | - var _getPollingConfig2 = _this2.getPollingConfig(), |
191 | | - url = _getPollingConfig2.url, |
192 | | - timeout = _getPollingConfig2.timeout; |
193 | | - |
194 | | - ping({ url: url, timeout: timeout }).then(function (online) { |
195 | | - online ? _this2.goOnline() : _this2.goOffline(); |
196 | | - }); |
197 | | - }, interval); |
198 | | - } |
199 | | - }, { |
200 | | - key: "stopPolling", |
201 | | - value: function stopPolling() { |
202 | | - clearInterval(this.pollingId); |
203 | | - } |
204 | | - }]); |
205 | | - |
206 | | - return Base; |
207 | | -}(_react.Component); |
208 | | - |
209 | | -Base.propTypes = propTypes; |
210 | | -Base.defaultProps = defaultProps; |
211 | | - |
212 | | -var Online = exports.Online = function (_Base) { |
213 | | - _inherits(Online, _Base); |
214 | | - |
215 | | - function Online() { |
216 | | - _classCallCheck(this, Online); |
217 | | - |
218 | | - return _possibleConstructorReturn(this, (Online.__proto__ || Object.getPrototypeOf(Online)).apply(this, arguments)); |
| 11 | +}); |
| 12 | +Object.defineProperty(exports, "Offline", { |
| 13 | + enumerable: true, |
| 14 | + get: function get() { |
| 15 | + return _Offline["default"]; |
219 | 16 | } |
220 | | - |
221 | | - _createClass(Online, [{ |
222 | | - key: "render", |
223 | | - value: function render() { |
224 | | - return this.state.online ? this.renderChildren() : null; |
225 | | - } |
226 | | - }]); |
227 | | - |
228 | | - return Online; |
229 | | -}(Base); |
230 | | - |
231 | | -Online.propTypes = propTypes; |
232 | | -Online.defaultProps = defaultProps; |
233 | | - |
234 | | -var Offline = exports.Offline = function (_Base2) { |
235 | | - _inherits(Offline, _Base2); |
236 | | - |
237 | | - function Offline() { |
238 | | - _classCallCheck(this, Offline); |
239 | | - |
240 | | - return _possibleConstructorReturn(this, (Offline.__proto__ || Object.getPrototypeOf(Offline)).apply(this, arguments)); |
| 17 | +}); |
| 18 | +Object.defineProperty(exports, "Detector", { |
| 19 | + enumerable: true, |
| 20 | + get: function get() { |
| 21 | + return _Detector["default"]; |
241 | 22 | } |
| 23 | +}); |
| 24 | +Object.defineProperty(exports, "useConnection", { |
| 25 | + enumerable: true, |
| 26 | + get: function get() { |
| 27 | + return _useConnection["default"]; |
| 28 | + } |
| 29 | +}); |
242 | 30 |
|
243 | | - _createClass(Offline, [{ |
244 | | - key: "render", |
245 | | - value: function render() { |
246 | | - return !this.state.online ? this.renderChildren() : null; |
247 | | - } |
248 | | - }]); |
249 | | - |
250 | | - return Offline; |
251 | | -}(Base); |
252 | | - |
253 | | -Offline.propTypes = propTypes; |
254 | | -Offline.defaultProps = defaultProps; |
255 | | - |
256 | | -var Detector = exports.Detector = function (_Base3) { |
257 | | - _inherits(Detector, _Base3); |
258 | | - |
259 | | - function Detector() { |
260 | | - _classCallCheck(this, Detector); |
| 31 | +var _Online = _interopRequireDefault(require("./Online")); |
261 | 32 |
|
262 | | - return _possibleConstructorReturn(this, (Detector.__proto__ || Object.getPrototypeOf(Detector)).apply(this, arguments)); |
263 | | - } |
| 33 | +var _Offline = _interopRequireDefault(require("./Offline")); |
264 | 34 |
|
265 | | - _createClass(Detector, [{ |
266 | | - key: "render", |
267 | | - value: function render() { |
268 | | - return this.props.render({ online: this.state.online }); |
269 | | - } |
270 | | - }]); |
| 35 | +var _Detector = _interopRequireDefault(require("./Detector")); |
271 | 36 |
|
272 | | - return Detector; |
273 | | -}(Base); |
| 37 | +var _useConnection = _interopRequireDefault(require("./useConnection")); |
274 | 38 |
|
275 | | -Detector.propTypes = Object.assign({}, propTypes, { |
276 | | - render: _propTypes2.default.func.isRequired |
277 | | -}); |
278 | | -Detector.defaultProps = defaultProps; |
| 39 | +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } |
0 commit comments