UNPKG

@slavmak2486/bx24ts

Version:

Library for bitrix24

99 lines (98 loc) 4.14 kB
"use strict"; 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.BatchHelper = void 0; class BatchHelper { constructor() { this.allRequest = []; } addToBatch(batchElement) { this.allRequest.push(...Object.entries(batchElement)); return this; } findDependencies(totalBatch) { const dependencies = {}; for (const [key, val] of totalBatch) { const params = Array.isArray(val) ? val[1] : val.params; const paramStr = JSON.stringify(params); const matches = paramStr.match(/\$result\[([^\]]+)\]/g); if (matches) { const parents = matches.map(match => match.replace(/\$result\[|\]/g, '')); parents.forEach(parent => { if (!dependencies[parent]) { dependencies[parent] = []; } dependencies[parent].push(key); }); } } return dependencies; } getArrBatches(countInBatch = 50) { var _a, _b; const result = []; const dependencies = this.findDependencies(this.allRequest); let currentSize = 0; let currentChunk = {}; const alradyInBatch = []; const obj = this.allRequest.reduce((result, el) => Object.assign(result, { [el[0]]: el[1] }), {}); for (const [key, value] of this.allRequest) { if (alradyInBatch.includes(key)) continue; if ((((_a = dependencies[key]) === null || _a === void 0 ? void 0 : _a.length) || 0) > countInBatch) { throw new Error("BatchHelper: too many dependencies for " + key); } if (currentSize >= countInBatch || (currentSize + 1 + (((_b = dependencies[key]) === null || _b === void 0 ? void 0 : _b.length) || 0)) > countInBatch) { result.push(currentChunk); currentChunk = {}; currentSize = 0; } alradyInBatch.push(key); currentChunk[key] = value; const deps = dependencies[key] || []; for (const dep of deps) { alradyInBatch.push(dep); currentChunk[dep] = obj[dep]; currentSize++; } currentSize++; } if (Object.keys(currentChunk).length > 0) { result.push(currentChunk); } return result; } runAll(bx24) { return __awaiter(this, void 0, void 0, function* () { const totalResult = {}; const arrBatch = this.getArrBatches(); for (const batch of arrBatch) { const ress = yield bx24.callBatch(batch); Object.assign(totalResult, ress); } return totalResult; }); } getBatchForLength(action, requestName, countRows, params = {}) { const countBatch = Math.ceil(countRows / (50 * 50)); for (let i = 0; i < countBatch; i++) { for (let j = 0; j < 50; j++) { if (50 * 50 * i + j * 50 >= countRows) { break; } const curParams = Object.assign({ start: 0 }, params); curParams['start'] = 50 * 50 * i + j * 50; this.addToBatch({ [requestName + `${i}-${j}`]: [action, curParams] }); } } } } exports.BatchHelper = BatchHelper;