-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
163 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
}, | ||
}; |