Skip to content

Commit

Permalink
updated TaskQueue now with Worker support
Browse files Browse the repository at this point in the history
  • Loading branch information
robp94 committed Oct 29, 2018
1 parent ad94d12 commit c8377cf
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 197 deletions.
139 changes: 89 additions & 50 deletions build/yuka.js
Original file line number Diff line number Diff line change
Expand Up @@ -7289,6 +7289,12 @@
resolve() {
}

executeWorker( array ) {

this.taskType.post( this.taskType.tasksToData( array ) );

}

}

/**
Expand Down Expand Up @@ -7337,8 +7343,6 @@
this.activeRequest = new Map();
this.nextId = 0;

this.worker = new Worker( path );

this.newRequests = new Array();

this.taskQueue = taskQueue;
Expand All @@ -7354,7 +7358,6 @@

}
this.useWorker = false;
this.init();//???

}

Expand All @@ -7365,14 +7368,15 @@

const f32Array = new Float32Array( event.data.buffer );
this.resolvePromises( f32Array );
//todo use window.requestIdleCallback

} );

this.worker.postMessage( { op: 'init' } );

}

post() {
post( array ) {
//send data to worker
}

Expand Down Expand Up @@ -7415,10 +7419,26 @@

}

resolvePromises() {
resolvePromises( f32Array ) {
//resolvePromises used for worker
}

tasksToData( tasks ) {

const dataArray = new Array();
const l = tasks.length;
for ( let i = 0; i < l; i ++ ) {

const task = tasks[ i ];
dataArray.push( task.requestId, task.from.x, task.from.y, task.from.z, task.to.x, task.to.y, task.to.z );

}

return dataArray;

}


}

/**
Expand All @@ -7441,18 +7461,7 @@
const requestId = this.nextId;
const promise = this.createPromise();


if ( this.useWorker ) {

this.array.push( requestId, from.x, from.y, from.z, to.x, to.y, to.z );

} else {

this.taskQueue.enqueue( new TaskPathPlanner( requestId, from, to, this ) );

}

//this.worker.postMessage( { op: 'search', requestId: requestId, from: f, to: t } );
this.taskQueue.enqueue( new TaskPathPlanner( requestId, from, to, this ) );

return promise;

Expand All @@ -7464,37 +7473,6 @@

}

post() {

const f = new Float32Array( this.array );
const buffer = f.buffer;
//this.array.length = 0;
this.worker.postMessage( { op: 'searches', buffer: buffer }, [ buffer ] );
/*console.time( 'main' );
this.findPaths(); //for performance
console.timeEnd( 'main' );*/
this.array.length = 0;

}

doWork( count = - 1 ) {

if ( this.array.length > 0 ) {

if ( this.useWorker ) {

this.post();

} else {

this.resolvePromises( this.findPaths( count ) );

}

}

}

findPaths( count = - 1 ) {

if ( count < 0 ) {
Expand Down Expand Up @@ -7539,6 +7517,16 @@
}


post( array ) {

const f = new Float32Array( array );
const buffer = f.buffer;
this.worker.postMessage( { op: 'searches', buffer: buffer }, [ buffer ] );




}

}

Expand All @@ -7553,6 +7541,13 @@
this.active = false;
this.handler = runTaskQueue.bind( this );
this.taskHandle = 0;
this.types = new Array();

}

addType( type ) {

this.types.push( type );

}

Expand All @@ -7577,6 +7572,38 @@

}

bundel( taskType ) {

const l = this.tasks.length;
const positions = new Array();
const bundledTasks = new Array();
for ( let i = 0; i < l; i ++ ) {

if ( this.tasks[ i ].taskType instanceof taskType.constructor ) {

positions.push( i );
bundledTasks.push( this.tasks[ i ] );

}

}
/*
Optimize add rest to an other array
don't use push create array with length and set length afterwards
*/

const l2 = positions.length;
positions.reverse();
for ( let i = 0; i < l2; i ++ ) {

this.tasks.splice( positions[ i ], 1 );

}

return bundledTasks;

}

}

function runTaskQueue( deadline ) {
Expand All @@ -7585,9 +7612,21 @@

while ( ( deadline.timeRemaining() > 0 || deadline.didTimeout ) && tasks.length > 0 ) {

tasks[ 0 ].execute();
const task = tasks[ 0 ];

if ( task.taskType.useWorker ) {

task.executeWorker( this.bundel( task.taskType ) );

} else {

task.execute();

tasks.shift();

}


tasks.shift();

}

Expand Down
Loading

0 comments on commit c8377cf

Please sign in to comment.