This project has moved. For the latest updates, please go here.

implementing callbacks like setTimeout

Feb 22, 2012 at 6:30 AM

We are trying to use Jurassic to run our unit tests as part of our build process.

We would like to be able to support some browser functionality like window.setTimeout as objects in our space.

We have it so that it succesfully calls back to a C# object we have written, but we need a way of passing a javascript callback to the setTimer function that we call back when the appropriaate amount of time has changed.

Our current code likes something like this:

        [JSFunction]
        public int setTimeout(ObjectInstance callback, int millisec, string lang = null)
        {
            Timer timer = new Timer(obj => this.Engine.CallGlobalFunction(callback), callback, millisec, Timeout.Infinite);
            return id;
        }
 Only problem is that the callback which is passed in comes in as a string which is the serialized form of the entire function.



Is there a recommended way of accomplishing this?

Thanks

Feb 22, 2012 at 9:30 AM
Edited Feb 22, 2012 at 9:32 AM

You can take a look at my implementation if you want to. The commented out instances are the ones that invoke strings.

Coordinator
Feb 22, 2012 at 10:37 AM

Instead of using CallGlobalFunction, check if the the callback is of type FunctionInstance - if it is, invoke the Call() method on it.

Feb 22, 2012 at 9:25 PM

That did the trick!  Thanks for the speedy response and really useful library!