0

I'm testing a component where I upload a file, convert to base64 and split into several CHUNK Parts send each one to an apex method.

This component seems quite common online. Like:

Bellow, is the method that performs my invocation to apex

uploadInChunk: function(component, file, fileContents, startPosition, endPosition, actualPart, totalParts) {

    var filePart= fileContents.substring(startPosition, endPosition);

    var action = component.get("c.uploadDocument");
    action.setParams({
       actualPart: String(actualPart),
       totalParts : String(totalParts),
       recordID : component.get('v.recordID'),
       part : filePart
    });

    action.setCallback(this, function(response) {

        var state = response.getState();
        if (state === "SUCCESS") {
                startPosition = endPosition;
                endPosition = Math.min(fileContents.length, startPosition + this.CHUNK_SIZE);
                                    
                if (startPosition < endPosition) {
                    this.uploadInChunk(component, file, fileContents, startPosition,endPosition,(actualPart+1),totalParts);
                } else {
                    this.showToast(component,$A.get("$Label.c.uploading"), 'Info');
                }
         } else if (state === "INCOMPLETE") {
            this.showToast(component, $A.get("$Label.c.Fail"), 'Error');
        } else if (state === "ERROR") {
            this.showToast(component, Error, 'Error');
            var errors = response.getError();
            if (errors) {
                if (errors[0] && errors[0].message) {
                    console.log("Error message: " + errors[0].message);
                    component.set("v.showLoadingSpinner", false);
                }
            } else {
                console.log("Unknown error");
            }
        }
    });
$A.enqueueAction(action);

My issue is about the perfomance.

The apex method only invokes a future method so the transaction is fast, however the callback to javascript takes a few more seconds. When I have 30/40 parts it delays the whole process to some minutes.

I realized that the filePart, being a very large String, passing as an input is the most delaying cause. (Commenting that input turns the callbacks much more faster :P )

I found something about promises and I still try to perform the invocation outside the setCallback, since I really don't need the response. But I couldn't manage that.

Can you help me on that?

2
  • 3
    Does this answer your question? How to turn boxcarring OFF for LWC imperative apex method calls?
    – Phil W
    Commented Jun 24, 2021 at 22:14
  • @PhilW thanks but not really. I tried action.setBackground() before my $A.enqueueAction(action);. It takes about 5 minutes for a 20MB upload (about 27 executions) Another test I perform was (cacheable=true) but it was the same Commented Jun 25, 2021 at 14:02

0

You must log in to answer this question.