0

When working with XPages' server-side JavaScript (SSJS), I miss the timing/scheduling functions setTimeout, setInterval, clearTimeout and clearInterval.

Is there, or can someone provide, a polyfill for these essential functions in SSJS?

xpages-noob
  • 1,569
  • 1
  • 10
  • 37

1 Answers1

1

SSJS is just a string which is invoked when a method is called. There is no "timing" functionality available because the SSJS engine is just "executed" during the request. When the request is finished, the SSJS code must be completed.

When using these functions in the browser or a runtime like node.js, your code is able to run "in the background", because the runtime is still "running" if a request is completed.

Just think about the following: To stop an setTimeout, you must use clearInterval. This requires a variable holding the reference to the timer. How do you store this variable in SSJS?

EDIT:

You could change the DemoExecutor class to get a handle to the SSJS Interpreter.

class DemoCallable implements Callable {
        private final NSFComponentModule module;
        private transient JavaScriptInterpreter jsInterpreter;
        private transient FBSGlobalObject globalObject;

        public DemoCallable(NSFComponentModule module, JavaScriptInterpreter  jsInterpreter, FBSGlobalObject globalObject ) {
            this.module = module;
            this.jsInterpreter = jsInterpreter;
            this.globalObject = globalObject;
        }

       ...

}

This gives you access to the Interpreter and the global JS Object.

You can get the objects from view root:

facesContext.getViewRoot().getGlobalObject()
facesContext.getViewRoot().getJSInterpreter()
Sven Hasselbach
  • 10,455
  • 1
  • 18
  • 26
  • Thank you for taking your time writing this explanation. I am aware of how SSJS works, but nonetheless the given functions are essential to me. For years, I have been using the following approach as a workaround: (1) I store the timed function with a unique key in the applicationScope. (2) I create a runnable task which calls an XPage that executes the timed function by key. (3) I schedule the task with a java.util.concurrent.ScheduledExecutorService. This approach works, but obviously consumes a lot of resources. Hence my question about a proper polyfill. – xpages-noob Dec 07 '18 at 08:45
  • PS: I also tried creating a runnable task which uses XPages' ExpressionEvaluator and ValueBindungs to directly run SSJS, but as it is running in a separate thread, I am missing the NotesContext there ("NotesContext is not initialized..."). Do you happen to know a solution to this? Using this approach would IMO considerably reduce the resource consumption of my current polyfill... – xpages-noob Dec 07 '18 at 08:55
  • 1
    Have a look at the XPagesExecutorService: http://hasselba.ch/blog/?p=2180 – Sven Hasselbach Dec 07 '18 at 10:06
  • Thanks for the advice - I'll have a look at it. – xpages-noob Dec 07 '18 at 10:32
  • Sorry for bothering you again. Do you know how to evaluate a SSJS expression using the NotesContext or the NotesSession in Java? – xpages-noob Dec 07 '18 at 12:11
  • @xpages-noob: Updated my answer. – Sven Hasselbach Dec 14 '18 at 08:23
  • Thanks for the additional information. I wonder that above code works for you as it does throw a "weird" exception in my (relatively fresh) Domino version (9.0.1 FP7): `Can't convert com.ibm.jscript.types.FBSGlobalObject to com.ibm.jscript.types.FBSGlobalObject` . However, you definitely pointed me into the right direction and I'm sure I can handle it from here onwards. Thanks again for your help. – xpages-noob Dec 14 '18 at 11:52