Class: Scheduler

Scheduler

The Scheduler class implements a master for TimeEngine instances that implement the scheduled interface. The scheduled interface allows for synchronizing an engine to a monotonous time as it is provided by the given getTimeFunction.

The class is based on recursive calls to setTimeout and uses the time returned by the getTimeFunction passed as first argument as a logical time passed to the advanceTime methods of the scheduled engines or to the scheduled callback functions. It extends the SchedulingQueue class that itself includes a PriorityQueue to assure the order of the scheduled engines (see SimpleScheduler for a simplified scheduler implementation without PriorityQueue).

An object implementing the scheduled interface MUST implement the advanceTime method and CAN implement the resetTime method.

advanceTime(time :Number) -> {Number}

The advanceTime method has to be implemented by an TimeEngine as part of the scheduled interface. The method is called by the master (e.g. the scheduler). It generates an event and to returns the time of the next event (i.e. the next call of advanceTime). The returned time has to be greater than the time received as argument of the method. In case that a TimeEngine has to generate multiple events at the same time, the engine has to implement its own loop while(event.time <= time) and return the time of the next event (if any).

resetTime(time=undefined :Number)

The resetTime method is provided by the TimeEngine base class. An engine may call this method to reset its next event time (e.g. when a parameter is changed that influences the engine's temporal behavior). When no argument is given, the time is reset to the current master time. When calling the method with Infinity the engine is suspended without being removed from the master.

https://rawgit.com/wavesjs/waves-masters/master/examples/scheduler/index.html

Constructor

new Scheduler(getTimeFunction, optionsopt)

Parameters:
Name Type Attributes Default Description
getTimeFunction function

Function that must return a time in second.

options Object <optional>
{}

default options.

Properties
Name Type Attributes Default Description
period Number <optional>
0.025

period of the scheduler.

lookahead Number <optional>
0.1

lookahead of the scheduler.

currentTimeToAudioTimeFunction Number <optional>
t => t

convertion function from currentTime to audioTime. Defaults to identity function.

Source:
See:
Example
import { Scheduler } from 'waves-masters';

const getTime = () => new Date().getTime() / 1000;
const scheduler = new Scheduler(getTime);

const myEngine = {
  advanceTime(currentTime) {
    console.log(currentTime);
    // ask to be called in 1 second
    return time + 1;
  }
}

const startTime = Math.ceil(getTime());
scheduler.add(myEngine, startTime);

Members

audioTime :Number

Scheduler current audio time according to currentTime

Type:
  • Number
Source:

currentTime :Number

Scheduler current logical time.

Type:
  • Number
Source:

lookahead :Number

scheduler lookahead time (> period)

Type:
  • Number
Source:

period :Number

scheduler (setTimeout) period

Type:
  • Number
Source:

Methods

add(engine, timeopt)

An object implementing the scheduled interface (called engine) to the scheduler at an optionally given time.

The advanceTime method of the engine added to a scheduler will be called at the given time and with the given time as argument. The advanceTime method can return a new scheduling time (i.e. the next time when it will be called) or it can return Infinity to suspend scheduling without removing the function from the scheduler. A function that does not return a value (or returns null or 0) is removed from the scheduler and cannot be used as argument of the methods remove and resetEngineTime anymore.

Parameters:
Name Type Attributes Default Description
engine TimeEngine | function

Engine to add to the scheduler

time Number <optional>
this.currentTime

Engine start time

Source:

clear()

Remove all scheduled engines from the scheduler.

Source:

remove(engine, timeopt)

Remove an engine implementing the scheduled interface that has been added to the scheduler using the add method from the scheduler.

Parameters:
Name Type Attributes Default Description
engine TimeEngine

Engine to remove from the scheduler

time Number <optional>
this.currentTime

Schedule time

Source:

resetEngineTime(engine, time)

Reschedule a scheduled an engine implementing the scheduled interface at a given time.

Parameters:
Name Type Description
engine TimeEngine

Engine to reschedule

time Number

Schedule time

Source: