Items stuck in memory

I seem to have things stuck in memory, how can I fix this?
allTransactionsOfPubID does not get set to an empty array unless I press my button twice.
Here's my code:

const pageSize = 1000;

allTransactionsOfPubID.setValue([])

progressBarLoading.clearValue()
txtStatusCount.clearValue()
progressBarLoading.setHidden(false);
txtStatusCount.setHidden(false);

async function getTransactions(pageSize){
    GetTransactionsofPublicationID3.trigger({
      additionalScope: {
        currentPage: 0,
        pageSize: pageSize
      }
  })
}

async function loopTransactions(content, currentPage, pageSize){
  await GetTransactionsofPublicationID3.trigger({
    additionalScope: {
      currentPage: currentPage,
      pageSize: pageSize
    }
  })
}

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

await getTransactions(pageSize)
let content = GetTransactionsofPublicationID3 && GetTransactionsofPublicationID3.data && GetTransactionsofPublicationID3.data.content ? GetTransactionsofPublicationID3.data.content : "";

const totalPages = GetTransactionsofPublicationID3 && GetTransactionsofPublicationID3.data && GetTransactionsofPublicationID3.data.totalPages ? GetTransactionsofPublicationID3.data.totalPages : 1;
progressBarLoading.setValue((1/totalPages) * 100)

txtStatusCount.setValue(1 + "/" + totalPages + " query")
allTransactionsOfPubID.setValue(content);

for (let i = 1; i < totalPages; i++){
  let percent = ((i+1)/totalPages) * 100;
  await loopTransactions(content, i, pageSize)
  content = [...content,...GetTransactionsofPublicationID3.data.content]
  await sleep(1000)
  allTransactionsOfPubID.setValue(content);
  progressBarLoading.setValue(percent);
  txtStatusCount.setValue((i + 1) + "/" + totalPages + " query")
}
txtStatusCount.setValue("Done!");
await sleep(5000)
progressBarLoading.setHidden(true);
txtStatusCount.setHidden(true);

Essentially I have to do an API trigger to get the first set of results as well as the number of pages in the results. Then I loop for the number of pages and accumulate the results.
This does seem to work, but maybe I'm not doing this the correct way and that is causing the data to say in memory for some reason?

1 Like

setValue is actually asynchronous. just add await before those lines and you should get the expected results.... I actually JUST made a topic requesting the return type hint to be undated: Update setValue() return type hint?

2 Likes

I've added await, to the setValue, but I think that my biggest issue was that I had not awaited the first query.
I had

async function getTransactions(pageSize){
    GetTransactionsofPublicationID3.trigger({
      additionalScope: {
        currentPage: 0,
        pageSize: pageSize
      }
  })
}

Note the lack of await.
This should be

async function getTransactions(pageSize){
    await GetTransactionsofPublicationID3.trigger({
      additionalScope: {
        currentPage: 0,
        pageSize: pageSize
      }
  })
}

Thanks for your help with this.

2 Likes