mirror of
https://github.com/microsoft/BCQuality.git
synced 2026-08-06 09:26:52 +01:00
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 02baffe8-0600-430d-81fa-a9993685e7cb
74 lines
1.8 KiB
JavaScript
74 lines
1.8 KiB
JavaScript
const pendingChunks = [];
|
|
let callInProgress = false;
|
|
let transferHalted = false;
|
|
|
|
function sendRows(rows, maxArgumentsBytes) {
|
|
if (transferHalted)
|
|
throw new Error("Retry or discard the failed chunk before sending more rows.");
|
|
|
|
const encoder = new TextEncoder();
|
|
const chunks = [];
|
|
let chunk = [];
|
|
const argumentBytes = (payload) =>
|
|
encoder.encode(JSON.stringify([payload])).length;
|
|
|
|
for (const row of rows) {
|
|
if (argumentBytes(JSON.stringify([row])) > maxArgumentsBytes)
|
|
throw new Error("A row exceeds the configured payload limit.");
|
|
|
|
const candidate = JSON.stringify([...chunk, row]);
|
|
|
|
if (argumentBytes(candidate) <= maxArgumentsBytes) {
|
|
chunk.push(row);
|
|
continue;
|
|
}
|
|
|
|
chunks.push(JSON.stringify(chunk));
|
|
chunk = [row];
|
|
}
|
|
|
|
if (chunk.length > 0)
|
|
chunks.push(JSON.stringify(chunk));
|
|
|
|
pendingChunks.push(...chunks);
|
|
sendNextChunk();
|
|
}
|
|
|
|
function sendNextChunk() {
|
|
if (callInProgress || pendingChunks.length === 0)
|
|
return;
|
|
|
|
callInProgress = true;
|
|
const payload = pendingChunks[0];
|
|
Microsoft.Dynamics.NAV.InvokeExtensibilityMethod(
|
|
"StoreRows",
|
|
[payload],
|
|
false,
|
|
() => {
|
|
pendingChunks.shift();
|
|
callInProgress = false;
|
|
sendNextChunk();
|
|
},
|
|
() => {
|
|
callInProgress = false;
|
|
transferHalted = true;
|
|
showTransferError();
|
|
});
|
|
}
|
|
|
|
function retryFailedChunk() {
|
|
if (!transferHalted)
|
|
return;
|
|
|
|
transferHalted = false;
|
|
sendNextChunk();
|
|
}
|
|
|
|
function discardFailedChunk() {
|
|
if (!transferHalted)
|
|
return;
|
|
|
|
pendingChunks.shift();
|
|
transferHalted = false;
|
|
sendNextChunk();
|
|
}
|