mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-06 14:05:46 +01:00
batch.processArray
use batch.processArray in notifications.push and moveRecentReplies added notification tests added test for moveRecentReplies clear group cache if a group is deleted
This commit is contained in:
50
src/batch.js
50
src/batch.js
@@ -16,7 +16,7 @@ var async = require('async'),
|
||||
options = {};
|
||||
}
|
||||
|
||||
callback = typeof callback === 'function' ? callback : function(){};
|
||||
callback = typeof callback === 'function' ? callback : function() {};
|
||||
options = options || {};
|
||||
|
||||
if (typeof process !== 'function') {
|
||||
@@ -63,4 +63,52 @@ var async = require('async'),
|
||||
);
|
||||
};
|
||||
|
||||
Batch.processArray = function(array, process, options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options;
|
||||
options = {};
|
||||
}
|
||||
|
||||
callback = typeof callback === 'function' ? callback : function() {};
|
||||
options = options || {};
|
||||
|
||||
if (!Array.isArray(array) || !array.length) {
|
||||
return callback();
|
||||
}
|
||||
if (typeof process !== 'function') {
|
||||
return callback(new Error('[[error:process-not-a-function]]'));
|
||||
}
|
||||
|
||||
var batch = options.batch || DEFAULT_BATCH_SIZE;
|
||||
var start = 0;
|
||||
var done = false;
|
||||
|
||||
async.whilst(
|
||||
function() {
|
||||
return !done;
|
||||
},
|
||||
function(next) {
|
||||
var currentBatch = array.slice(start, start + batch);
|
||||
if (!currentBatch.length) {
|
||||
done = true;
|
||||
return next();
|
||||
}
|
||||
process(currentBatch, function(err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
start = start + batch;
|
||||
if (options.interval) {
|
||||
setTimeout(next, options.interval);
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
},
|
||||
function(err) {
|
||||
callback(err);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
}(exports));
|
||||
|
||||
Reference in New Issue
Block a user