LinkManager.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _react = _interopRequireDefault(require("react"));
  7. var _Logger = _interopRequireDefault(require("./Logger"));
  8. var _Types = _interopRequireDefault(require("../modules/Types"));
  9. require("./ModuleLoader.css");
  10. var _ModuleUtilities = require("../modules/ModuleUtilities");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. 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; }
  13. 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; }
  14. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  15. 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); } }
  16. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  17. var LinkManager =
  18. /*#__PURE__*/
  19. function () {
  20. function LinkManager() {
  21. _classCallCheck(this, LinkManager);
  22. this.__disableCore = false;
  23. this.clean();
  24. }
  25. _createClass(LinkManager, [{
  26. key: "disableCore",
  27. value: function disableCore() {
  28. this.__disableCore = true;
  29. }
  30. }, {
  31. key: "clean",
  32. value: function clean() {
  33. this.links = {};
  34. this.reverseLinks = {};
  35. this.inputs = {};
  36. this.outputs = {};
  37. }
  38. }, {
  39. key: "_resolve",
  40. value: function _resolve(output, value) {
  41. //console.log("Resolve", output, value);
  42. var pid = output.pid;
  43. if (pid === "core" && this.__disableCore) return; // find all links
  44. var links = this.links[pid][output.name]; // find all inputs
  45. for (var i = 0; i < links.length; i++) {
  46. var _links$i = links[i],
  47. _pid = _links$i.pid,
  48. name = _links$i.name; // resolve
  49. if (this.inputs[_pid][name]) this.inputs[_pid][name].update(value, '' + output.id + output.name);
  50. }
  51. }
  52. }, {
  53. key: "addOutputRef",
  54. value: function addOutputRef(output) {
  55. output = this.ghostOutputRef(output, false);
  56. this.outputs[output.pid] = _objectSpread({}, this.outputs[output.pid], _defineProperty({}, output.name, output));
  57. this.links[output.pid] = _objectSpread(_defineProperty({}, output.name, []), this.links[output.pid]);
  58. return output;
  59. }
  60. }, {
  61. key: "addInputRef",
  62. value: function addInputRef(input) {
  63. input = this.ghostInputRef(input);
  64. this.inputs[input.pid] = _objectSpread({}, this.inputs[input.pid], _defineProperty({}, input.name, input));
  65. this.reverseLinks[input.pid] = _objectSpread(_defineProperty({}, input.name, []), this.reverseLinks[input.pid]);
  66. return input;
  67. }
  68. }, {
  69. key: "ghostInputRef",
  70. value: function ghostInputRef(input) {
  71. var self = this;
  72. return _objectSpread({}, input, {
  73. value: (0, _ModuleUtilities.Resolver)(input),
  74. onUpdate: function onUpdate(fn) {
  75. this._onUpdate = fn;
  76. },
  77. update: function update(v, name) {
  78. if (v === undefined) return;
  79. this.value = (0, _ModuleUtilities.Resolver)(input, v);
  80. if (this._onUpdate) this._onUpdate(this.value);
  81. }
  82. });
  83. }
  84. }, {
  85. key: "ghostOutputRef",
  86. value: function ghostOutputRef(output) {
  87. var muted = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  88. var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  89. var self = this;
  90. return _objectSpread({}, output, {
  91. set: function set(v) {
  92. this.value = v;
  93. if (props[output.name]) props[output.name](this.value);
  94. if (!muted) self._resolve(this, this.value);
  95. }
  96. });
  97. }
  98. }, {
  99. key: "createLink",
  100. value: function createLink(input, output) {
  101. // check if they exists
  102. if (!this.inputs[input.pid] || !this.outputs[output.pid]) return;
  103. if ((output.pid !== "core" || !this.__disableCore) && this.inputs[input.pid][input.name] && this.outputs[output.pid][output.name]) {
  104. this.inputs[input.pid][input.name].value = this.outputs[output.pid][output.name].value;
  105. }
  106. if (!this.links[output.pid][output.name]) {
  107. this.links[output.pid][output.name] = [];
  108. }
  109. this.links[output.pid][output.name].push({
  110. pid: input.pid,
  111. name: input.name
  112. });
  113. if (!this.reverseLinks[input.pid][input.name]) {
  114. this.reverseLinks[input.pid][input.name] = [];
  115. }
  116. this.reverseLinks[input.pid][input.name].push({
  117. pid: output.pid,
  118. name: output.name
  119. });
  120. }
  121. }, {
  122. key: "addLink",
  123. value: function addLink(link) {
  124. this.createLink(link.input, link.output);
  125. }
  126. }, {
  127. key: "clearLinks",
  128. value: function clearLinks(arr) {
  129. for (var i in this.links) {
  130. if (!arr[i] && i != "core") {
  131. this.breakLinks(i);
  132. }
  133. }
  134. for (var i in this.reverseLinks) {
  135. if (!arr[i] && i != "core") {
  136. this.breakLinks(i);
  137. }
  138. }
  139. }
  140. }, {
  141. key: "breakInputLinks",
  142. value: function breakInputLinks(input) {
  143. if (!input.pid) return;
  144. var id = input.pid;
  145. var rlinks = this.reverseLinks[id];
  146. var arr = rlinks[input.name];
  147. for (var index = 0; index < arr.length; index++) {
  148. var _arr$index = arr[index],
  149. pid = _arr$index.pid,
  150. name = _arr$index.name; // resolve
  151. for (var z = 0; z < this.links[pid][name].length; z++) {
  152. if (this.links[pid][name][z].pid == id && this.links[pid][name][z].name == input.name) {
  153. this.links[pid][name].splice(z, 1);
  154. z = z - 1;
  155. }
  156. }
  157. }
  158. this.reverseLinks[id][input.name] = [];
  159. }
  160. }, {
  161. key: "breakOutputLinks",
  162. value: function breakOutputLinks(output) {
  163. if (!output.pid) return;
  164. var id = output.pid;
  165. var rlinks = this.links[id];
  166. var arr = rlinks[output.name];
  167. for (var index = 0; index < arr.length; index++) {
  168. var _arr$index2 = arr[index],
  169. pid = _arr$index2.pid,
  170. name = _arr$index2.name; // resolve
  171. for (var z = 0; z < this.reverseLinks[pid][name].length; z++) {
  172. if (this.reverseLinks[pid][name][z].pid == id && this.reverseLinks[pid][name][z].name == output.name) {
  173. this.reverseLinks[pid][name].splice(z, 1);
  174. z = z - 1;
  175. }
  176. }
  177. }
  178. this.links[id][output.name] = [];
  179. }
  180. }, {
  181. key: "breakLinks",
  182. value: function breakLinks(id) {
  183. for (var name in this.reverseLinks[id]) {
  184. var arr = this.reverseLinks[id][name];
  185. for (var index = 0; index < arr.length; index++) {
  186. var _arr$index3 = arr[index],
  187. pid = _arr$index3.pid,
  188. _name = _arr$index3.name; // resolve
  189. for (var z = 0; z < this.links[pid][_name].length; z++) {
  190. if (this.links[pid][_name][z].pid == id) {
  191. this.links[pid][_name].splice(z, 1);
  192. z = z - 1;
  193. }
  194. }
  195. }
  196. }
  197. delete this.reverseLinks[id];
  198. delete this.links[id];
  199. delete this.inputs[id];
  200. delete this.outputs[id];
  201. }
  202. }, {
  203. key: "rename",
  204. value: function rename(oldid, newid) {
  205. for (var name in this.reverseLinks[oldid]) {
  206. var arr = this.reverseLinks[oldid][name];
  207. for (var index = 0; index < arr.length; index++) {
  208. var _arr$index4 = arr[index],
  209. pid = _arr$index4.pid,
  210. _name2 = _arr$index4.name; // resolve
  211. for (var z = 0; z < this.links[pid][_name2].length; z++) {
  212. if (this.links[pid][_name2][z].pid == oldid) {
  213. this.links[pid][_name2][z].pid = newid;
  214. }
  215. }
  216. }
  217. }
  218. this.reverseLinks[newid] = this.reverseLinks[oldid]; // NEED TO BE HERE
  219. for (var name in this.links[oldid]) {
  220. var _arr = this.links[oldid][name];
  221. for (var index = 0; index < _arr.length; index++) {
  222. var _arr$index5 = _arr[index],
  223. pid = _arr$index5.pid,
  224. _name3 = _arr$index5.name; // resolve
  225. for (var z = 0; z < this.reverseLinks[pid][_name3].length; z++) {
  226. if (this.reverseLinks[pid][_name3][z].pid == oldid) {
  227. this.reverseLinks[pid][_name3][z].pid = newid;
  228. }
  229. }
  230. }
  231. }
  232. this.links[newid] = this.links[oldid];
  233. this.inputs[newid] = this.inputs[oldid];
  234. this.outputs[newid] = this.outputs[oldid];
  235. delete this.inputs[oldid];
  236. delete this.outputs[oldid];
  237. delete this.reverseLinks[oldid];
  238. delete this.links[oldid];
  239. }
  240. }, {
  241. key: "connect",
  242. value: function connect(input, output) {
  243. this.createLink(input, output);
  244. }
  245. }, {
  246. key: "getOutputConnections",
  247. value: function getOutputConnections(output) {
  248. return this.links[output.pid][output.name];
  249. }
  250. }, {
  251. key: "getInputConnections",
  252. value: function getInputConnections(input) {
  253. return this.reverseLinks[input.pid][input.name];
  254. }
  255. }, {
  256. key: "isReady",
  257. value: function isReady(mod) {
  258. for (var i = 0; i < mod.inputs.length; i++) {
  259. if (!this.inputs[mod.id][mod.inputs[i].name]) {
  260. return false;
  261. }
  262. }
  263. for (var i = 0; i < mod.outputs.length; i++) {
  264. if (!this.outputs[mod.id][mod.outputs[i].name]) {
  265. return false;
  266. }
  267. } // maybe check more here!
  268. return true;
  269. }
  270. }, {
  271. key: "selectOutput",
  272. value: function selectOutput(output) {
  273. this.__selectedOutput = output;
  274. }
  275. }, {
  276. key: "connectSelected",
  277. value: function connectSelected(input) {
  278. if (this.__selectedOutput) this.createLink(input, this.__selectedOutput);
  279. }
  280. }, {
  281. key: "export",
  282. value: function _export() {
  283. var links = [];
  284. for (var i in this.links) {
  285. for (var j in this.links[i]) {
  286. if (this.links[i][j].length) {
  287. for (var z in this.links[i][j]) {
  288. var temp = {
  289. output: {
  290. name: j,
  291. pid: i
  292. },
  293. input: this.links[i][j][z]
  294. };
  295. links.push(temp);
  296. }
  297. }
  298. }
  299. }
  300. return links;
  301. }
  302. }]);
  303. return LinkManager;
  304. }();
  305. var _default = LinkManager;
  306. exports.default = _default;