UNPKG

@io-maana/q-assistant-client

Version:

JavaScript package to streamline communication between an assistant and the Maana Q Assistant API.

774 lines (656 loc) 23.3 kB
"use strict"; require("core-js/modules/es.object.define-property"); require("core-js/modules/es.object.freeze"); require("core-js/modules/es.object.to-string"); require("core-js/modules/es.promise"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("regenerator-runtime/runtime"); var _postRobot = _interopRequireDefault(require("post-robot")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } var events = require('events'); var EventEmitter = new events.EventEmitter(); // The collection of event types used in the API var EventTypes = Object.freeze({ LOCKING_CHANGED: 'lockingChanged' }); // Wrapper for post-robot async client -> API call. var APICall = /*#__PURE__*/ function () { var _ref = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee(callName, arg) { var _ref2, source, origin, data; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return _postRobot.default.send(window.parent, callName, arg); case 2: _ref2 = _context.sent; source = _ref2.source; origin = _ref2.origin; data = _ref2.data; return _context.abrupt("return", data); case 7: case "end": return _context.stop(); } } }, _callee); })); return function APICall(_x, _x2) { return _ref.apply(this, arguments); }; }(); // Wrapper for post-robot listener. var createAPIListener = /*#__PURE__*/ function () { var _ref3 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee2(callName, cb) { return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _postRobot.default.on(callName, cb); case 1: case "end": return _context2.stop(); } } }, _callee2); })); return function createAPIListener(_x3, _x4) { return _ref3.apply(this, arguments); }; }(); /** * Class that exposes concrete API calls to the parent API. * These calls are made over post-message via post-robot to the parent window. */ var AssistantAPIClient = /*#__PURE__*/ function () { function AssistantAPIClient() { _classCallCheck(this, AssistantAPIClient); _defineProperty(this, "clearState", function () { EventEmitter.removeAllListeners(); }); _defineProperty(this, "getUserInfo", function () { return APICall('getUserInfo'); }); _defineProperty(this, "addSelectionChangedListener", /*#__PURE__*/ function () { var _ref4 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee3(cb) { return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: EventEmitter.addListener('selectionChanged', cb); case 1: case "end": return _context3.stop(); } } }, _callee3); })); return function (_x5) { return _ref4.apply(this, arguments); }; }()); _defineProperty(this, "removeSelectionChangedListener", /*#__PURE__*/ function () { var _ref5 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee4(cb) { return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: // If the callback is not provided, then remove all of the listeners. if (cb) { EventEmitter.removeListener('selectionChanged', cb); } else { EventEmitter.removeAllListeners('selectionChanged'); } case 1: case "end": return _context4.stop(); } } }, _callee4); })); return function (_x6) { return _ref5.apply(this, arguments); }; }()); _defineProperty(this, "getCurrentSelection", function () { return APICall('getCurrentSelection'); }); _defineProperty(this, "getServiceById", function (id) { return APICall('getServiceById', id); }); _defineProperty(this, "createService", function (input) { return APICall('createService', input); }); _defineProperty(this, "refreshServiceSchema", function (input) { return APICall('refreshServiceSchema', input); }); _defineProperty(this, "reloadServiceSchema", function (id) { return APICall('reloadServiceSchema', id); }); _defineProperty(this, "deleteService", function (id) { return APICall('deleteService', id); }); _defineProperty(this, "executeFunction", function (input) { return APICall('executeFunction', input); }); _defineProperty(this, "createFunction", function (input) { return APICall('createFunction', input); }); _defineProperty(this, "updateFunction", function (input) { return APICall('updateFunction', input); }); _defineProperty(this, "deleteFunction", function (input) { return APICall('deleteFunction', input); }); _defineProperty(this, "getFunctionById", function (id) { return APICall('getFunctionById', id); }); _defineProperty(this, "getFunctionsById", function (ids) { return APICall('getFunctionsById', ids); }); _defineProperty(this, "addFunctionExecutionListener", /*#__PURE__*/ function () { var _ref6 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee5(id, cb) { return regeneratorRuntime.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: EventEmitter.addListener("function:".concat(id), cb); case 1: case "end": return _context5.stop(); } } }, _callee5); })); return function (_x7, _x8) { return _ref6.apply(this, arguments); }; }()); _defineProperty(this, "removeFunctionExecutionListener", /*#__PURE__*/ function () { var _ref7 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee6(id, cb) { return regeneratorRuntime.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: // If the callback is not provided, then remove all of the listeners. if (cb) { EventEmitter.removeListener("function:".concat(id), cb); } else { EventEmitter.removeAllListeners("function:".concat(id)); } case 1: case "end": return _context6.stop(); } } }, _callee6); })); return function (_x9, _x10) { return _ref7.apply(this, arguments); }; }()); _defineProperty(this, "createKind", function (input) { return APICall('createKind', input); }); _defineProperty(this, "updateKind", function (input) { return APICall('updateKind', input); }); _defineProperty(this, "deleteKind", function (input) { return APICall('deleteKind', input); }); _defineProperty(this, "getKindById", function (id) { return APICall('getKindById', id); }); _defineProperty(this, "getKindsById", function (ids) { return APICall('getKindsById', ids); }); _defineProperty(this, "getAllReferencedKinds", function (input) { return APICall('getAllReferencedKinds', input); }); _defineProperty(this, "addInventoryChangedListener", /*#__PURE__*/ function () { var _ref8 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee7(cb) { return regeneratorRuntime.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: EventEmitter.addListener('inventoryChanged', cb); case 1: case "end": return _context7.stop(); } } }, _callee7); })); return function (_x11) { return _ref8.apply(this, arguments); }; }()); _defineProperty(this, "removeInventoryChangedListener", /*#__PURE__*/ function () { var _ref9 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee8(cb) { return regeneratorRuntime.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: // If the callback is not provided, then remove all of the listeners. if (cb) { EventEmitter.removeListener('inventoryChanged', cb); } else { EventEmitter.removeAllListeners('inventoryChanged'); } case 1: case "end": return _context8.stop(); } } }, _callee8); })); return function (_x12) { return _ref9.apply(this, arguments); }; }()); _defineProperty(this, "getFunctionGraph", function (id) { return APICall('getFunctionGraph', id); }); _defineProperty(this, "addRenderModeChangedListener", /*#__PURE__*/ function () { var _ref10 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee9(cb) { return regeneratorRuntime.wrap(function _callee9$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: EventEmitter.addListener('renderModeChanged', cb); case 1: case "end": return _context9.stop(); } } }, _callee9); })); return function (_x13) { return _ref10.apply(this, arguments); }; }()); _defineProperty(this, "removeRenderModeChangedListener", /*#__PURE__*/ function () { var _ref11 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee10(cb) { return regeneratorRuntime.wrap(function _callee10$(_context10) { while (1) { switch (_context10.prev = _context10.next) { case 0: // If the callback is not provided, then remove all of the listeners. if (cb) { EventEmitter.removeListener('renderModeChanged', cb); } else { EventEmitter.removeAllListeners('renderModeChanged'); } case 1: case "end": return _context10.stop(); } } }, _callee10); })); return function (_x14) { return _ref11.apply(this, arguments); }; }()); _defineProperty(this, "getRenderMode", function () { return APICall('getRenderMode'); }); _defineProperty(this, "addRepairListener", /*#__PURE__*/ function () { var _ref12 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee11(cb) { return regeneratorRuntime.wrap(function _callee11$(_context11) { while (1) { switch (_context11.prev = _context11.next) { case 0: EventEmitter.addListener('repair', cb); case 1: case "end": return _context11.stop(); } } }, _callee11); })); return function (_x15) { return _ref12.apply(this, arguments); }; }()); _defineProperty(this, "removeRepairListener", /*#__PURE__*/ function () { var _ref13 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee12(cb) { return regeneratorRuntime.wrap(function _callee12$(_context12) { while (1) { switch (_context12.prev = _context12.next) { case 0: // If the callback is not provided, then remove all of the listeners. if (cb) { EventEmitter.removeListener('repair', cb); } else { EventEmitter.removeAllListeners('repair'); } case 1: case "end": return _context12.stop(); } } }, _callee12); })); return function (_x16) { return _ref13.apply(this, arguments); }; }()); _defineProperty(this, "reportError", function (error) { return APICall('reportError', error); }); _defineProperty(this, "getEventEmitter", function () { return EventEmitter; }); _defineProperty(this, "executeGraphql", function (input) { return APICall('executeGraphql', input); }); // Attach selection event emmiter to API listener createAPIListener('selectionChanged', /*#__PURE__*/ function () { var _ref14 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee13(event) { return regeneratorRuntime.wrap(function _callee13$(_context13) { while (1) { switch (_context13.prev = _context13.next) { case 0: EventEmitter.emit('selectionChanged', event.data); case 1: case "end": return _context13.stop(); } } }, _callee13); })); return function (_x17) { return _ref14.apply(this, arguments); }; }()); // Attach function execution event emmiter to API listener. // Use convention to filter by function ID. createAPIListener('functionExecuted', /*#__PURE__*/ function () { var _ref15 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee14(event) { return regeneratorRuntime.wrap(function _callee14$(_context14) { while (1) { switch (_context14.prev = _context14.next) { case 0: EventEmitter.emit("function:".concat(event.data.id), event.data.result); case 1: case "end": return _context14.stop(); } } }, _callee14); })); return function (_x18) { return _ref15.apply(this, arguments); }; }()); // Attach inventory event emitter to API listener. createAPIListener('inventoryChanged', /*#__PURE__*/ function () { var _ref16 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee15(event) { return regeneratorRuntime.wrap(function _callee15$(_context15) { while (1) { switch (_context15.prev = _context15.next) { case 0: EventEmitter.emit('inventoryChanged', event.data); case 1: case "end": return _context15.stop(); } } }, _callee15); })); return function (_x19) { return _ref16.apply(this, arguments); }; }()); // Attach render mode event emitter to API listener. createAPIListener('renderModeChanged', /*#__PURE__*/ function () { var _ref17 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee16(event) { return regeneratorRuntime.wrap(function _callee16$(_context16) { while (1) { switch (_context16.prev = _context16.next) { case 0: EventEmitter.emit('renderModeChanged', event.data); case 1: case "end": return _context16.stop(); } } }, _callee16); })); return function (_x20) { return _ref17.apply(this, arguments); }; }()); // Attach repair listener. createAPIListener('repair', /*#__PURE__*/ function () { var _ref18 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee17(event) { return regeneratorRuntime.wrap(function _callee17$(_context17) { while (1) { switch (_context17.prev = _context17.next) { case 0: EventEmitter.emit('repair', event.data); case 1: case "end": return _context17.stop(); } } }, _callee17); })); return function (_x21) { return _ref18.apply(this, arguments); }; }()); // Attach locking changed listener. createAPIListener(EventTypes.LOCKING_CHANGED, /*#__PURE__*/ function () { var _ref19 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee18(event) { return regeneratorRuntime.wrap(function _callee18$(_context18) { while (1) { switch (_context18.prev = _context18.next) { case 0: EventEmitter.emit(EventTypes.LOCKING_CHANGED, event.data); case 1: case "end": return _context18.stop(); } } }, _callee18); })); return function (_x22) { return _ref19.apply(this, arguments); }; }()); } // // Assistant State // /** * Updates the current state of the Assistant. * * @param {AssistantState} state The new state of the assistant. */ _createClass(AssistantAPIClient, [{ key: "setAssistantState", value: function setAssistantState(state) { return APICall('setAssistantState', state); } // // State management // }, { key: "getWorkspace", // // Workspace // /** * Returns the requested Workspace, if no Workspace ID is specified it returns * the Workspace that the user is currently using. * * @param {string} id The ID of the Workspace to load. (optional) * @return {Workspace} The requested Workspace. */ value: function getWorkspace(id) { return APICall('getWorkspace', id); } /** * Returns a list of user accessible Workspaces. By default it will just be * the user owned Workspaces, but can be configured to also return all the * public workspaces. * * @param {boolean} includePublic When true the returned list includes public Workspaces. * @return {Array<Workspace>} The list of Workspaces. */ }, { key: "getUserAccessibleWorkspaces", value: function getUserAccessibleWorkspaces() { var includePublic = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; return APICall('getUserAccessibleWorkspaces', includePublic); } /** * Creates a new Workspace. The id, name, and serviceId can optionally be * set, or they can be left undefined to use the defaults. * * @param {Object} workspace The Workspace information, can container {id, name, serviceId} * @return {Workspace} The new Workspace. */ }, { key: "createWorkspace", value: function createWorkspace(workspace) { return APICall('createWorkspace', workspace); } // // Functions // }, { key: "moveKindsAndFunctions", /** * Moves a collection of Kinds and Functions from the origin Workspace to the * target Workspace. * * @param {string} originId The ID of the origin Workspace. * @param {string} targetId The ID of the target Workspace. * @param {Array<string>} kindIds An array of the IDs of the kinds to move. * @param {Array<string>} functionIds An array of the IDs of the functions to move. */ value: function moveKindsAndFunctions(originId, targetId, kindIds, functionIds) { return APICall('moveKindsAndFunctions', { originId: originId, targetId: targetId, kindIds: kindIds, functionIds: functionIds }); } // // Graphs // }, { key: "addLockingChangedListener", // // Locking // /** * Adds a callback function to be called every time the locking changed event * is triggered. * * @param {Function} cb The callback function to call */ value: function addLockingChangedListener(cb) { EventEmitter.addListener(EventTypes.LOCKING_CHANGED, cb); } /** * Removes a callback function from the list be called every time the locking * changed event is triggered. If no callback is passed in, then all * listeners are removed for the locking changed event. * * * @param {Function|undefined} cb The callback function to remove */ }, { key: "removeLockingChangedListener", value: function removeLockingChangedListener(cb) { // If the callback is not provided, then remove all of the listeners. if (cb) { EventEmitter.removeListener(EventTypes.LOCKING_CHANGED, cb); } else { EventEmitter.removeAllListeners(EventTypes.LOCKING_CHANGED); } } // // Undocumented // }]); return AssistantAPIClient; }(); // Export as singleton. var _default = new AssistantAPIClient(); exports.default = _default;