We are seeing a surprising scenario when we are on a slow network connection and our calls to the WL Server time out.

This happens at WL.Client.connect as well as on invokeProcedure:

  • we execute the call with a timeout of 10 seconds
  • the network connection is slow so the call times out
  • the defined onFailure procedure associated to that call is executed
  • the WL Server responds with a valid response after the timeout
  • the onSuccess procedure associated to that call is executed

Is this the designed and intended behavior of the WL Client Framework? Is this specified in the InfoCenter documentation or somewhere?

All developers in our team expected these two procedures to be exclusive and our code was implemented based on this assumption. We are now investigating options on how to match a timed-out/failed response to a success response to make sure we achieve an exclusive execution of onFailure or onSuccess code/logic in our app.

Note: we did not test that with connectOnStartup=true and since the initOptions does not provide an onSuccess procedure (since WL handles that internally) it might be even harder to implement an exclusive execution in this case.

1

There are 1 best solutions below

0
On BEST ANSWER

That seems like expected behavior, but don't quote me on that.

You can get the behavior you want (only call the failure callback when it fails, and only call the success callback when it succeeds) using jQuery.Deferreds. There are ways of creating these deferred objects with dojo and other libraries. But, I just tested with jQuery's implementation, which is shipped with every version of IBM Worklight.

$(function () {

var WL = {};
WL.Client = {};
WL.Client.invokeProcedureMock = function (options) {
  options.onFailure('failure');
  options.onSuccess('success');
};

var dfd = $.Deferred();

var options = {
  onSuccess: dfd.resolve,
  onFailure: dfd.reject
};

WL.Client.invokeProcedureMock(options);

dfd
  .done(function (msg) {
    // handle invokeProcedure success
    console.log(msg);
  })
  .fail(function (msg) {
    //handle invokeProcedure failure
    console.log(msg);
  });

});

I put the code above in a JSFiddle, notice that even if I call the onSuccess callback, it won't have any effect because I already called the failure callback (which rejected the deferred). You would add your application logic inside the .done or .fail blocks.

This is just a suggestion, there are likely many ways to approach your issue.