Calling Node.js methods
Create JavaScript Worker object
Create a new object variable and inherit from the JavaScript Worker, grouped under the OW3 Worker Objects. For more control, you might prefer to create a new object class in your library and inherit from the JavaScript worker through the $superclass
property.
Initialise and start the worker
Once you have an object or an object reference to the JavaScript Worker, you need initialise and then start the worker:
Do jsWorker.$init()
Do jsWorker.$start() Returns #F
A new thread will be launched upon calling $start
where the Node.js process will wait until we give it methods to execute. Furthermore, you only need to call $start
as you can make multiple method calls to the same worker.
Call a method
If your JavaScript worker has started successfully, we can use $callmethod(cModule, cMethod)
where cModule is the name of the module we wish to use and cMethod is the name of the method inside our module we want to execute:
Do jsWorker.$callmethod('omnis_test', 'test') Returns #F
Passing parameters
Parameters passed to the JavaScript worker are in the form of a list or row.
You can pass the list or row in the $callmethod
:
Do lRow.$cols.$add('message',kCharacter,kSimplechar)
Do lRow.$assigncols('hello world')
Do jsWorker.$callmethod("omnis_test","test",lRow) Returns #F
In order to access parameters on the JavaScript module side, use the param
parameter of your method:
const methodMap = {
test: function (param) {
console.log(`Message is ${param.message}`);
// ...
},
};
Method results
When the JavaScript worker returns, the $methodreturn
callback is called with a row parameter.
Inside the row parameter, you can find the returned data from your method as well as any tags supplied to $callmethod
.
For example, calling the test method in the omnis_test module will result in Unicode characters to be returned inside the unicode column in the first row parameter of $methodreturn
.
Cancel call
If you have called a Node.js method but wish to cancel it, use the $cancel
method on the JavaScript worker:
Do jsWorker.$cancel()
Cancelling a call might be useful if you think it has been running for too long and wish to stop it.
Waiting on a call
The $callmethod
is asynchronous by default, but there might be times where you wish to block the main thread until the JavaScript worker finishes its job, in which case you can pass kTrue
to the bWait parameter of $callmethod
(4th parameter):
Do jsWorker.$callmethod('omnis_test','test',,kTrue) Returns #F
When bWait is true, the JavaScript worker will block the main thread until it returns.
Tagging method calls
The same JavaScript worker instance can be used for multiple method calls, as such it may come in useful the ability of tagging each call with an identifiable number.
The 6th parameter of $callmethod
can accept a tag which will be returned in the $methodreturn
:
Do jsWorker.$callmethod('omnis_test','test',,,,lTagID) Returns #F
A __tag column is added to the row parameter of $methodreturn
if a tag was given to $callmethod
, allowing you to identify the results.