UNPKG

awv3

Version:
304 lines (247 loc) 9.21 kB
import _regeneratorRuntime from "@babel/runtime/regenerator"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import Base from './base'; import { createContext, handleResult } from '../core/parser'; import Parser from '../core/parser'; var SignalR = /*#__PURE__*/ function (_Base) { _inheritsLoose(SignalR, _Base); function SignalR(options) { var _this; if (options === void 0) { options = {}; } _this = _Base.call(this, options) || this; _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 = Promise.resolve(); _this._handler = null; _this._defaultContext = createContext(); _this._defaultHandler = /*#__PURE__*/ function () { var _ref = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee(data) { return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return handleResult(_this._defaultContext, data); case 2: _this._defaultContext.promises = []; _this._defaultContext.results = []; case 4: case "end": return _context.stop(); } } }, _callee, this); })); return function (_x) { 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(_assertThisInitialized(_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(_assertThisInitialized(_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; } var _proto = SignalR.prototype; _proto.connect = function connect(url) { var _this2 = this; if (url === void 0) { url = this._hub.url; } console.log('connecting to ' + url); if (this.connected) return Promise.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 = Promise.resolve(url + '/signalr'); return first.then(function (url) { if (!url) return Promise.reject('Supply URL!'); if (_this2._timeout) clearTimeout(_this2._timeout); _this2._timeout = setTimeout(function () {// Slots occupied }, 4000); // Link URL _this2._hub.url = url; // Start hub _this2._hub.start(_this2.options).done(function () { _this2._proxy.clientHub.server.init(false, !!_this2.options.pause, !!_this2.options.timeOut ? _this2.options.timeOut : 0, !!_this2.options.rebuild ? _this2.options.rebuild : false); if (_this2._heartbeat) clearInterval(_this2._heartbeat); _this2._heartbeat = setInterval(function () { _this2._proxy.clientHub.server.ping(); }, _this2.ping); }); // Return promise return new Promise(function (resolve, reject) { _this2._currentResolve = resolve; _this2._currentReject = reject; }); }); }; _proto.disconnect = 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); } }; _proto.send = function send(message) { if (this.transport === 'webSockets' || this._queueBlock) { this._queueBlock = false; this._proxy.clientHub.server.send(message); } else { this._queue.push(message); } }; _proto.request = function request(command, factory, timeout) { var _this3 = this; if (!this.connected) return Promise.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 Promise(function (resolve, reject) { var timeout = setTimeout(reject, timeout || 120000), context = createContext(factory, resolve, reject, command); context.options.callback({ type: Parser.Factory.Started, context: context }); // Override result callback for each transaction & handle all incoming packages _this3._handler = function (data) { return handleResult(context, data); }; _this3.send(JSON.stringify({ command: 'BeginFrame', transactionID: context.id })); for (var _iterator = command, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var _ref2; if (_isArray) { if (_i >= _iterator.length) break; _ref2 = _iterator[_i++]; } else { _i = _iterator.next(); if (_i.done) break; _ref2 = _i.value; } var _item = _ref2; _this3.send(JSON.stringify(_item)); } _this3.send(JSON.stringify({ command: 'EndFrame', transactionID: context.id })); }).then(function (results) { return results; }, function (failure) { return failure; }).then(function (results) { results.options.callback({ type: Parser.Factory.Finished, context: results }); // Clean up and return context clearTimeout(_this3._timeout); _this3._handler = null; return results; }); }; // Fullfil last transaction, then queue next this._sequence = this._sequence.then(action, action); return this._sequence; }; return SignalR; }(Base); export { SignalR as default }; 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 Promise(function (resolve, reject) { var count = 0, winner; 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(); }); }); }); };