UNPKG

@bigfishtv/cockpit

Version:

63 lines (46 loc) 2.89 kB
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; }; 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; } /** * @module Decorators/persistState */ import React, { Component } from 'react'; import { getUploader } from '../api/tankUpload'; var defaultEvents = ['StateChanged', 'QueueChanged', 'UploadProgress']; /** * Decorator for providing uploader class to wrapped component along with forceUpdate hooks * @param {String[]} events - uploader events that should force an update, default is StateChanged and UploadProgress * @return {Component} returns wrapped component */ export default function wrapUploader() { var events = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultEvents; return function (WrappedComponent) { return function (_Component) { _inherits(UploaderDecorator, _Component); function UploaderDecorator() { _classCallCheck(this, UploaderDecorator); return _possibleConstructorReturn(this, _Component.apply(this, arguments)); } UploaderDecorator.prototype.componentDidMount = function componentDidMount() { var _this2 = this; events.map(function (eventName) { if (getUploader()) getUploader().bind(eventName, _this2.handleUploaderEvent, _this2); }); }; UploaderDecorator.prototype.componentWillUnmount = function componentWillUnmount() { var _this3 = this; events.map(function (eventName) { if (getUploader()) getUploader().unbind(eventName, _this3.handleUploaderEvent); }); }; UploaderDecorator.prototype.handleUploaderEvent = function handleUploaderEvent() { this.forceUpdate(); }; UploaderDecorator.prototype.render = function render() { return React.createElement(WrappedComponent, _extends({ uploader: getUploader() }, this.props)); }; return UploaderDecorator; }(Component); }; }