- Move Video Block JS files from xmodule/js/src/video/ to xmodule/assets/video/public/js/ - Update JavaScript files from RequireJS to ES6 import/export - test: Enable and fix Karma Js tests for Video XBlock (#37351) --------- Co-authored-by: salmannawaz <salman.nawaz@arbisoft.com>
53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Provides convenient way to process big amount of data without UI blocking.
|
|
*
|
|
* @param {array} list Array to process.
|
|
* @param {function} process Calls this function on each item in the list.
|
|
* @return {array} Returns a Promise object to observe when all actions of a
|
|
* certain type bound to the collection, queued or not, have finished.
|
|
*/
|
|
let AsyncProcess = {
|
|
array: function(list, process) {
|
|
if (!_.isArray(list)) {
|
|
return $.Deferred().reject().promise();
|
|
}
|
|
|
|
if (!_.isFunction(process) || !list.length) {
|
|
return $.Deferred().resolve(list).promise();
|
|
}
|
|
|
|
let MAX_DELAY = 50, // maximum amount of time that js code should be allowed to run continuously
|
|
dfd = $.Deferred();
|
|
let result = [];
|
|
let index = 0;
|
|
let len = list.length;
|
|
|
|
let getCurrentTime = function() {
|
|
return (new Date()).getTime();
|
|
};
|
|
|
|
let handler = function() {
|
|
let start = getCurrentTime();
|
|
|
|
do {
|
|
result[index] = process(list[index], index);
|
|
index++;
|
|
} while (index < len && getCurrentTime() - start < MAX_DELAY);
|
|
|
|
if (index < len) {
|
|
setTimeout(handler, 25);
|
|
} else {
|
|
dfd.resolve(result);
|
|
}
|
|
};
|
|
|
|
setTimeout(handler, 25);
|
|
|
|
return dfd.promise();
|
|
}
|
|
};
|
|
|
|
export default AsyncProcess;
|