awv3
Version:
⚡ AWV3 embedded CAD
152 lines (114 loc) • 3.85 kB
JavaScript
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import io from 'socket.io-client';
import Base from './base';
import Parser from '../core/parser';
import { createContext, handleResult } from '../core/parser';
var SocketIO =
/*#__PURE__*/
function (_Base) {
_inheritsLoose(SocketIO, _Base);
function SocketIO(options) {
return _Base.call(this, options) || this;
}
var _proto = SocketIO.prototype;
_proto.connect = function connect(url, options) {
var _this = this;
if (url === void 0) {
url = this.url;
}
return new Promise(function (resolve, reject) {
_this.url = url;
_this.socket = io(url
/*, { transports: ['polling'] }*/
);
_this.socket.once('connect', function () {
_this.socket.once('permission', function (data) {
_this.connected = true;
_this.id = data.id;
_this.version = data.version;
_this.emit('connected', _this, data);
resolve(_this);
});
_this.socket.emit('init', options || _this.options);
});
_this.socket.on('connect_error', function (reason) {
_this.emit('error', reason);
reject(reason);
});
_this.socket.on('connect_timeout', function (reason) {
_this.emit('error', reason);
reject(reason);
});
var current = undefined;
_this.socket.on('binary', function (data, fn) {
if (!_this.version) SocketIO._ack(_this.socket); // Backward compatible ack
if (current) handleResult(current, new Uint8Array(data));
if (fn) fn('ack'); // Newer awv3-node 8.x.x ack
});
_this.socket.on('json', function (data, fn) {
if (!_this.version) SocketIO._ack(_this.socket); // Backward compatible ack
if (data.from === 'BeginFrame') current = _this.tasks.get(data.transactionID);
if (current) handleResult(current, data);
if (current && data.from === 'EndFrame' && data.transactionID === current.id) {
_this.tasks.delete(current.id);
current = undefined;
}
if (fn) fn('ack'); // Newer awv3-node 8.x.x ack
});
_this.socket.on('undo', function (args) {
return _this.emit('undo', args);
});
});
};
_proto.disconnect = function disconnect() {
this.socket.disconnect();
this.socket = undefined;
this.tasks = new Map();
this.emit('disconnected', this);
};
_proto.requestByName = function requestByName(name, command, factory) {
var _this2 = this;
if (factory === void 0) {
factory = {};
}
return new Promise(function (resolve, reject) {
var context = createContext(factory, resolve, reject, command);
context.options.callback({
type: Parser.Factory.Started,
context: context
});
_this2.tasks.set(context.id, context);
_this2.socket.emit(name, context.id, command, factory.classcad);
}).then(function (results) {
results.options.callback({
type: Parser.Factory.Finished,
context: results
});
return results;
});
};
_proto.request = function request(command, factory) {
if (factory === void 0) {
factory = {};
}
return this.requestByName('send', command, factory);
};
_proto.undo = function undo(factory) {
if (factory === void 0) {
factory = {};
}
return this.requestByName('undo', undefined, factory);
};
_proto.redo = function redo(factory) {
if (factory === void 0) {
factory = {};
}
return this.requestByName('redo', undefined, factory);
};
SocketIO._ack = function _ack(socket) {
//if (socket.io.engine.transport.name !== 'websocket');
socket && socket.emit('received');
};
return SocketIO;
}(Base);
export { SocketIO as default };