UNPKG

webgme

Version:

Web-based Generic Modeling Environment

166 lines (137 loc) 4.96 kB
/*globals require,importScripts*/ // This is the code for using the autorouter as a web worker. var worker = this, window = {}, //jshint ignore: line WebGMEGlobal = {gmeConfig: {}}, respondToAll = false, basePath = '/', msgQueue = []; /** * Start the worker. This is done after the relevant config has been received. * * @return {undefined} */ var startWorker = function () { 'use strict'; if (WebGMEGlobal.gmeConfig.client.mountedPath) { basePath = WebGMEGlobal.gmeConfig.client.mountedPath + '/'; } importScripts(basePath + 'common/lib/requirejs/require.js'); // Queue any messages received while loading the dependencies worker.onmessage = function (msg) { msgQueue.push(msg); }; require({ baseUrl: basePath, paths: { client: 'client', underscore: 'bower_components/underscore/underscore-min', debug: 'common/lib/debug/debug', AutoRouterActionApplier: 'lib/autorouter/action-applier.min' // create a map file for debugging }, shim: { debug: { exports: 'window.debug' } } }, [ 'AutoRouterActionApplier', 'client/logger', 'underscore' ], function ( ActionApplier, Logger, _ ) { var AutoRouterWorker = function () { // Add recording actions? this.logger = Logger.create('gme:Widgets:DiagramDesigner:AutoRouter:Worker', WebGMEGlobal.gmeConfig.client.log); this._recordActions = true; this.init(); this.respondTo = { getPathPoints: true, routePaths: true, addBox: true, addPath: true }; }; AutoRouterWorker.prototype.handleMessage = function (msg) { this.logger.debug('Received:', msg.data); this._handleMessage(msg.data); }; AutoRouterWorker.prototype._handleMessage = function (msg) { var response, action = msg[0], lightResult, result; response = [action, JSON.parse(JSON.stringify(msg[1]))]; // Copy the input args // If routing async, decorate the request if (action === 'routeAsync') { // Send getPathPoints response for each path on each update msg[1] = [{callback: this._updatePaths.bind(this)}]; } try { result = this._invokeAutoRouterMethodUnsafe.apply(this, msg); } catch (e) { // Send error message worker.postMessage(['BugReplayList', this._getActionSequence()]); } if (respondToAll || this.respondTo[action]) { if (result) { if (action === 'addBox') { lightResult = {id: result.box.id}; lightResult.ports = Object.keys(result.ports).map(function (key) { return {id: key}; }); } } if (lightResult) { response.push(lightResult); } else { response.push(result); } this.logger.debug('Response:', response); worker.postMessage(response); } }; /** * Update all the paths on the graph. * * @return {undefined} */ AutoRouterWorker.prototype._updatePaths = function (paths) { this.logger.debug('Updating paths'); var id, points, content = [], msg = ['routePaths', null]; for (var i = paths.length; i--;) { id = this._arPathId2Original[paths[i].id]; points = this._invokeAutoRouterMethod('getPathPoints', [id]); content.push([id, points]); } msg[1] = content; worker.postMessage(msg); }; _.extend(AutoRouterWorker.prototype, ActionApplier.prototype); var autorouterWorker = new AutoRouterWorker(); autorouterWorker.logger.debug('AR Worker is now listening...'); // Handle the queued messages while (msgQueue.length) { autorouterWorker.handleMessage(msgQueue.shift()); } worker.onmessage = autorouterWorker.handleMessage.bind(autorouterWorker); autorouterWorker.logger.debug('Ready for requests!'); worker.postMessage('READY'); }.bind(this)); }; // Set the WebGMEGlobal.gmeConfig.client config value for use in the loggers worker.onmessage = function (msg) { 'use strict'; WebGMEGlobal.gmeConfig.client = msg.data[0]; respondToAll = msg.data[1]; startWorker(); };