From b4e7fa625a10f3d81eb7c07a2b96e45c3bbb8f55 Mon Sep 17 00:00:00 2001 From: Kai Salmen Date: Mon, 24 Jun 2019 22:10:05 +0200 Subject: [PATCH] Implemented review suggestions and added additional code doc --- examples/jsm/loaders/OBJLoader2Parallel.js | 7 +- .../jsm/loaders/obj2/shared/MeshReceiver.js | 2 - .../worker/main/WorkerExecutionSupport.js | 106 ++++++++++++------ 3 files changed, 72 insertions(+), 43 deletions(-) diff --git a/examples/jsm/loaders/OBJLoader2Parallel.js b/examples/jsm/loaders/OBJLoader2Parallel.js index 0071bb1be8..5b422252ea 100644 --- a/examples/jsm/loaders/OBJLoader2Parallel.js +++ b/examples/jsm/loaders/OBJLoader2Parallel.js @@ -56,11 +56,6 @@ OBJLoader2Parallel.prototype.setCallbackOnParseComplete = function ( onParseComp if ( onParseComplete !== undefined && onParseComplete !== null ) { this.callbacks.onParseComplete = onParseComplete; } - else { - - throw "No callbackOnLoad was provided! Aborting!"; - - } return this; }; @@ -121,7 +116,7 @@ OBJLoader2Parallel.prototype.buildWorkerCode = function () { */ OBJLoader2Parallel.prototype._configure = function () { if ( this.callbacks.onParseComplete === null ) { - "No callbackOnLoad was provided! Aborting!" + throw "No callbackOnLoad was provided! Aborting!"; } // check if worker is already available and if so, then fast-fail if ( this.workerExecutionSupport.isWorkerLoaded( this.preferJsmWorker ) ) return; diff --git a/examples/jsm/loaders/obj2/shared/MeshReceiver.js b/examples/jsm/loaders/obj2/shared/MeshReceiver.js index ea6dc14d15..98fb877dec 100644 --- a/examples/jsm/loaders/obj2/shared/MeshReceiver.js +++ b/examples/jsm/loaders/obj2/shared/MeshReceiver.js @@ -11,8 +11,6 @@ import { Points } from "../../../../../build/three.module.js"; -import { MaterialHandler } from "./MaterialHandler.js"; - /** * diff --git a/examples/jsm/loaders/obj2/worker/main/WorkerExecutionSupport.js b/examples/jsm/loaders/obj2/worker/main/WorkerExecutionSupport.js index 3854064b0e..a6a2efdfa4 100644 --- a/examples/jsm/loaders/obj2/worker/main/WorkerExecutionSupport.js +++ b/examples/jsm/loaders/obj2/worker/main/WorkerExecutionSupport.js @@ -18,7 +18,7 @@ const CodeBuilderInstructions = function ( supportsStandardWorker, supportsJsmWo this.codeFragments = []; this.importStatements = []; - this.jsmWorkerFile; + this.jsmWorkerFile = null; this.defaultGeometryType = 0; }; @@ -38,18 +38,37 @@ CodeBuilderInstructions.prototype = { return this.preferJsmWorker; }, + /** + * Set the full path to the module that contains the worker code. + * + * @param {String} jsmWorkerFile + */ setJsmWorkerFile: function ( jsmWorkerFile ) { - this.jsmWorkerFile = jsmWorkerFile; + if ( jsmWorkerFile !== undefined && jsmWorkerFile !== null ) { + this.jsmWorkerFile = jsmWorkerFile; + } }, + /** + * Add code that is contained in addition to fragments and libraries + * @param {String} startCode + */ addStartCode: function ( startCode ) { this.startCode = startCode; }, + /** + * Add code fragment that is included in the provided order + * @param {String} code + */ addCodeFragment: function ( code ) { this.codeFragments.push( code ); }, + /** + * Add full path to a library that is contained at the start of the worker via "importScripts" + * @param {String} libraryPath + */ addLibraryImport: function ( libraryPath ) { let libraryUrl = new URL( libraryPath, window.location.href ).href; let code = 'importScripts( "' + libraryUrl + '" );'; @@ -82,7 +101,7 @@ const WorkerExecutionSupport = function () { this._reset(); }; -WorkerExecutionSupport.WORKER_SUPPORT_VERSION = '3.0.0-preview'; +WorkerExecutionSupport.WORKER_SUPPORT_VERSION = '3.0.0-beta2'; console.info( 'Using WorkerSupport version: ' + WorkerExecutionSupport.WORKER_SUPPORT_VERSION ); @@ -221,24 +240,30 @@ WorkerExecutionSupport.prototype = { */ _buildWorkerJsm: function ( codeBuilderInstructions ) { let jsmSuccess = true; - this._buildWorkerCheckPreconditions( true, 'buildWorkerJsm' ); + let timeLabel = 'buildWorkerJsm'; + let workerAvailable = this._buildWorkerCheckPreconditions( true, timeLabel ); + if ( ! workerAvailable ) { - let workerFileUrl = new URL( codeBuilderInstructions.jsmWorkerFile, window.location.href ).href; - try { + let workerFileUrl = new URL( codeBuilderInstructions.jsmWorkerFile, window.location.href ).href; + try { - let worker = new Worker( workerFileUrl, { type: "module" } ); - this._configureWorkerCommunication( worker, true, codeBuilderInstructions.defaultGeometryType, 'buildWorkerJsm' ); + let worker = new Worker( workerFileUrl, { type: "module" } ); + this._configureWorkerCommunication( worker, true, codeBuilderInstructions.defaultGeometryType, timeLabel ); - } - catch ( e ) { + } + catch ( e ) { - jsmSuccess = false; - if ( e instanceof TypeError || e instanceof SyntaxError ) { + jsmSuccess = false; + // Chrome throws this exception, but Firefox currently does not complain, but can't execute the worker afterwards + if ( e instanceof TypeError || e instanceof SyntaxError ) { - console.error( "Modules are not supported in workers." ); + console.error( "Modules are not supported in workers." ); + } } + } + return jsmSuccess; }, @@ -254,34 +279,45 @@ WorkerExecutionSupport.prototype = { * @private */ _buildWorkerStandard: function ( codeBuilderInstructions ) { - this._buildWorkerCheckPreconditions( false,'buildWorkerStandard' ); - - let concatenateCode = ''; - codeBuilderInstructions.getImportStatements().forEach( function ( element ) { - concatenateCode += element + '\n'; - } ); - concatenateCode += '\n'; - codeBuilderInstructions.getCodeFragments().forEach( function ( element ) { - concatenateCode += element+ '\n'; - } ); - concatenateCode += '\n'; - concatenateCode += codeBuilderInstructions.getStartCode(); - - let blob = new Blob( [ concatenateCode ], { type: 'application/javascript' } ); - let worker = new Worker( window.URL.createObjectURL( blob ) ); - - this._configureWorkerCommunication( worker, false, codeBuilderInstructions.defaultGeometryType, 'buildWorkerStandard' ); + let timeLabel = 'buildWorkerStandard'; + let workerAvailable = this._buildWorkerCheckPreconditions( false, timeLabel ); + if ( ! workerAvailable ) { + + let concatenateCode = ''; + codeBuilderInstructions.getImportStatements().forEach( function ( element ) { + concatenateCode += element + '\n'; + } ); + concatenateCode += '\n'; + codeBuilderInstructions.getCodeFragments().forEach( function ( element ) { + concatenateCode += element + '\n'; + } ); + concatenateCode += '\n'; + concatenateCode += codeBuilderInstructions.getStartCode(); + + let blob = new Blob( [ concatenateCode ], { type: 'application/javascript' } ); + let worker = new Worker( window.URL.createObjectURL( blob ) ); + + this._configureWorkerCommunication( worker, false, codeBuilderInstructions.defaultGeometryType, timeLabel ); + + } }, _buildWorkerCheckPreconditions: function ( requireJsmWorker, timeLabel ) { - if ( this.isWorkerLoaded( requireJsmWorker ) ) return; + let workerAvailable = false; + if ( this.isWorkerLoaded( requireJsmWorker ) ) { - if ( this.logging.enabled ) { + workerAvailable = true; - console.info( 'WorkerExecutionSupport: Building ' + ( requireJsmWorker ? 'jsm' : 'standard' ) + ' worker code...' ); - console.time( timeLabel ); + } else { + if ( this.logging.enabled ) { + + console.info( 'WorkerExecutionSupport: Building ' + ( requireJsmWorker ? 'jsm' : 'standard' ) + ' worker code...' ); + console.time( timeLabel ); + + } } + return workerAvailable; }, _configureWorkerCommunication: function ( worker, haveJsmWorker, defaultGeometryType, timeLabel ) { @@ -402,7 +438,7 @@ WorkerExecutionSupport.prototype = { }, _postMessage: function () { - if ( this.worker.queuedMessage !== null && this.isWorkerLoaded( this.worker.jsmWorker ) ) { + if ( this.worker.queuedMessage !== null ) { if ( this.worker.queuedMessage.payload.data.input instanceof ArrayBuffer ) { -- GitLab