X

The Visual Builder Cloud Service Blog

Abort Long Running REST Calls in Visual Builder

Shay Shmeltzer
Director of Product Management - Oracle

Sometime a REST call to the backend in your application could take too long, and you want to allow the user to cancel this operation mid-run. This is possible using an AbortController - a new interface that allows you to abort a web request (This interface is supported by most modern browsers). In the blog below I'll show how to use this technique in a Visual Builder application.

The process is quite straightforward and the artifacts are shown in the video below. The steps to achieve this consist of:

1. Adding an application variable that will track the AbortController - this will be a variable of type Any

2. Add two JS functions at the application's level:

//Method to create a new AbortController
  AppModule.prototype.createAbortController = function() {
    return new AbortController();
  };

//Method to abort a REST call
AppModule.prototype.abortRequests = function (abortController) {
  abortController.abort();
};

3. In the VBEnter event of the application, call the createAbortController JS function created above, then use an assign variable action to take the returned value from the function and assign it to your app level abortController variable.

4. When invoking the REST call using a call REST action, you'll need to add one more parameter to the action directly in the json file for the action. This will associate the signal to the abortController variable. See the bold line below:

        "callRestEndpoint1": {
          "module": "vb/action/builtin/restAction",
          "parameters": {
            "endpoint": "businessObjects/do_CalculateTotal_Employees",
            "signal": "{{ $application.variables.abortController.signal }}",
            "uriParams": {
              "Employees_Id": "1"
            },
            "contentType": "application/vnd.oracle.adf.action+json"
          },

This associates the abortController with this specific REST call.

5. Add a button that can cancel the call - and an action chain when it is pressed.

The action will call the abortRequests JS function passing the application level abortController variable as an input parameter to the function. This will abort the REST call that was associated with that controller in the previous step.

You also need to reset the value of the abortController variable. So call the same two steps you had in the vbEnter event creating a new controller and assigning it to the variable.

You should also handle this cancel operation there (for example showing a notification to the user and resetting anything that you need for "normal" flow).

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.