Joe Walker | 16 May 18:14

Serializing closures


In Javascript:

function delayedUpdater(message) {
  dwr.util.setValue("id", message);
}

dwr.engine.setActiveReverseAjax(true);
Remote.method(delayedUpdater, 4000 /* No callback */);


And on the server:

public class Remote {
  public void method(JavascriptFunction delayedUpdater, int delay) {
    // Fire off a new thread to do some work
    Thread work = new Thread() {
      public void run() {
        // Loop forever pinging the client
        int i = 0;
        while (true) {
          Thread.currentThread().sleep(delay);
          delayedUpdater.execute("Hello, World: " + (i++));
        }
      }
    }.start();
  }
}


In short: how about we create a new type: org.directwebremoting.io.JavascriptFunction, with associated converter logic so we can pass a function to the server for execution.

I was just doing some work on the dwr data store when I found myself needing something like this, and it seems like it could be generally useful.
Thoughts?

Joe.



Gmane