Add helper functions for async task running.

This commit is contained in:
kovacsv 2021-10-23 08:36:01 +02:00
parent 654ffd0acc
commit 19ca7a697b
4 changed files with 18 additions and 12 deletions

View File

@ -61,3 +61,15 @@ OV.RunTaskAsync = function (task)
task (); task ();
}, 0); }, 0);
}; };
OV.RunTasks = function (count, callbacks)
{
let taskRunner = new OV.TaskRunner ();
taskRunner.Run (count, callbacks);
};
OV.RunTaskBatch = function (count, batchCount, callbacks)
{
let taskRunner = new OV.TaskRunner ();
taskRunner.RunBatch (count, batchCount, callbacks);
};

View File

@ -62,8 +62,7 @@ OV.FileList = class
GetContent (onReady) GetContent (onReady)
{ {
let taskRunner = new OV.TaskRunner (); OV.RunTasks (this.files.length, {
taskRunner.Run (this.files.length, {
runTask : (index, complete) => { runTask : (index, complete) => {
this.GetFileContent (this.files[index], complete); this.GetFileContent (this.files[index], complete);
}, },

View File

@ -256,8 +256,7 @@ OV.ConvertModelToThreeObject = function (model, params, output, callbacks)
} }
let threeObject = new THREE.Object3D (); let threeObject = new THREE.Object3D ();
let taskRunner = new OV.TaskRunner (); OV.RunTaskBatch (model.MeshCount (), 100, {
taskRunner.RunBatch (model.MeshCount (), 100, {
runTask : (firstIndex, lastIndex, ready) => { runTask : (firstIndex, lastIndex, ready) => {
for (let meshIndex = firstIndex; meshIndex <= lastIndex; meshIndex++) { for (let meshIndex = firstIndex; meshIndex <= lastIndex; meshIndex++) {
let mesh = model.GetMesh (meshIndex); let mesh = model.GetMesh (meshIndex);

View File

@ -2,9 +2,8 @@ var assert = require ('assert');
describe ('Task Runner', function () { describe ('Task Runner', function () {
it ('Run task zero times', function (done) { it ('Run task zero times', function (done) {
var tr = new OV.TaskRunner ();
var numbers = []; var numbers = [];
tr.Run (0, { OV.RunTasks (0, {
runTask : function (index, ready) { runTask : function (index, ready) {
numbers.push (index); numbers.push (index);
ready (); ready ();
@ -17,9 +16,8 @@ describe ('Task Runner', function () {
}); });
it ('Run task three times', function (done) { it ('Run task three times', function (done) {
var tr = new OV.TaskRunner ();
var numbers = []; var numbers = [];
tr.Run (3, { OV.RunTasks (3, {
runTask : function (index, ready) { runTask : function (index, ready) {
numbers.push (index); numbers.push (index);
ready (); ready ();
@ -32,9 +30,8 @@ describe ('Task Runner', function () {
}); });
it ('Run task batched', function (done) { it ('Run task batched', function (done) {
var tr = new OV.TaskRunner ();
var indices = []; var indices = [];
tr.RunBatch (10, 3, { OV.RunTaskBatch (10, 3, {
runTask : function (firstIndex, lastIndex, ready) { runTask : function (firstIndex, lastIndex, ready) {
indices.push ([firstIndex, lastIndex]); indices.push ([firstIndex, lastIndex]);
ready (); ready ();
@ -47,9 +44,8 @@ describe ('Task Runner', function () {
}); });
it ('Run task batched zero times', function (done) { it ('Run task batched zero times', function (done) {
var tr = new OV.TaskRunner ();
var indices = []; var indices = [];
tr.RunBatch (0, 3, { OV.RunTaskBatch (0, 3, {
runTask : function (firstIndex, lastIndex, ready) { runTask : function (firstIndex, lastIndex, ready) {
indices.push ([firstIndex, lastIndex]); indices.push ([firstIndex, lastIndex]);
ready (); ready ();