awv3
Version:
⚡ AWV3 embedded CAD
108 lines (94 loc) • 2.98 kB
JavaScript
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import Parser from '../core/parser';
import Base from './base';
import EventEmitter from 'events';
import { createContext, mergeContext, handleResult } from '../core/parser';
var Rest =
/*#__PURE__*/
function (_Base) {
_inheritsLoose(Rest, _Base);
function Rest(options) {
return _Base.call(this, options) || this;
}
var _proto = Rest.prototype;
_proto.connect = function connect(url) {
var _this = this;
this.url = this.url || url;
return fetch(this.url + "/login").then(function (res) {
return res.json();
}).then(function (res) {
if (res.status === 'permitted') {
_this.alive = true;
_this.id = res.id;
return _this;
}
});
};
_proto.disconnect = function disconnect() {
this.alive = false;
this.emit('disconnected');
return fetch(this.url + "/logout/" + this.id);
};
_proto.requestByName = function requestByName(name, command, factory) {
return this.request(command, factory);
};
_proto.request = function request(command, factory) {
var _this2 = this;
return new Promise(function (resolve, reject) {
var context = createContext(factory, resolve, reject, command);
context.options.callback({
type: Parser.Factory.Started,
context: context
});
return fetch(_this2.url + "/execute/" + _this2.id, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify(command)
}).then(function (res) {
return res.json();
}).then(function (res) {
return handleResult(context, res);
}).then(
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee(res) {
var results;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return Promise.all(context.promises);
case 2:
results = _context.sent;
context = mergeContext(context);
resolve(context);
case 5:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
return function (_x) {
return _ref.apply(this, arguments);
};
}());
}).then(function (results) {
results.options.callback({
type: Parser.Factory.Finished,
context: results
});
return results;
});
};
return Rest;
}(Base);
export { Rest as default };