UNPKG

awv3

Version:
356 lines (281 loc) 13.2 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _getIterator2 = require('babel-runtime/core-js/get-iterator'); var _getIterator3 = _interopRequireDefault(_getIterator2); var _stringify = require('babel-runtime/core-js/json/stringify'); var _stringify2 = _interopRequireDefault(_stringify); var _regenerator = require('babel-runtime/regenerator'); var _regenerator2 = _interopRequireDefault(_regenerator); var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); var _promise = require('babel-runtime/core-js/promise'); var _promise2 = _interopRequireDefault(_promise); var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _base = require('./base'); var _base2 = _interopRequireDefault(_base); var _parser = require('../core/parser'); var _parser2 = _interopRequireDefault(_parser); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var SignalR = function (_Base) { (0, _inherits3.default)(SignalR, _Base); function SignalR() { var _this2 = this; var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; (0, _classCallCheck3.default)(this, SignalR); var _this = (0, _possibleConstructorReturn3.default)(this, (SignalR.__proto__ || (0, _getPrototypeOf2.default)(SignalR)).call(this, options)); _this.ping = options.ping || 10000; _this.onPause = undefined; _this.onResume = undefined; _this._connection; _this._heartbeat; _this._timeout; _this._queueBlock; _this._currentResolve; _this._currentReject; _this._queue = []; _this._sequence = _promise2.default.resolve(); _this._handler = null; _this._defaultContext = (0, _parser.createContext)(); _this._defaultHandler = function () { var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(data) { return _regenerator2.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return (0, _parser.handleResult)(_this._defaultContext, data); case 2: _this._defaultContext.promises = []; _this._defaultContext.results = []; case 4: case 'end': return _context.stop(); } } }, _callee, _this2); })); return function (_x2) { return _ref.apply(this, arguments); }; }(); _this.connected = false; _this.paused = false; _this.transport = ''; _this.serverState = undefined; // Initialize hub var hub = require('../communication/signalrhub').default; hub(_this); _this._proxy.clientHub.client.result = function (data) { // Parse data into JSON var obj = JSON.parse(data); if (!_this.connected) { if (obj.command == "PERMISSION") { _this.connected = true; _this.transport = obj.transport; _this._currentResolve(_this); } else if (obj.command == "WAIT") { _this.transport = obj.transport; } } else { if (obj.command === "Service") { // Connection to a ClassCAD instance has been paused if (obj.event === "Pause") { _this.paused = true; _this.serverState = obj.state; if (!!_this.onPause) _this.onPause(obj); // Connection to ClassCAD is reestablished } else if (obj.event === "Resume") { _this.paused = false; if (!!_this.onResume) _this.onResume(obj); } } else (_this._handler || _this._defaultHandler)(obj); } }; _this._proxy.clientHub.client.disconnect = function () { if (_this._heartbeat) clearInterval(_this._heartbeat); _this._hub.stop(); _this.connected = false; }; _this._proxy.clientHub.client.debug = function (message) { console.log(message); }; _this._proxy.clientHub.client.queueNext = function () { var message = _this._queue.shift(); if (message !== undefined) _this._proxy.clientHub.server.send(message);else _this._queueBlock = true; }; _this._hub.disconnected(function () { if (_this._heartbeat) clearInterval(_this._heartbeat); if (_this._timeout) clearTimeout(_this._timeout); _this.connected = false; _this._currentReject(_this._hub.url + ' not found'); }); return _this; } (0, _createClass3.default)(SignalR, [{ key: 'connect', value: function connect() { var _this3 = this; var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this._hub.url; console.log("connecting to " + url); if (this.connected) return _promise2.default.reject("Disconnect first!"); var first; /*if (!!this.options.loadBallanced) { first = this.canvas.parser.stream(url + '/ip').then(context => { if (context.results.length > 0) { var scaledUri = context.results[0].result.url + '/signalr'; return scaledUri; } }).catch(reason => Promise.reject("Supply URL!")); } else*/ first = _promise2.default.resolve(url + '/signalr'); return first.then(function (url) { if (!url) return _promise2.default.reject("Supply URL!"); if (_this3._timeout) clearTimeout(_this3._timeout); _this3._timeout = setTimeout(function () { // Slots occupied }, 4000); // Link URL _this3._hub.url = url; // Start hub _this3._hub.start(_this3.options).done(function () { _this3._proxy.clientHub.server.init(false, !!_this3.options.pause, !!_this3.options.timeOut ? _this3.options.timeOut : 0, !!_this3.options.rebuild ? _this3.options.rebuild : false); if (_this3._heartbeat) clearInterval(_this3._heartbeat); _this3._heartbeat = setInterval(function () { _this3._proxy.clientHub.server.ping(); }, _this3.ping); }); // Return promise return new _promise2.default(function (resolve, reject) { _this3._currentResolve = resolve; _this3._currentReject = reject; }); }); } }, { key: 'disconnect', value: function disconnect() { if (!this.connected) return; if (this._proxy.clientHub !== undefined) { this._proxy.clientHub.client.disconnect(); this._currentResolve = null; this._currentReject = null; this.connected = false; if (this._timeout) clearTimeout(this._timeout); } } }, { key: 'send', value: function send(message) { if (this.transport === "webSockets" || this._queueBlock) { this._queueBlock = false; this._proxy.clientHub.server.send(message); } else { this._queue.push(message); } } }, { key: 'request', value: function request(command, factory, timeout) { var _this4 = this; if (!this.connected) return _promise2.default.reject("Not connected!"); // If we're pause, we need to load our previous state if (this.paused && !!this.serverState) { var state = this.serverState; this.serverState = undefined; this.setState(state, !!this.options.rebuild, !!this.options.rebuild); } command = Array.isArray(command) ? command : [command]; var action = function action() { return new _promise2.default(function (resolve, reject) { var timeout = setTimeout(reject, timeout || 120000), context = (0, _parser.createContext)(factory, resolve, reject, command); context.options.callback({ type: _parser2.default.Factory.Started, context: context }); // Override result callback for each transaction & handle all incoming packages _this4._handler = function (data) { return (0, _parser.handleResult)(context, data); }; _this4.send((0, _stringify2.default)({ command: 'BeginFrame', transactionID: context.id })); var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = (0, _getIterator3.default)(command), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var item = _step.value; _this4.send((0, _stringify2.default)(item)); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } _this4.send((0, _stringify2.default)({ command: 'EndFrame', transactionID: context.id })); }).then(function (results) { return results; }, function (failure) { return failure; }).then(function (results) { results.options.callback({ type: _parser2.default.Factory.Finished, context: results }); // Clean up and return context clearTimeout(_this4._timeout); _this4._handler = null; return results; }); }; // Fullfil last transaction, then queue next this._sequence = this._sequence.then(action, action); return this._sequence; } }]); return SignalR; }(_base2.default); exports.default = SignalR; SignalR.eliminate = function (urls, options) { if (urls.length === 1) return new SignalR(options).connect(urls.shift()); var first = urls.shift(); return urls.reduce(function (sequence, url) { return sequence.then(function (server) { return server; }, function () { return new SignalR(options).connect(url); }); }, new SignalR(options).connect(first)); }; SignalR.race = function (urls, options) { return new _promise2.default(function (resolve, reject) { var count = 0, winner = void 0; var connections = urls.map(function (url) { return new SignalR(options).connect(url).then(function (item) { return !winner ? resolve(winner = item) : item.disconnect(); }).catch(function () { if (++count === urls.length) reject(); }); }); }); };