maybe I’m doing something wrong (with a “multiple” callCommand call), but I’m getting weird results. the following code:
document.getElementById("buttonIDtest2").onclick = function() {
Asc.scope.i = 0;
while (Asc.scope.i < 5)
{
Asc.scope.i++;
console.log('[',Date.now(),'] Asc.scope.i (while) = ',Asc.scope.i);
window.Asc.plugin.callCommand(
function () {
Asc.scope.i++;
console.log('[',Date.now(),'] iRetCode(int) = Asc.scope.i (int) = ',Asc.scope.i);
return { iRetCode : Asc.scope.i };
}, false, false);
window.Asc.plugin.onCommandCallback = function (res1) {
console.log('[',Date.now(),'] iRetCode(ext) = ',res1.iRetCode)
Asc.scope.i++
console.log('[',Date.now(),'] Asc.scope.i (onCommandCallback) = ',Asc.scope.i);
};
}
};
I was hoping it would run (and added the time in milliseconds to check) like this:
Asc.scope.i (while) = 1
iRetCode(int) = Asc.scope.i (int) = 2
iRetCode(ext) = 2
Asc.scope.i (onCommandCallback) = 3
Asc.scope.i (while) = 4
iRetCode(int) = Asc.scope.i (int) = 5
iRetCode(ext) = 5
Asc.scope.i (onCommandCallback) = 6
...
But in fact, I see this:
[ 1738663271433 ] Asc.scope.i (while) = 1
[ 1738663271434 ] Asc.scope.i (while) = 2
[ 1738663271434 ] Asc.scope.i (while) = 3
[ 1738663271434 ] Asc.scope.i (while) = 4
[ 1738663271434 ] Asc.scope.i (while) = 5
[ 1738663271437 ] iRetCode(int) = Asc.scope.i (int) = 2
[ 1738663271443 ] iRetCode(int) = Asc.scope.i (int) = 3
[ 1738663271449 ] iRetCode(int) = Asc.scope.i (int) = 4
[ 1738663271453 ] iRetCode(int) = Asc.scope.i (int) = 5
[ 1738663271456 ] iRetCode(int) = Asc.scope.i (int) = 6
[ 1738663271459 ] iRetCode(ext) = 2
[ 1738663271459 ] Asc.scope.i (onCommandCallback) = 6
[ 1738663271459 ] iRetCode(ext) = 3
[ 1738663271459 ] Asc.scope.i (onCommandCallback) = 7
[ 1738663271459 ] iRetCode(ext) = 4
[ 1738663271459 ] Asc.scope.i (onCommandCallback) = 8
[ 1738663271460 ] iRetCode(ext) = 5
[ 1738663271460 ] Asc.scope.i (onCommandCallback) = 9
[ 1738663271460 ] iRetCode(ext) = 6
[ 1738663271460 ] Asc.scope.i (onCommandCallback) = 10
why does this happen ?
If the Onlyoffce engine algorithm works only like this (all the steps of the “external” loop of while
are executed first, then all callCommand
functions at once, then all onCommandCallback
functions at once), then how do I implement such an algorithm:
- Use
callCommand
to get the number of rows from the document table
- if the number of rows is greater than zero, then use the
while
loop, where the condition is that the current row (the number of which is returned from onCommandCallback
on each iteration of the while
loop, and within callCommand
to process 20-30 rows at a time) is less than the number of rows in the table, or that the previous callCommand
was with an error (problems with row processing), and inside while
there will be a callCommand
, But after each call, analyze onCommandCallback
to see if we need to exit while
.
How to implement this if, when executing the test code (see above), we get the “wrong” order of command execution ?
I could assume that the code works very fast, and the onCommandCallback
functions work in a separate thread and because of this the callCommand
functions and onCommandCallback
are “mixed”, but I ran it 5 times in a row - and the order is always strictly like this (both the order in the log and the chronology by timestamps)…