UNPKG

react-network-info

Version:
151 lines (119 loc) 6.12 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } 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; } /** * High Order Component * that provides info about the connection type/speed * to the wrapped components. * You can use it to hide/show a section of a page based * on users internet type/speed. * <br/><br/> * [![Build Status](https://travis-ci.org/AvraamMavridis/react-network-info.svg?branch=master)](https://travis-ci.org/AvraamMavridis/react-network-info) [![Greenkeeper badge](https://badges.greenkeeper.io/AvraamMavridis/react-network-info.svg)](https://greenkeeper.io/) <br/> <a href="https://nodei.co/npm/react-network-info/"><img src="https://nodei.co/npm/react-network-info.png?mini=true"></a><br/><br/> * * * The following properties are passed to the wrapped components * - <strong>downlink</strong> : The effective bandwidth estimate in megabits per secondrounded to the nearest multiple of 25 kilobits per seconds. * - <strong>effectiveType</strong> : The effective type of the connection meaning one of 'slow-2g', '2g', '3g', or '4g'. This value is determined using a combination of recently observed, round-trip time and downlink values. * - <strong>rtt</strong> : The estimated effective round-trip time of the current connection, rounded to the nearest multiple of 25 milliseconds. * * @class NetworkInformation * @extends {Component} */ var NetworkInformation = function (_Component) { _inherits(NetworkInformation, _Component); function NetworkInformation() { var _ref; var _temp, _this, _ret; _classCallCheck(this, NetworkInformation); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = NetworkInformation.__proto__ || Object.getPrototypeOf(NetworkInformation)).call.apply(_ref, [this].concat(args))), _this), _this.state = { downlink: Infinity, effectiveType: undefined, rtt: Infinity }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(NetworkInformation, [{ key: 'updateConnectionStatus', /** * Update the connection info */ value: function updateConnectionStatus() { this.setState({ /** * Returns the effective bandwidth estimate in megabits per second, * rounded to the nearest multiple of 25 kilobits per seconds. */ downlink: this.connection.downlink, /** * Returns the effective type of the connection * meaning one of 'slow-2g', '2g', '3g', or '4g'. * This value is determined using a combination of * recently observed, round-trip time and downlink values. */ effectiveType: this.connection.effectiveType, /** * Returns the estimated effective round-trip * time of the current connection, rounded to * the nearest multiple of 25 milliseconds. */ rtt: this.connection.rtt }); } /** * Attach listener for the connection */ }, { key: 'componentDidMount', value: function componentDidMount() { this.connection = window.navigator.connection || window.navigator.mozConnection || window.navigator.webkitConnection; if (this.connection) { this.updateConnectionStatus(); this.connection.addEventListener('typechange', this.updateConnectionStatus); } } /** * Remove listener */ }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (this.connection) { this.connection.removeEventListener('typechange', this.updateConnectionStatus); } } /** * Render component * * @returns {JSX.Element} */ }, { key: 'render', value: function render() { var _this2 = this; var children = this.props.children; if (typeof children === 'function') { return children(this.state); } return _react2.default.Children.map(children, function (child) { return _react2.default.cloneElement(child, _this2.state); }); } }]); return NetworkInformation; }(_react.Component); NetworkInformation.propTypes = { children: _propTypes2.default.oneOfType([_propTypes2.default.node, _propTypes2.default.func]) }; exports.default = NetworkInformation;