Jobs And Freeze In Netgem

Posted By : Shakil Pathan | 28-May-2014

Hi guys, In this blog, I'm going to explain you about jobs and freeze in netgem.
As javascript is mono-threaded, while your code is running, any event treatment, such as key will be postponed until end of script is reached. This may cause GUI freezes.
Just execute a long script for several seconds. If you press any key during this time, execution of key handling code will be paused until script finishes.Here we block during 10seconds.

for (var i=0; i<20; i++) {
	blockDuringInMs(500);
}


function blockDuringInMs(delay){
	for (var now = new Date(); new Date() - now < delay; );
	return true;
}

Create job object.
First parameter is name. Only usefull to identify it during debug.
For each part of the process, simply add a callback to the JSJob. They will be executed asynchronously.
Let's add a final job to notify end.
Callback must return true, else job execution will be stopped.

var job = new JSJob("Test");  
  
for (var i = 0; i < 20; i++) {  
    job.jobAdd(this.jobCallback.bind(this));  
}  
  
job.jobAdd(this.endOfStep2.bind(this)); 

Callback will receive as parameter:
Job
Callback must return:
false to abort job
true to execute next job.

MyFormWidget.prototype.jobCallback = function jobCallback(job)  
{  
    BlockDuringInMs(500);  
    return true;  
}  

Let's create a new job.
Array parsing can be easily done with a specific listAdd API.
The callback is called for each index on the array.
When the loop lasts too long, the job is reschedule.

var job = this.job = new JSJob("Test");  
  
job.listAdd(new Array(20), this.listCallback.bind(this));  
  
job.jobAdd(this.endOfStep4.bind(this)); 

Callback will receive as parameter:
Current index
Current object
Job
Callback must return:
false to abort job
true to keep array parsing
"abort" to abort array parsing and execute next job

MyFormWidget.prototype.listCallback = function listCallback(index, data, job)  
{  
    NGM.trace("Index = " + index);  
    BlockDuringInMs(500);  
    return true;  
}  

You can cancel/stop a job any time.

this.job.stop();  

Thanks,
Shakil

About Author

Author Image
Shakil Pathan

Shakil is an experienced Groovy and Grails developer . He has also worked extensively on developing STB applications using NetGem .

Request for Proposal

Name is required

Comment is required

Sending message..