UNPKG

awv3

Version:
520 lines (467 loc) 15.7 kB
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _extends from "@babel/runtime/helpers/extends"; import without from 'lodash/without'; import jsonpatch from 'jsonpatch'; import { mixin } from '../lifecycle'; import { buildFeaturePath, normalizeName } from '../helpers'; import { createContext } from '../../core/parser'; import Parser from '../../core/parser'; import Defaults from '../../core/defaults'; import { actions as globalActions } from './globals'; var scope = 'connections'; var parser = new Parser(); export var base = mixin(scope, connection); export var types = _extends({}, base.types, { init: scope + "/init", setTree: scope + "/setTree", destroyTree: scope + "/destroyTree", patchTree: scope + "/patchTree", request: scope + "/request", message: scope + "/message", recalc: scope + "/recalc", setName: scope + "/setName", setColor: scope + "/setColor", setActiveFeature: scope + "/setActiveFeature", setActiveRoot: scope + "/setActiveRoot", setDefaultFeatureVisibility: scope + "/setDefaultFeatureVisibility", linkPlugins: scope + "/linkPlugins", unlinkPlugins: scope + "/unlinkPlugins", disableAllPlugins: scope + "/disableAllPlugins", updateTree: scope + "/updateTree" }); var actions = _extends({}, base.actions, { // Basic Redux actions setTree: function setTree(id, tree) { return { type: types.setTree, id: id, tree: tree }; }, destroyTree: function destroyTree(id) { return { type: types.destroyTree, id: id }; }, patchTree: function patchTree(id, patches) { return { type: types.patchTree, id: id, patches: patches }; }, message: function message(id, type, _message, reset) { var _ref; return _ref = { type: types.message, id: id }, _ref["type"] = type, _ref.message = _message, _ref.reset = reset, _ref; }, setActiveFeature: function setActiveFeature(id, feature, activationInfo) { if (feature === void 0) { feature = undefined; } if (activationInfo === void 0) { activationInfo = undefined; } return { type: types.setActiveFeature, id: id, feature: feature, activationInfo: activationInfo }; }, setActiveRoot: function setActiveRoot(id, root) { if (root === void 0) { root = undefined; } return { type: types.setActiveRoot, id: id, root: root }; }, setDefaultFeatureVisibility: function setDefaultFeatureVisibility(id, visible) { return { type: types.setDefaultFeatureVisibility, id: id, visible: visible }; }, linkPlugins: function linkPlugins(id, plugins) { return { type: types.linkPlugins, id: id, plugins: plugins }; }, unlinkPlugins: function unlinkPlugins(id, plugins) { return { type: types.unlinkPlugins, id: id, plugins: plugins }; }, updateTree: function updateTree(id, node, attributes) { return { type: types.updateTree, id: id, node: node, attributes: attributes }; }, // Redux thunks connect: function connect(id, url, protocol) { return ( /*#__PURE__*/ function () { var _ref2 = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee(dispatch) { var connection; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: connection = base.references[id]; _context.next = 3; return new protocol().connect(url); case 3: connection.socket = _context.sent; connection.socket.on('undo', function (args) { return dispatch(actions.update(id, { undo: args })); }); _context.next = 7; return dispatch(actions.init(id)); case 7: connection.emit('connected', connection); case 8: case "end": return _context.stop(); } } }, _callee, this); })); return function (_x) { return _ref2.apply(this, arguments); }; }() ); }, init: function init(id) { return ( /*#__PURE__*/ function () { var _ref3 = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee2(dispatch) { var connection, context; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: connection = base.references[id]; // this setting should be set inside the ClassCAD ini file //await connection.socket.execute('CADH_SetFacetingParameters(0,0.01,-0.01);') _context2.next = 3; return connection.socket.request({ command: 'GetTree' }); case 3: context = _context2.sent; dispatch({ type: types.setTree, id: id, tree: context.firstResult }); dispatch(actions.message(id, 'success', "Connected to " + connection.socket.url)); return _context2.abrupt("return", context); case 7: case "end": return _context2.stop(); } } }, _callee2, this); })); return function (_x2) { return _ref3.apply(this, arguments); }; }() ); }, request: function request(id, command, factory, name) { if (name === void 0) { name = 'send'; } return function (dispatch) { var connection = base.references[id]; var task = patchContext(dispatch, connection, factory); dispatch(globalActions.beginTask(task.id, command)); // Make request right away as to not waste time var serverCall = connection.socket.requestByName(name, command, task); // Results will be handled in queued order instead connection.sequence = connection.sequence.then(function () { return serverCall.then(function (context) { // If context contains patches, run them context.patches && dispatch(actions.patchTree(id, context.patches)); // Error will be collected, too context.errors && context.errors.forEach(function (_ref4) { var errorMessage = _ref4.errorMessage; return dispatch(actions.message(id, 'error', errorMessage)); }); // Mark task as finished and return full context dispatch(globalActions.finishTask(task.id)); return context; }); }); // Return promise return connection.sequence; }; }, stream: function stream(id, url, factory) { return ( /*#__PURE__*/ function () { var _ref5 = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee3(dispatch) { var connection, context; return _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: connection = base.references[id]; _context3.next = 3; return parser.stream(url, patchContext(dispatch, connection, factory)); case 3: context = _context3.sent; context.patches && dispatch(actions.patchTree(id, context.patches)); return _context3.abrupt("return", context); case 6: case "end": return _context3.stop(); } } }, _callee3, this); })); return function (_x3) { return _ref5.apply(this, arguments); }; }() ); }, parse: function parse(id, blob, factory) { return ( /*#__PURE__*/ function () { var _ref6 = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee4(dispatch) { var connection, context; return _regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: connection = base.references[id]; _context4.next = 3; return parser.parse(blob, patchContext(dispatch, connection, factory)); case 3: context = _context4.sent; context.patches && dispatch(actions.patchTree(id, context.patches)); return _context4.abrupt("return", context); case 6: case "end": return _context4.stop(); } } }, _callee4, this); })); return function (_x4) { return _ref6.apply(this, arguments); }; }() ); }, execute: function execute(id, command) { return function (dispatch) { return base.references[id].execute(command); }; }, exportStep: function exportStep(id, path) { return function (dispatch) { return base.references[id].execute("_C.CADApplication.ExportStep(\"" + path + "\");"); }; }, exportOf1: function exportOf1(id, path) { return function (dispatch) { return base.references[id].execute("_C.CADApplication.ExportOf1(\"" + path + "\");"); }; }, recalc: function recalc(id) { return function (dispatch) { return base.references[id].execute("_C.GlobaleFunktionen.UseOnStartRecalc(_O);"); }; }, setColor: function setColor(id, ccid, rgb) { return function (dispatch) { return base.references[id].execute("_C.CADApplication.SetColor(CADH_RealToId(" + ccid + ")," + rgb.join(',') + ");"); }; }, setName: function setName(id, ccid, name) { return function (dispatch) { var connection = base.references[id]; return connection.execute(buildFeaturePath(connection.tree, ccid) + ".OBJ_ChangeName(\"" + normalizeName(name) + "\");"); }; }, newPart: function newPart(id) { return function (dispatch) { dispatch(actions.setActiveFeature(id, undefined)); dispatch(actions.setDefaultFeatureVisibility(id, true)); return base.references[id].execute("_C.CADApplication.NewPart();"); }; }, readStream: function readStream(id, ccid, data, format) { if (format === void 0) { format = 'step'; } return function (dispatch) { var connection = base.references[id]; return connection.request({ command: 'Execute', task: buildFeaturePath(connection.tree, ccid) + ".ReadStream(\"data\", VOID, \"" + format + "\");", streamData: { data: data } }); }; }, load: function load(id, data, extension) { if (extension === void 0) { extension = 'of1'; } return function (dispatch) { dispatch(actions.setActiveFeature(id, undefined)); dispatch(actions.setDefaultFeatureVisibility(id, false)); return base.references[id].request({ command: 'Execute', task: [{ 'Common.Load': ['data', extension, Defaults.gpu ? true : false] }], streamData: { data: data } }); }; } }); export { actions }; function connection(state, _ref7) { var _extends2; var type = _ref7.type, payload = _objectWithoutProperties(_ref7, ["type"]); switch (type) { case types.setTree: return _extends({}, state, { tree: payload.tree }); case types.destroyTree: return _extends({}, state, { tree: { '1': { id: 1, name: '', class: '', parent: null }, root: 1 } }); case types.patchTree: return _extends({}, state, { tree: jsonpatch.apply_patch(state.tree, payload.patches) }); case types.message: return _extends({}, state, { messages: state.messages.concat([{ type: payload.type, message: payload.message, reset: payload.reset }]) }); case types.setActiveFeature: return _extends({}, state, { activeFeature: payload.feature, activeFeatureActivationInfo: payload.activationInfo }); case types.setActiveRoot: return _extends({}, state, { activeRoot: payload.root }); case types.setDefaultFeatureVisibility: return _extends({}, state, { defaultFeatureVisibility: payload.visible }); case types.linkPlugins: return _extends({}, state, { plugins: state.plugins.concat(Array.isArray(payload.plugins) ? payload.plugins : [payload.plugins]) }); case types.unlinkPlugins: return _extends({}, state, { plugins: without.apply(void 0, [state.plugins].concat(Array.isArray(payload.plugins) ? payload.plugins : [payload.plugins])) }); case types.updateTree: return _extends({}, state, { tree: _extends({}, state.tree, (_extends2 = {}, _extends2[payload.node] = _extends({}, state.tree[payload.node], payload.attributes), _extends2)) }); default: return state; } } function patchContext(dispatch, connection, factory) { if (factory === void 0) { factory = {}; } var _createContext = createContext(factory), options = _createContext.options; return _extends({}, options, connection.session.options, { session: connection.session, callback: function callback(args) { if (args.type === 'Model') { var id = args.model.userData.id; var previous = connection.primitives[id]; if (previous) { // TODO: Maybe it would be better to cause actions here // Primitive is already known and needs to be replaced var references = previous.references.slice(); previous.destroy(); connection.primitives[id] = args.model; args.model.references = []; references.forEach(function (clone) { // Get clones parent var parent = clone.parent; // Destroy clone clone.destroyAsync(); // Add new clone to old clones parent if (parent) { // Create new clone and reference it var newClone = args.model.clone(); parent.addAsync(newClone); // Reference new clone args.model.references.push(newClone); } }); } else { // New primitive connection.primitives[id] = args.model; args.model.references = []; if (connection.waiting[id]) { connection.waiting[id].forEach(function (callback) { return callback(args.model); }); delete connection.waiting[id]; connection.updateView(); } } } // Call original callback options.callback(args); // Call session callback connection.session.options.callback && connection.session.options.callback(args); } }); } export var reducer = base.reducer;