A few helpful js client changes

8 views
Skip to first unread message

Evan Leonard

unread,
May 21, 2008, 10:16:46 AM5/21/08
to jabsor...@googlegroups.com
I all I have a few accumulated changes that I've made to the js client that I wanted to share with the community:

1) First is, when I was debugging some memory leaks in ie6 I found it really helpful to set the http_max_spare parameter to zero. This linearize the calls and look at one xmlrpc object at a time. Here's the line I changed to do this:

JSONRpcClient.http_max_spare = 0;  // changing the pool size to 0.

2) Session timeouts were a problem we struggled with. We decided to handle them this way in json-rpc-java

JSONRpcClient.toplevel_ex_handler = function (ex) {
    if(ex instanceof JSONRpcClient.Exception)
    {
        // assuming this is because of a session timeout.  navigate to current location,
        // and the auth filter should let them log in and get back to whatever page we're on.
        if(ex.code == 550) //550 happens when the session has timed out
        {
            setTimeout(function () {navigate(document.location)}, 300);
            return;
        }
    }
    if(ex)
        throw ex.message.substring(ex.name.length + 2); //ideally this would throw a complex exception object
    else
        throw "Error making rpc request to server";
};

3) In our templated pages its possible that we need an instance of the js rpc client in many unrelated templates. We didn't want to have to instantiate separate JSONRpcClient objects and make a separate getMethods call back to the server for each template so we used the js singleton patter to solve it this way:

var RpcClient = function()
{
    var instance = null;

    return function()
    {
        if(instance == null)
            instance = new JSONRpcClient(CONTEXT_PATH + "/RPC", null, null, null);

        return instance;
    };
}(); //<---- The outer function gets evaluated here, so all new's refer to the same var instance.

Now we can just make new RpcClients where ever we like and they will always return the same instance:

var a = new RpcClient();
var b = new RpcClient();

if(a !== b) throw "Oh noes! Not same instance"

This code will run without a problem. Also, this has the nice side benefit of encapsulating our remote RPC URL inside the RpcClient constructor so it can be changed easily in one location.

I hope some or all of these are helpful.

All the best,
Evan Leonard


raziel alvarez

unread,
May 21, 2008, 10:32:32 AM5/21/08
to jabsor...@googlegroups.com
Related to the memory leaks issue, in our code we "free" the JSONRpcClient when the interface  is destroyed. Code looks like:

    // 4) Clean up the JSONRpcClient
    if (typeof JSONRpcClient != 'undefined') {
      for (var i=0;i<JSONRpcClient.http_active.length;i++) {
        var http = JSONRpcClient.http_active[i];
        http.onreadystatechange = null_function;
        http.abort();
        delete JSONRpcClient.http_active[i];
      }
      for (var i=0;i<JSONRpcClient.http_spare.length;i++) {
        var http = JSONRpcClient.http_spare[i];
        http.onreadystatechange = null_function;
        http.abort();
        delete JSONRpcClient.http_spare[i];
      }

      for (var props in JSONRpcClient) {
        var p = JSONRpcClient[props];
        if (p == null) continue;
        if (p.constructor == Array) {
          p.length = 0;
        } else if (p.constructor == Object) {
          for (var props2 in p) {
            delete p[props2];
          }
        }
        delete JSONRpcClient[props];
      }
    }

- Raziel

William Becker

unread,
Jun 18, 2008, 7:37:00 AM6/18/08
to jabsor...@googlegroups.com
Thanks for these suggestions. I am not sure they can be added in to the code, but I have create a page on the wiki that mentions these. If you want personal credit for these I can put this on too.

Hopefully we can get a goodly sized repository of these things that will help users of this tool.

Cheers,
Will

evan.leonard

unread,
Jun 19, 2008, 8:41:17 AM6/19/08
to jabsorb-user

Sounds good. No personal credit needed.
I'm glad they may be useful.

Best,
Evan


On Jun 18, 7:37 am, "William Becker" <wbec...@gmail.com> wrote:
> Thanks for these suggestions. I am not sure they can be added in to the
> code, but I have create a page on the wiki <http://jabsorb.org/Tips> that
> mentions these. If you want personal credit for these I can put this on too.
>
> Hopefully we can get a goodly sized repository of these things that will
> help users of this tool.
>
> Cheers,
> Will
>

raziel alvarez

unread,
Jun 19, 2008, 9:27:27 AM6/19/08
to jabsor...@googlegroups.com
Seems good to me too. Hope it helps others.
Reply all
Reply to author
Forward
0 new messages