Base class for providing scheduling in virtual time. This inherits from the Rx.Scheduler
class.
The following shows an example of using the Rx.VirtualTimeScheduler
. In order for this to work, you must implement the add
, toDateTimeOffset
and toRelative
methods as described below.
/* Comparer required for scheduling priority */
function comparer (x, y) {
if (x > y) { return 1; }
if (x < y) { return -1; }
return 0;
}
var scheduler = new Rx.VirtualTimeScheduler(0, comparer);
/**
* Adds a relative time value to an absolute time value.
* @param {Any} absolute Absolute virtual time value.
* @param {Any} relative Relative virtual time value to add.
* @return {Any} Resulting absolute virtual time sum value.
*/
scheduler.add = function (absolute, relative) {
return absolute + relative;
};
/**
* Converts an absolute time to a number
* @param {Number} The absolute time in ms
* @returns {Number} The absolute time in ms
*/
scheduler.toDateTimeOffset = function (absolute) {
return new Date(absolute).getTime();
};
/**
* Converts the time span number/Date to a relative virtual time value.
* @param {Number} timeSpan TimeSpan value to convert.
* @return {Number} Corresponding relative virtual time value.
*/
scheduler.toRelative = function (timeSpan) {
return timeSpan;
};
// Schedule some time
scheduler.scheduleAbsolute(1, function () { console.log('foo'); });
scheduler.scheduleAbsolute(2, function () { console.log('bar'); });
scheduler.scheduleAbsolute(3, function () { scheduler.stop(); });
// Start the scheduler
scheduler.start();
// => foo
// => bar
// Check the clock once stopped
console.log(scheduler.now());
// => 3
console.log(scheduler.clock);
// => 3
- rx.virtualtime.js
advanceBy
advanceTo
scheduleAbsolute
scheduleAbsoluteWithState
scheduleRelative
scheduleRelativeWithState
sleep
start
stop
Creates a new virtual time scheduler with the specified initial clock value and absolute time comparer.
initialClock
(Function): Initial value for the clock.comparer
(Function): Comparer to determine causality of events based on absolute time.
function comparer (x, y) {
if (x > y) { return 1; }
if (x < y) { return -1; }
return 0;
}
var scheduler = new Rx.VirtualTimeScheduler(
0, /* initial clock of 0 */
comparer /* comparer for determining order */
);
- rx.virtualtime.js
Advances the scheduler's clock by the specified relative time, running all work scheduled for that timespan.
time
(Any): Relative time to advance the scheduler's clock by.
var scheduler = new MyVirtualScheduler(
200 /* initial time */
);
scheduler.scheduleAbsolute(250, function () {
console.log('hello');
});
scheduler.advanceBy(300);
// => hello
console.log(scheduler.clock);
// => 500
- rx.virtualtime.js
Advances the scheduler's clock to the specified time, running all work till that point.
time
(Any): Absolute time to advance the scheduler's clock to.
var scheduler = new MyVirtualScheduler(
0 /* initial time */
);
scheduler.scheduleAbsolute(100, function () {
console.log('hello');
});
scheduler.scheduleAbsolute(200, function () {
console.log('world');
});
scheduler.advanceBy(300);
// => hello
// => world
console.log(scheduler.clock);
// => 300
- rx.virtualtime.js
Schedules an action to be executed at dueTime.
dueTime
(Any): Absolute time at which to execute the action.action
(Function): Action to be executed.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var scheduler = new MyVirtualScheduler(
0 /* initial time */
);
scheduler.scheduleAbsolute(100, function () {
console.log('hello');
});
scheduler.scheduleAbsolute(200, function () {
console.log('world');
});
scheduler.advanceBy(300);
// => hello
// => world
console.log(scheduler.clock);
// => 300
- rx.virtualtime.js
Schedules an action to be executed at dueTime.
state
: (Any): State passed to the action to be executed.dueTime
(Any): Absolute time at which to execute the action.action
(Function): Action to be executed.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var scheduler = new MyVirtualScheduler(
0 /* initial time */
);
scheduler.scheduleAbsoluteWithState('world', 100, function (x) {
console.log('hello ' + x);
});
scheduler.scheduleAbsoluteWithState(200, function () {
console.log('goodnight ' + x);
}, 'moon');
scheduler.start();
// => hello world
// => goodnight moon
console.log(scheduler.clock);
// => 200
- rx.virtualtime.js
Schedules an action to be executed at dueTime.
dueTime
(Any): Relative time after which to execute the action.action
(Function): Action to be executed.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var scheduler = new MyVirtualScheduler(
100 /* initial time */
);
scheduler.scheduleRelative(100, function () {
console.log('hello');
});
scheduler.scheduleRelative(200, function () {
console.log('world');
});
scheduler.start();
// => hello
// => world
console.log(scheduler.clock);
// => 400
- rx.virtualtime.js
Schedules an action to be executed at dueTime.
state
: (Any): State passed to the action to be executed.dueTime
(Any): Relative time after which to execute the action.action
(Function): Action to be executed.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var scheduler = new MyVirtualScheduler(
0 /* initial time */
);
scheduler.scheduleRelativeWithState('world', 100, function (x) {
console.log('hello ' + x);
});
scheduler.scheduleRelativeWithState('moon', 200, function () {
console.log('goodnight ' + x);
});
scheduler.start();
// => hello world
// => goodnight moon
console.log(scheduler.clock);
// => 300
- rx.virtualtime.js
Advances the scheduler's clock by the specified relative time.
time
(Any): Relative time to advance the scheduler's clock by.
var scheduler = new MyVirtualScheduler(
0 /* initial time */
);
scheduler.sleep(400);
console.log(scheduler.clock);
// => 400
- rx.virtualtime.js
Starts the virtual time scheduler.
var scheduler = new MyVirtualScheduler(
0 /* initial time */
);
scheduler.scheduleRelativeWithState('world', 100, function (x) {
console.log('hello ' + x);
});
scheduler.scheduleRelativeWithState('moon', 200, function () {
console.log('goodnight ' + x);
});
scheduler.start();
// => hello world
// => goodnight moon
console.log(scheduler.clock);
// => 400
- rx.virtualtime.js
Stops the virtual time scheduler.
var scheduler = new MyVirtualScheduler(
0 /* initial time */
);
scheduler.scheduleRelative(100, function () {
console.log('hello world');
});
scheduler.scheduleRelative(100, function () {
scheduler.stop();
});
scheduler.scheduleRelative(100, function () {
console.log('hello world');
});
scheduler.start();
// => hello world
- rx.virtualtime.js
Adds a relative time value to an absolute time value. This method is used in several methods including scheduleRelativeWithState
, advanceBy
and sleep
.
absolute
(Any): Absolute virtual time value.relative
(Any): Relative virtual time value.
(Any): Resulting absolute virtual time sum value.
One possible implementation could be as simple as the following:
scheduler.add = function (absolute, relative) {
return absolute + relative;
};
- rx.virtualtime.js
Converts an absolute time to a number. This is used directly in the now
method on the Rx.Scheduler
absolute
(Any): The absolute time to convert.
(Number): The absolute time in ms.
One possible implementation could be as simple as the following:
// String -> Number
scheduler.toDateTimeOffset = function (absolute) {
return return absolute.length;
};
- rx.virtualtime.js
Converts the time span number/Date to a relative virtual time value.
timeSpan
(Any): The time span number value to convert. This is used directly inscheduleWithRelativeAndState
andscheduleWithAbsoluteAndState
.
(Number): Corresponding relative virtual time value.
One possible implementation could be as simple as the following:
// Number -> Number
scheduler.toRelative = function (timeSpan) {
return timeSpan;
};
- rx.virtualtime.js