Skip to content

Commit

Permalink
Added task queue
Browse files Browse the repository at this point in the history
  • Loading branch information
yushulx committed Jul 15, 2020
1 parent 42ba9ab commit 24fc0d7
Showing 1 changed file with 163 additions and 5 deletions.
168 changes: 163 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,172 @@ var barcodeTypes = formats.OneD | formats.PDF417 | formats.QRCode | formats.Data
// Please visit https://www.dynamsoft.com/CustomerPortal/Portal/Triallicense.aspx or contact [email protected] to get a valid trial or full license.
dbr.initLicense(license);

/*
* https://github.com/Keillion/www.keillion.site/blob/v0/js/task-queue.js
*/
var TaskQueue = function(){
/// <summary>
/// @class TaskQueue
/// </summary>

this._queue = [];
this.isWorking = false;

/// <param name="timeout" type="int">
/// Timeout between task.
/// Between the interval, other work can be done, such as UI-response work.
/// </param>
this.timeout = 100;
};

TaskQueue.prototype.push = function(task, context, args, handleReturn){
/// <summary>
/// Push task. If <span>!isWorking</span>, start the task queue automatically.
/// </summary>

this._queue.push({
"task": task,
"context": context,
"args": args,
"handleReturn": handleReturn
});
if(!this.isWorking){
this.next();
}
};

TaskQueue.prototype.unshift = function(task, context, args, handleReturn){
/// <summary>
/// Push task. If <span>!isWorking</span>, start the task queue automatically.
/// </summary>

this._queue.unshift({
"task": task,
"context": context,
"args": args,
"handleReturn": handleReturn
});
if(!this.isWorking){
this.next();
}
};

TaskQueue.prototype.next = function(){
/// <summary>
/// Do the next task.
/// You need to call it manually in the end of your task.
/// To assure <function>next</function> will be called,
/// in some case you can put the function in <span>finally</span>,
/// in other case you should carefully handle <span>setTimeout</span>.
/// </summary>

if(this._queue.length == 0){
this.isWorking = false;
return;
}
this.isWorking = true;
var item = this._queue.shift();
var task = item.task;
var taskContext = item.context ? item.context : null;
var taskArguments = item.args ? item.args : [];
var handleReturn = item.handleReturn;
setTimeout(function(){
var ret = task.apply(taskContext, taskArguments);
if(typeof handleReturn == 'function'){
handleReturn(ret);
}
}, this.timeout);
};

/*
TaskQueue.test = function(){
var taskQueue = new TaskQueue();
var task = function(mess){
console.log(mess);
taskQueue.next();
};
for(var i = 0; i < 100; ++i){
taskQueue.push(task, null, [i]);
}
};*/






var dbrTaskQueue = new TaskQueue();
dbrTaskQueue.timeout = 0;
module.exports = {
decodeFileAsync: dbr.decodeFileAsync,
decodeFileStreamAsync: dbr.decodeFileStreamAsync,
decodeBase64Async: dbr.decodeBase64Async,
decodeYUYVAsync: dbr.decodeYUYVAsync,
decodeFileAsync: function(){
var callback = arguments[2];
arguments[2] = function(){
try{
callback.apply(this, arguments);
}catch(ex){
setTimeout(function(){throw ex;},0); // when the user defined callback throw err, taskqueue can still work
}
dbrTaskQueue.next();
};
dbrTaskQueue.push(dbr.decodeFileAsync, null, Array.from(arguments));
},
decodeFileStreamAsync: function(){
var callback = arguments[3];
arguments[3] = function(){
try{
callback.apply(this, arguments);
}catch(ex){
setTimeout(function(){throw ex;},0); // when the user defined callback throw err, taskqueue can still work
}
dbrTaskQueue.next();
};
dbrTaskQueue.push(dbr.decodeFileStreamAsync, null, Array.from(arguments));
},
decodeBase64Async: function(){
var callback = arguments[2];
arguments[2] = function(){
try{
callback.apply(this, arguments);
}catch(ex){
setTimeout(function(){throw ex;},0); // when the user defined callback throw err, taskqueue can still work
}
dbrTaskQueue.next();
};
dbrTaskQueue.push(dbr.decodeBase64Async, null, Array.from(arguments));
},
decodeYUYVAsync: function(){
var callback = arguments[4];
arguments[4] = function(){
try{
callback.apply(this, arguments);
}catch(ex){
setTimeout(function(){throw ex;},0); // when the user defined callback throw err, taskqueue can still work
}
dbrTaskQueue.next();
};
dbrTaskQueue.push(dbr.decodeYUYVAsync, null, Array.from(arguments));
},
formats: formats,
initLicense: dbr.initLicense,
barcodeTypes: barcodeTypes,
setParameters: dbr.setParameters,
decodeBufferAsync: dbr.decodeBufferAsync
decodeBufferAsync: function(){
maxBufferLength = 1
if (arguments.length == 8) {
maxBufferLength = arguments[7]
}

if (dbrTaskQueue._queue.length > maxBufferLength) return;

var callback = arguments[5];
arguments[5] = function(){
try{
callback.apply(this, arguments);
}catch(ex){
setTimeout(function(){throw ex;},0); // when the user defined callback throw err, taskqueue can still work
}
dbrTaskQueue.next();
};
dbrTaskQueue.push(dbr.decodeBufferAsync, null, Array.from(arguments));
},
};

0 comments on commit 24fc0d7

Please sign in to comment.