Class: Servant

Servant

Class that implements requestAnimationFrame based features to balance heavy workloads or handle time/frame based operations.

Members

staticServant.hasReqAnimFrameBoolean

Flag that indicates the requestAnimationFrame method exists.

staticServant.listArray.<ServantNode>

List of executing nodes.

Methods

staticServant.add(p_node, p_run_on_background)

Adds a Node to the execution pool.

Name Type Description
p_node ServantNode

Reference to a execution node.

p_run_on_background Boolean nullable

Flag that indicates the loop will keep running when the tab isn't focused.

Example

var duration = 3.0;
var n = null;
n = {
update: function() {
duration -= Time.delta; //Decrements the duration
if(duration<=0) Servant.remove(n);
}
};
Servant.add(n);

staticServant.clear()

Clears the execution list.

staticServant.delay(p_callback, p_delay, p_run_on_background){ServantUpdateNode}

Waits delay seconds and then Executes the callback.

Name Type Description
p_callback String | ServantUpdateCallback

Reference to a function that will handle each update or String path compatible with Suit notifications.

p_delay Number nullable

Delay in seconds. Defaults to 0.0.

p_run_on_background Boolean nullable

Flag that indicates the loop will keep running when the tab isn't focused.

Returns:
Type Description
ServantUpdateNode
  • The created execution node.
Example

//Using 'function'
//Waits 3s and calls the function with the argument list.
Servant.delay(function(a,b){
console.log(a+" "+b);
},3,["Hello","world"]);

//Using SuitJS

var c = {
on: function(n) {
switch(n.path) {
case "servant-callback@complete": console.log(n.data[0]+" "+n.data[1]); break;
}
}
};

SuitJS.controller.add(c);

Servant.delay("servant-callback",3,["Hello","world"]);

staticServant.iterate(p_callback, p_list, p_step, p_timeout, p_run_on_background){ServantUpdateNode}

Iterates a list in a thread-like routine.

Name Type Description
p_callback ServantIterationCallback

Reference to a function that will handle each update or String path compatible with Suit notifications.

p_list Array.<Object>

List of objects.

p_step Number nullable

Iterations per frame. Defaults to 1.

p_timeout Number nullable

Timeout in seconds. Stops the execution after some time. Defaults to 0xffffff(infinite).

p_run_on_background Boolean nullable

Flag that indicates the loop will keep running when the tab isn't focused.

Returns:
Type Description
ServantUpdateNode
  • The created execution node.
Example

var list = [1,2,3,4,5,6];
//Will handle 1 element per frame.
//See the time stamp per iteration
Servant.iterate(function (it,i,len) {
console.log(i+"> "+it+" @ "+Time.elapsed);
},list,1);

staticServant.remove(p_node)

Removes a Node of the execution pool.

Name Type Description
p_node ServantNode

Reference to a execution node.

See:

staticServant.run(p_callback, p_duration, p_delay, p_run_on_background){ServantUpdateNode}

Continuously Executes a callback waiting delay and during duration in seconds.

Name Type Description
p_callback String | ServantUpdateCallback

Reference to a function that will handle each update or String path compatible with Suit notifications.

p_duration Number nullable

Duration in seconds. Defaults to 0xffffff (infinite).

p_delay Number nullable

Delay in seconds. Defaults to 0.0.

p_run_on_background Boolean nullable

Flag that indicates the loop will keep running when the tab isn't focused.

Returns:
Type Description
ServantUpdateNode
  • The created execution node.
Example

//Using 'function'.
Servant.run(function(node){
if(node.elapsed >= 3.0) console.log("Time Out!");
}3);

//Using SuitJS (don't forget to add 'suitjs.js' on your page)

var c = {
on: function(n) {
switch(n.path) {
case "servant-callback@update": console.log(n.data.elapsed); break;
}
}
};

SuitJS.controller.add(c);

Servant.run("servant-callback",3);

staticServant.set(p_target, p_property, p_value, p_delay, p_run_on_background){ServantUpdateNode}

Waits delay seconds and then sets the target property with value.

Name Type Description
p_target Object

Object to be modified.

p_property String

Property of the target.

p_value Object

Value to be set.

p_delay Number nullable

Delay in seconds. Defaults to 0.0.

p_run_on_background Boolean nullable

Flag that indicates the loop will keep running when the tab isn't focused.

Returns:
Type Description
ServantUpdateNode
  • The created execution node.
Example

var o = {count: 0};
Servant.set(o,"count",10,3); //Sets 'o.count' to 10 after 3s

staticServant.start()

Start all loops.

staticServant.stop()

Stop all loops. Call Servant.start() to activate them again.

comments powered by Disqus