bcquality/microsoft/knowledge/ui/control-addin-throttle-al-calls-and-payload-size.good.js
Jesper Schulz-Wedde cf55246ecf Add P0 integration and control add-in guidance
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 02baffe8-0600-430d-81fa-a9993685e7cb
2026-07-14 12:19:22 +02:00

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();
}