@metacell/geppetto-meta-client
Version:
Geppetto web frontend. Geppetto is an open-source platform to build web-based tools to visualize and simulate neuroscience data and models.
190 lines (179 loc) • 7.37 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
* Client class use to handle Geppetto workflows
*
* @module Manager
* @author Matteo Cantarelli
*/
import EventManager from './EventManager';
import MessageSocket from '../communication/MessageSocket';
import ModelFactory from '@metacell/geppetto-meta-core/ModelFactory';
import Resources from '@metacell/geppetto-meta-core/Resources';
import Instances from '@metacell/geppetto-meta-core/Instances';
import ModelManager from '@metacell/geppetto-meta-core/ModelManager';
export var Manager = /*#__PURE__*/function () {
function Manager() {
_classCallCheck(this, Manager);
/**
* Augments the instances array with some utilities methods for ease of access
*/
_defineProperty(this, "augmentInstancesArray", Instances.augmentInstancesArray);
}
return _createClass(Manager, [{
key: "loadProject",
value:
/**
*
* @param payload
*/
function loadProject(project, persisted) {
// we remove anything from any previous loaded project if there was one
window.Project = project;
window.Project.readOnly = !persisted;
}
/**
*
* @param payload
*/
}, {
key: "loadModel",
value: function loadModel(model) {
ModelManager.loadModel(model);
EventManager.modelLoaded();
// populate control panel with instances
EventManager.instancesCreated(window.Instances);
console.timeEnd(Resources.LOADING_PROJECT);
return window.Model;
}
/**
* Fetch variable
*
* @param variableId
* @param datasourceId
*/
}, {
key: "fetchVariables",
value: function fetchVariables(variableIds, datasourceId, callback) {
if (!Object.prototype.hasOwnProperty.call(window.Model, variableIds)) {
var params = {};
params["projectId"] = Project.getId();
params["variableId"] = variableIds;
params["dataSourceId"] = datasourceId;
var requestID = MessageSocket.send("fetch_variable", params, callback);
EventManager.actionsHandler[EventManager.clientActions.SPIN_LOGO]();
} else {
console.log(Resources.VARIABLE_ALREADY_EXISTS);
// the variable already exists, run the callback
callback();
}
}
/**
* Fetch variables and instances
*
* @param variables
* @param instances
* @param worldId
* @param datasourceId
*/
}, {
key: "fetch",
value: function fetch(variableIds, instanceIds, worldId, datasourceId, callback) {
var params = {};
params["projectId"] = Project.getId();
params["variables"] = variableIds;
params["instances"] = instanceIds;
params["worldId"] = worldId;
params["dataSourceId"] = datasourceId;
var requestID = MessageSocket.send("fetch", params, callback);
EventManager.actionsHandler[EventManager.clientActions.SPIN_LOGO]();
}
/**
* Adds fetched variable to model
*
* @param rawModel
*/
}, {
key: "addVariableToModel",
value: function addVariableToModel(rawModel) {
console.time(Resources.ADDING_VARIABLE);
var newInstances = ModelManager.addVariableToModel(rawModel);
EventManager.actionsHandler[EventManager.clientActions.INSTANCES_CREATED](newInstances);
console.timeEnd(Resources.ADDING_VARIABLE);
console.log(Resources.VARIABLE_ADDED);
}
/**
* Resolve import type
*
* @param typePath
*/
}, {
key: "resolveImportType",
value: function resolveImportType(typePaths, callback) {
if (typeof typePaths == "string") {
typePaths = [typePaths];
}
var params = {};
params["projectId"] = Project.getId();
// replace client naming first occurrence - the server doesn't know about it
var paths = [];
for (var i = 0; i < typePaths.length; i++) {
paths.push(typePaths[i].replace(Resources.MODEL_PREFIX_CLIENT + ".", ''));
}
params["paths"] = paths;
var requestID = MessageSocket.send("resolve_import_type", params, callback);
EventManager.actionsHandler[EventManager.clientActions.SPIN_LOGO]();
}
/**
* Swap resolved import type with actual type
*
* @param rawModel
*/
}, {
key: "swapResolvedType",
value: function swapResolvedType(rawModel) {
console.time(Resources.IMPORT_TYPE_RESOLVED);
// STEP 1: merge model - expect a fully formed Geppetto model to be merged into current one
var diffReport = ModelFactory.mergeModel(rawModel, true);
// STEP 2: add new instances for new types if any
var newInstances = ModelFactory.createInstancesFromDiffReport(diffReport);
// STEP: 3 update components
EventManager.actionsHandler[EventManager.clientActions.INSTANCES_CREATED](newInstances);
console.timeEnd(Resources.IMPORT_TYPE_RESOLVED);
console.log(Resources.IMPORT_TYPE_RESOLVED);
}
/**
*
* @param typePath
* @param callback
*/
}, {
key: "resolveImportValue",
value: function resolveImportValue(typePath, callback) {
var params = {};
params["projectId"] = Project.getId();
// replace client naming first occurrence - the server doesn't know about it
params["path"] = typePath.replace(Resources.MODEL_PREFIX_CLIENT + ".", '');
var requestID = MessageSocket.send("resolve_import_value", params, callback);
EventManager.actionsHandler[EventManager.clientActions.SPIN_LOGO]();
}
/**
* Swap resolved import value with actual type
*
* @param rawModel
*/
}, {
key: "swapResolvedValue",
value: function swapResolvedValue(rawModel) {
// STEP 1: merge model - expect a fully formed Geppetto model to be merged into current one
var diffReport = ModelFactory.mergeValue(rawModel, true);
console.log(Resources.IMPORT_VALUE_RESOLVED);
}
}]);
}();
export default new Manager();