Skip to content

Commit

Permalink
Implemented review suggestions and added additional code doc
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisalmen committed Jun 24, 2019
1 parent 05a07fb commit b4e7fa6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 43 deletions.
7 changes: 1 addition & 6 deletions examples/jsm/loaders/OBJLoader2Parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions examples/jsm/loaders/obj2/shared/MeshReceiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
Points
} from "../../../../../build/three.module.js";

import { MaterialHandler } from "./MaterialHandler.js";


/**
*
Expand Down
106 changes: 71 additions & 35 deletions examples/jsm/loaders/obj2/worker/main/WorkerExecutionSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const CodeBuilderInstructions = function ( supportsStandardWorker, supportsJsmWo
this.codeFragments = [];
this.importStatements = [];

this.jsmWorkerFile;
this.jsmWorkerFile = null;
this.defaultGeometryType = 0;
};

Expand All @@ -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 + '" );';
Expand Down Expand Up @@ -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 );


Expand Down Expand Up @@ -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;
},

Expand All @@ -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 ) {
Expand Down Expand Up @@ -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 ) {

Expand Down

0 comments on commit b4e7fa6

Please sign in to comment.