UNPKG

@choerodon/master

Version:
158 lines (128 loc) 6.16 kB
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } /* eslint-disable react/static-property-placement */ import { Component } from 'react'; import PropTypes from 'prop-types'; import { getCookie } from "../../utils"; import AppState from "../../containers/stores/c7n/AppState"; var WSHandler = /*#__PURE__*/function (_Component) { _inherits(WSHandler, _Component); var _super = _createSuper(WSHandler); function WSHandler() { var _this; _classCallCheck(this, WSHandler); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _this.state = { data: null }; _this.handleMessage = function (data) { var _this$props = _this.props, onMessage = _this$props.onMessage, type = _this$props.type, dataKey = _this$props.dataKey, typeKey = _this$props.typeKey; if (typeof onMessage === 'function') { onMessage(JSON.parse(data)[dataKey]); } if (type) { var jsonData = JSON.parse(data); if (jsonData[typeKey] === type) { _this.setState({ data: jsonData[dataKey] }); } } else { _this.setState({ data: JSON.parse(data)[dataKey] }); } }; return _this; } _createClass(WSHandler, [{ key: "componentWillMount", value: function componentWillMount() { this.register(this.props, this.context); } }, { key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps, nextContext) { if (nextProps.messageKey !== this.props.messageKey || this.getPath(nextProps.path) !== this.getPath(this.props.path)) { this.unregister(this.props, this.context); this.register(nextProps, nextContext); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.unregister(this.props, this.context); } }, { key: "getPath", value: function getPath(path) { if (typeof path === 'string') { return path; } if (typeof path === 'function') { return path(); } return undefined; } }, { key: "register", value: function register(props, context) { var messageKey = props.messageKey, path = props.path; var ws = context.ws; if (ws) { ws.register(messageKey, { type: 'notify', key: messageKey }, this.handleMessage, this.getPath(path)); } } }, { key: "unregister", value: function unregister(props, context) { var messageKey = props.messageKey, path = props.path; var ws = context.ws; if (ws) { ws.unregister(messageKey, this.handleMessage, this.getPath(path)); } } }, { key: "render", value: function render() { var data = this.state.data; var children = this.props.children; if (typeof children === 'function') { return children(data); } return children; } }]); return WSHandler; }(Component); WSHandler.defaultProps = { path: function path() { return "websocket?group=choerodon:msg:site-msg:".concat(AppState.userInfo.id, "&processor=choerodon_msg&access_token=").concat(getCookie('access_token')); }, dataKey: 'message', typeKey: 'key' }; WSHandler.contextTypes = { ws: PropTypes.object }; export { WSHandler as default };