"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _Logger = _interopRequireDefault(require("./Logger")); var _Types = _interopRequireDefault(require("../modules/Types")); require("./ModuleLoader.css"); var _ModuleUtilities = require("../modules/ModuleUtilities"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } 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 _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; } var LinkManager = /*#__PURE__*/ function () { function LinkManager() { _classCallCheck(this, LinkManager); this.__disableCore = false; this.clean(); } _createClass(LinkManager, [{ key: "disableCore", value: function disableCore() { this.__disableCore = true; } }, { key: "clean", value: function clean() { this.links = {}; this.reverseLinks = {}; this.inputs = {}; this.outputs = {}; } }, { key: "_resolve", value: function _resolve(output, value) { //console.log("Resolve", output, value); var pid = output.pid; if (pid === "core" && this.__disableCore) return; // find all links var links = this.links[pid][output.name]; // find all inputs for (var i = 0; i < links.length; i++) { var _links$i = links[i], _pid = _links$i.pid, name = _links$i.name; // resolve if (this.inputs[_pid][name]) this.inputs[_pid][name].update(value, '' + output.id + output.name); } } }, { key: "addOutputRef", value: function addOutputRef(output) { output = this.ghostOutputRef(output, false); this.outputs[output.pid] = _objectSpread({}, this.outputs[output.pid], _defineProperty({}, output.name, output)); this.links[output.pid] = _objectSpread(_defineProperty({}, output.name, []), this.links[output.pid]); return output; } }, { key: "addInputRef", value: function addInputRef(input) { input = this.ghostInputRef(input); this.inputs[input.pid] = _objectSpread({}, this.inputs[input.pid], _defineProperty({}, input.name, input)); this.reverseLinks[input.pid] = _objectSpread(_defineProperty({}, input.name, []), this.reverseLinks[input.pid]); return input; } }, { key: "ghostInputRef", value: function ghostInputRef(input) { var self = this; return _objectSpread({}, input, { value: (0, _ModuleUtilities.Resolver)(input), onUpdate: function onUpdate(fn) { this._onUpdate = fn; }, update: function update(v, name) { if (v === undefined) return; this.value = (0, _ModuleUtilities.Resolver)(input, v); if (this._onUpdate) this._onUpdate(this.value); } }); } }, { key: "ghostOutputRef", value: function ghostOutputRef(output) { var muted = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; var self = this; return _objectSpread({}, output, { set: function set(v) { this.value = v; if (props[output.name]) props[output.name](this.value); if (!muted) self._resolve(this, this.value); } }); } }, { key: "createLink", value: function createLink(input, output) { // check if they exists if (!this.inputs[input.pid] || !this.outputs[output.pid]) return; if ((output.pid !== "core" || !this.__disableCore) && this.inputs[input.pid][input.name] && this.outputs[output.pid][output.name]) { this.inputs[input.pid][input.name].value = this.outputs[output.pid][output.name].value; } if (!this.links[output.pid][output.name]) { this.links[output.pid][output.name] = []; } this.links[output.pid][output.name].push({ pid: input.pid, name: input.name }); if (!this.reverseLinks[input.pid][input.name]) { this.reverseLinks[input.pid][input.name] = []; } this.reverseLinks[input.pid][input.name].push({ pid: output.pid, name: output.name }); } }, { key: "addLink", value: function addLink(link) { this.createLink(link.input, link.output); } }, { key: "clearLinks", value: function clearLinks(arr) { for (var i in this.links) { if (!arr[i] && i != "core") { this.breakLinks(i); } } for (var i in this.reverseLinks) { if (!arr[i] && i != "core") { this.breakLinks(i); } } } }, { key: "breakInputLinks", value: function breakInputLinks(input) { if (!input.pid) return; var id = input.pid; var rlinks = this.reverseLinks[id]; var arr = rlinks[input.name]; for (var index = 0; index < arr.length; index++) { var _arr$index = arr[index], pid = _arr$index.pid, name = _arr$index.name; // resolve for (var z = 0; z < this.links[pid][name].length; z++) { if (this.links[pid][name][z].pid == id && this.links[pid][name][z].name == input.name) { this.links[pid][name].splice(z, 1); z = z - 1; } } } this.reverseLinks[id][input.name] = []; } }, { key: "breakOutputLinks", value: function breakOutputLinks(output) { if (!output.pid) return; var id = output.pid; var rlinks = this.links[id]; var arr = rlinks[output.name]; for (var index = 0; index < arr.length; index++) { var _arr$index2 = arr[index], pid = _arr$index2.pid, name = _arr$index2.name; // resolve for (var z = 0; z < this.reverseLinks[pid][name].length; z++) { if (this.reverseLinks[pid][name][z].pid == id && this.reverseLinks[pid][name][z].name == output.name) { this.reverseLinks[pid][name].splice(z, 1); z = z - 1; } } } this.links[id][output.name] = []; } }, { key: "breakLinks", value: function breakLinks(id) { for (var name in this.reverseLinks[id]) { var arr = this.reverseLinks[id][name]; for (var index = 0; index < arr.length; index++) { var _arr$index3 = arr[index], pid = _arr$index3.pid, _name = _arr$index3.name; // resolve for (var z = 0; z < this.links[pid][_name].length; z++) { if (this.links[pid][_name][z].pid == id) { this.links[pid][_name].splice(z, 1); z = z - 1; } } } } delete this.reverseLinks[id]; delete this.links[id]; delete this.inputs[id]; delete this.outputs[id]; } }, { key: "rename", value: function rename(oldid, newid) { for (var name in this.reverseLinks[oldid]) { var arr = this.reverseLinks[oldid][name]; for (var index = 0; index < arr.length; index++) { var _arr$index4 = arr[index], pid = _arr$index4.pid, _name2 = _arr$index4.name; // resolve for (var z = 0; z < this.links[pid][_name2].length; z++) { if (this.links[pid][_name2][z].pid == oldid) { this.links[pid][_name2][z].pid = newid; } } } } this.reverseLinks[newid] = this.reverseLinks[oldid]; // NEED TO BE HERE for (var name in this.links[oldid]) { var _arr = this.links[oldid][name]; for (var index = 0; index < _arr.length; index++) { var _arr$index5 = _arr[index], pid = _arr$index5.pid, _name3 = _arr$index5.name; // resolve for (var z = 0; z < this.reverseLinks[pid][_name3].length; z++) { if (this.reverseLinks[pid][_name3][z].pid == oldid) { this.reverseLinks[pid][_name3][z].pid = newid; } } } } this.links[newid] = this.links[oldid]; this.inputs[newid] = this.inputs[oldid]; this.outputs[newid] = this.outputs[oldid]; delete this.inputs[oldid]; delete this.outputs[oldid]; delete this.reverseLinks[oldid]; delete this.links[oldid]; } }, { key: "connect", value: function connect(input, output) { this.createLink(input, output); } }, { key: "getOutputConnections", value: function getOutputConnections(output) { return this.links[output.pid][output.name]; } }, { key: "getInputConnections", value: function getInputConnections(input) { return this.reverseLinks[input.pid][input.name]; } }, { key: "isReady", value: function isReady(mod) { for (var i = 0; i < mod.inputs.length; i++) { if (!this.inputs[mod.id][mod.inputs[i].name]) { return false; } } for (var i = 0; i < mod.outputs.length; i++) { if (!this.outputs[mod.id][mod.outputs[i].name]) { return false; } } // maybe check more here! return true; } }, { key: "selectOutput", value: function selectOutput(output) { this.__selectedOutput = output; } }, { key: "connectSelected", value: function connectSelected(input) { if (this.__selectedOutput) this.createLink(input, this.__selectedOutput); } }, { key: "export", value: function _export() { var links = []; for (var i in this.links) { for (var j in this.links[i]) { if (this.links[i][j].length) { for (var z in this.links[i][j]) { var temp = { output: { name: j, pid: i }, input: this.links[i][j][z] }; links.push(temp); } } } } return links; } }]); return LinkManager; }(); var _default = LinkManager; exports.default = _default;