ottoman
Version:
Ottoman Couchbase ODM
65 lines • 2.53 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.batchProcessQueue = exports.chunkArray = void 0;
const types_1 = require("./types");
/**
* @ignore
*/
const chunkArray = (list, size) => {
const clonedList = [...list];
const results = [];
while (clonedList.length) {
results.push(clonedList.splice(0, size));
}
return results;
};
exports.chunkArray = chunkArray;
/**
* @ignore
*/
function* processBatch(items, fn, metadata, extra, options) {
const clonedItems = [...items];
for (const items of clonedItems) {
yield fn(items, metadata, extra, options)
.then((result) => result)
.catch((error) => error);
}
}
/**
* @ignore
*/
const batchProcessQueue = (metadata) => (items, fn, extra = {}, options = {}, throttle = 100) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const chunks = (0, exports.chunkArray)([...items], throttle);
const result = { success: 0, match_number: items.length, errors: [], data: [] };
for (const chunk of chunks) {
const promises = [];
for (const promise of processBatch(chunk, fn, metadata, extra, options)) {
promises.push(promise);
}
const batchResults = yield Promise.all(promises);
for (const r of batchResults) {
if (r.status === 'FAILURE') {
result.errors.push(r);
}
else {
result.success += 1;
if (r.payload) {
(_a = result.data) === null || _a === void 0 ? void 0 : _a.push(r.payload);
}
}
}
}
return new types_1.ManyQueryResponse(result.errors.length > 0 ? 'FAILURE' : 'SUCCESS', result);
});
exports.batchProcessQueue = batchProcessQueue;
//# sourceMappingURL=utils.js.map