devextreme
Version:
HTML5 JavaScript Component Suite for Responsive Web Development
38 lines (37 loc) • 1.68 kB
JavaScript
/**
* DevExtreme (esm/__internal/scheduler/utils/macro_task_array/methods.js)
* Version: 24.2.6
* Build date: Mon Mar 17 2025
*
* Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
import macroTaskDispatcher from "./dispatcher";
export const DEFAULT_STEPS_VALUE = 100;
export const DEFAULT_MACRO_TASK_TIMEOUT = 0;
export const macroTaskArrayForEach = async function(array, callback) {
let step = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : 100;
let macroTaskTimeoutMs = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : 0;
const promises = [];
const batchesCount = Math.ceil(array.length / step);
for (let batchIdx = 0; batchIdx < batchesCount; batchIdx += 1) {
const scheduledTask = macroTaskDispatcher.schedule((() => {
const startIdx = batchIdx * step;
const maxIdx = startIdx + step;
for (let idx = startIdx; idx < maxIdx && void 0 !== array[idx]; idx += 1) {
callback(array[idx])
}
}), macroTaskTimeoutMs);
promises.push(scheduledTask)
}
await Promise.all(promises)
};
export const macroTaskArrayMap = async function(array, callback) {
let step = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : 100;
let macroTaskTimeoutMs = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : 0;
const result = [];
await macroTaskArrayForEach(array, (item => {
result.push(callback(item))
}), step, macroTaskTimeoutMs);
return result
};