File size: 8,591 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ELK = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/*******************************************************************************
 * Copyright (c) 2017 Kiel University and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
var ELK = function () {
  function ELK() {
    var _this = this;

    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        _ref$defaultLayoutOpt = _ref.defaultLayoutOptions,
        defaultLayoutOptions = _ref$defaultLayoutOpt === undefined ? {} : _ref$defaultLayoutOpt,
        _ref$algorithms = _ref.algorithms,
        algorithms = _ref$algorithms === undefined ? ['layered', 'stress', 'mrtree', 'radial', 'force', 'disco', 'sporeOverlap', 'sporeCompaction', 'rectpacking'] : _ref$algorithms,
        workerFactory = _ref.workerFactory,
        workerUrl = _ref.workerUrl;

    _classCallCheck(this, ELK);

    this.defaultLayoutOptions = defaultLayoutOptions;
    this.initialized = false;

    // check valid worker construction possible
    if (typeof workerUrl === 'undefined' && typeof workerFactory === 'undefined') {
      throw new Error("Cannot construct an ELK without both 'workerUrl' and 'workerFactory'.");
    }
    var factory = workerFactory;
    if (typeof workerUrl !== 'undefined' && typeof workerFactory === 'undefined') {
      // use default Web Worker
      factory = function factory(url) {
        return new Worker(url);
      };
    }

    // create the worker
    var worker = factory(workerUrl);
    if (typeof worker.postMessage !== 'function') {
      throw new TypeError("Created worker does not provide" + " the required 'postMessage' function.");
    }

    // wrap the worker to return promises
    this.worker = new PromisedWorker(worker);

    // initially register algorithms
    this.worker.postMessage({
      cmd: 'register',
      algorithms: algorithms
    }).then(function (r) {
      return _this.initialized = true;
    }).catch(console.err);
  }

  _createClass(ELK, [{
    key: 'layout',
    value: function layout(graph) {
      var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
          _ref2$layoutOptions = _ref2.layoutOptions,
          layoutOptions = _ref2$layoutOptions === undefined ? this.defaultLayoutOptions : _ref2$layoutOptions,
          _ref2$logging = _ref2.logging,
          logging = _ref2$logging === undefined ? false : _ref2$logging,
          _ref2$measureExecutio = _ref2.measureExecutionTime,
          measureExecutionTime = _ref2$measureExecutio === undefined ? false : _ref2$measureExecutio;

      if (!graph) {
        return Promise.reject(new Error("Missing mandatory parameter 'graph'."));
      }
      return this.worker.postMessage({
        cmd: 'layout',
        graph: graph,
        layoutOptions: layoutOptions,
        options: {
          logging: logging,
          measureExecutionTime: measureExecutionTime
        }
      });
    }
  }, {
    key: 'knownLayoutAlgorithms',
    value: function knownLayoutAlgorithms() {
      return this.worker.postMessage({ cmd: 'algorithms' });
    }
  }, {
    key: 'knownLayoutOptions',
    value: function knownLayoutOptions() {
      return this.worker.postMessage({ cmd: 'options' });
    }
  }, {
    key: 'knownLayoutCategories',
    value: function knownLayoutCategories() {
      return this.worker.postMessage({ cmd: 'categories' });
    }
  }, {
    key: 'terminateWorker',
    value: function terminateWorker() {
      if (this.worker) this.worker.terminate();
    }
  }]);

  return ELK;
}();

exports.default = ELK;

var PromisedWorker = function () {
  function PromisedWorker(worker) {
    var _this2 = this;

    _classCallCheck(this, PromisedWorker);

    if (worker === undefined) {
      throw new Error("Missing mandatory parameter 'worker'.");
    }
    this.resolvers = {};
    this.worker = worker;
    this.worker.onmessage = function (answer) {
      // why is this necessary?
      setTimeout(function () {
        _this2.receive(_this2, answer);
      }, 0);
    };
  }

  _createClass(PromisedWorker, [{
    key: 'postMessage',
    value: function postMessage(msg) {
      var id = this.id || 0;
      this.id = id + 1;
      msg.id = id;
      var self = this;
      return new Promise(function (resolve, reject) {
        // prepare the resolver
        self.resolvers[id] = function (err, res) {
          if (err) {
            self.convertGwtStyleError(err);
            reject(err);
          } else {
            resolve(res);
          }
        };
        // post the message
        self.worker.postMessage(msg);
      });
    }
  }, {
    key: 'receive',
    value: function receive(self, answer) {
      var json = answer.data;
      var resolver = self.resolvers[json.id];
      if (resolver) {
        delete self.resolvers[json.id];
        if (json.error) {
          resolver(json.error);
        } else {
          resolver(null, json.data);
        }
      }
    }
  }, {
    key: 'terminate',
    value: function terminate() {
      if (this.worker) {
        this.worker.terminate();
      }
    }
  }, {
    key: 'convertGwtStyleError',
    value: function convertGwtStyleError(err) {
      if (!err) {
        return;
      }
      // Somewhat flatten the way GWT stores nested exception(s)
      var javaException = err['__java$exception'];
      if (javaException) {
        // Note that the property name of the nested exception is different
        // in the non-minified ('cause') and the minified (not deterministic) version.
        // Hence, the version below only works for the non-minified version.
        // However, as the minified stack trace is not of much use anyway, one
        // should switch the used version for debugging in such a case.
        if (javaException.cause && javaException.cause.backingJsObject) {
          err.cause = javaException.cause.backingJsObject;
          this.convertGwtStyleError(err.cause);
        }
        delete err['__java$exception'];
      }
    }
  }]);

  return PromisedWorker;
}();
},{}],2:[function(require,module,exports){
"use strict";

/*******************************************************************************
 * Copyright (c) 2021 Kiel University and others.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 * 
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
var ELK = require('./elk-api.js').default;

Object.defineProperty(module.exports, "__esModule", {
  value: true
});
module.exports = ELK;
ELK.default = ELK;
},{"./elk-api.js":1}]},{},[2])(2)
});