UNPKG

singularityui-tailer

Version:
272 lines (225 loc) 10.5 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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 _reactRedux = require('react-redux'); var _immutable = require('immutable'); var _immutable2 = _interopRequireDefault(_immutable); var _actions = require('../actions'); var _connectToTailer = require('./connectToTailer'); var _connectToTailer2 = _interopRequireDefault(_connectToTailer); var _selectors = require('../selectors'); var Selectors = _interopRequireWildcard(_selectors); var _Log = require('./Log'); var _Log2 = _interopRequireDefault(_Log); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } 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; } var SandboxTailer = function (_Component) { _inherits(SandboxTailer, _Component); function SandboxTailer() { _classCallCheck(this, SandboxTailer); var _this = _possibleConstructorReturn(this, (SandboxTailer.__proto__ || Object.getPrototypeOf(SandboxTailer)).call(this)); _this.initializeFile = _this.initializeFile.bind(_this); _this.loadLine = _this.loadLine.bind(_this); _this.tailLog = _this.tailLog.bind(_this); return _this; } _createClass(SandboxTailer, [{ key: 'initializeFile', value: function initializeFile(atOffset) { this.props.unloadFile(); if (atOffset !== undefined) { if (atOffset === -1) { this.props.fetchTail(); } else { this.props.fetchLength(); this.fetchSafe(atOffset, atOffset + _actions.SANDBOX_MAX_BYTES); } } else { this.props.fetchLength(); this.fetchSafe(0, _actions.SANDBOX_MAX_BYTES); } } }, { key: 'fetchSafe', value: function fetchSafe(byteRangeStart, byteRangeEnd) { var fetchChunk = this.props.fetchChunk; // if already in flight, don't request again if (!this.props.requests.has(byteRangeStart)) { return fetchChunk(byteRangeStart, byteRangeEnd); } return Promise.resolve(); } }, { key: 'loadLine', value: function loadLine(index, loadUp, lines, chunks) { var byteRangeStart = void 0; var byteRangeEnd = void 0; var resultPromise = Promise.resolve(); if (index < lines.size) { var lineToLoad = lines.get(index); if (loadUp) { byteRangeEnd = lineToLoad.end; byteRangeStart = Math.max(0, byteRangeEnd - _actions.SANDBOX_MAX_BYTES); } else { byteRangeStart = lineToLoad.start; // if this is the last line, and not a missing marker if (index === lines.size - 1 && !lineToLoad.isMissingMarker) { // we have to load from the end of this line instead of the beginning byteRangeStart = lineToLoad.end; } byteRangeEnd = byteRangeStart + _actions.SANDBOX_MAX_BYTES; } resultPromise = this.fetchSafe(byteRangeStart, byteRangeEnd); var MIN_LOADED_LINES_TO_TRIGGER_UNLOAD = 800; var MIN_LOADED_CHUNKS_TO_TRIGGER_UNLOAD = 5; if (lines.size >= MIN_LOADED_LINES_TO_TRIGGER_UNLOAD && chunks.size >= MIN_LOADED_CHUNKS_TO_TRIGGER_UNLOAD) { if (loadUp) { // remove bottom this.props.unloadFileChunk(-1); } else { // remove top this.props.unloadFileChunk(0); } } } return resultPromise; } }, { key: 'loadLines', value: function loadLines(startIndex, stopIndex, lines) { var byteRangeStart = void 0; var byteRangeEnd = void 0; if (startIndex < lines.size) { byteRangeStart = lines.get(startIndex).start; } else if (lines.size === 0) { byteRangeStart = 0; } else { byteRangeStart = lines.last().end; } if (stopIndex < lines.size) { byteRangeEnd = Math.min(lines.get(stopIndex).end, byteRangeStart + _actions.SANDBOX_MAX_BYTES); } else { byteRangeEnd = byteRangeStart + _actions.SANDBOX_MAX_BYTES; } this.fetchSafe(byteRangeStart, byteRangeEnd); } }, { key: 'tailLog', value: function tailLog(lines) { if (lines.size) { var lastLine = lines.last(); this.fetchSafe(lastLine.end, lastLine.end + _actions.SANDBOX_MAX_BYTES); } else { this.fetchSafe(0, _actions.SANDBOX_MAX_BYTES); } } }, { key: 'render', value: function render() { return _react2.default.createElement(_Log2.default, { tailerId: this.props.tailerId, initializeFile: this.initializeFile, loadLine: this.loadLine, tailLog: this.tailLog, goToOffset: this.props.goToOffset, lineLinkRenderer: this.props.lineLinkRenderer, startTailing: this.props.startTailing, stopTailing: this.props.stopTailing }); } }]); return SandboxTailer; }(_react.Component); SandboxTailer.propTypes = { tailerId: _react.PropTypes.string.isRequired, taskId: _react.PropTypes.string.isRequired, path: _react.PropTypes.string.isRequired, requests: _react.PropTypes.instanceOf(_immutable2.default.Map).isRequired, fetchLength: _react.PropTypes.func.isRequired, fetchChunk: _react.PropTypes.func.isRequired, fetchTail: _react.PropTypes.func.isRequired, unloadFile: _react.PropTypes.func.isRequired, unloadFileChunk: _react.PropTypes.func.isRequired, goToOffset: _react.PropTypes.number, lineLinkRenderer: _react.PropTypes.func, startTailing: _react.PropTypes.func.isRequired, stopTailing: _react.PropTypes.func.isRequired }; var mapStateToProps = function mapStateToProps(state, ownProps) { return { requests: Selectors.getRequests(state, ownProps), config: Selectors.getConfig(state, ownProps) }; }; var mapDispatchToProps = function mapDispatchToProps(dispatch, ownProps) { return { fetchLength: function fetchLength(config) { return dispatch((0, _actions.sandboxFetchLength)(ownProps.tailerId, ownProps.taskId, ownProps.path, config)); }, fetchChunk: function fetchChunk(start, end, config) { return dispatch((0, _actions.sandboxFetchChunk)(ownProps.tailerId, ownProps.taskId, ownProps.path, start, end, config)); }, fetchTail: function fetchTail(config) { return dispatch((0, _actions.sandboxFetchTail)(ownProps.tailerId, ownProps.taskId, ownProps.path, config)); }, unloadFile: function unloadFile() { return dispatch((0, _actions.unloadFile)(ownProps.tailerId)); }, unloadFileChunk: function unloadFileChunk(index) { return dispatch((0, _actions.unloadFileChunk)(ownProps.tailerId, index)); }, startTailing: function startTailing() { return dispatch((0, _actions.startTailing)(ownProps.tailerId)); }, stopTailing: function stopTailing() { return dispatch((0, _actions.stopTailing)(ownProps.tailerId)); } }; }; var mergeProps = function mergeProps(stateProps, dispatchProps, ownProps) { return _extends({}, stateProps, ownProps, { fetchLength: function fetchLength() { return dispatchProps.fetchLength(stateProps.config); }, fetchChunk: function fetchChunk(start, end) { return dispatchProps.fetchChunk(start, end, stateProps.config); }, fetchTail: function fetchTail() { return dispatchProps.fetchTail(stateProps.config); }, unloadFile: function unloadFile() { return dispatchProps.unloadFile(); }, unloadFileChunk: function unloadFileChunk(start) { return dispatchProps.unloadFileChunk(start); }, startTailing: function startTailing() { return dispatchProps.startTailing(); }, stopTailing: function stopTailing() { return dispatchProps.stopTailing(); } }); }; var _default = (0, _connectToTailer2.default)((0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps, mergeProps)(SandboxTailer)); exports.default = _default; ; var _temp = function () { if (typeof __REACT_HOT_LOADER__ === 'undefined') { return; } __REACT_HOT_LOADER__.register(SandboxTailer, 'SandboxTailer', 'src/components/SandboxTailer.js'); __REACT_HOT_LOADER__.register(mapStateToProps, 'mapStateToProps', 'src/components/SandboxTailer.js'); __REACT_HOT_LOADER__.register(mapDispatchToProps, 'mapDispatchToProps', 'src/components/SandboxTailer.js'); __REACT_HOT_LOADER__.register(mergeProps, 'mergeProps', 'src/components/SandboxTailer.js'); __REACT_HOT_LOADER__.register(_default, 'default', 'src/components/SandboxTailer.js'); }(); ;