Provides a set of methods available to all scheduler instances.
The follow example shows the basic usage of an Scheduler.
const AsyncScheduler = require('rx.schedulers').AsyncScheduler;
const scheduler = new AsyncScheduler();
const disposable = scheduler.schedule(
'world',
function (scheduler, x) { console.log(`hello ${x}`); }
);
// => hello worldReturns a scheduler that wraps the original scheduler, adding exception handling for scheduled actions.
handlerFunction: Handler that's run if an exception is caught. The error will be rethrown if the handler returnsfalse.
Scheduler: Wrapper around the original scheduler, enforcing exception handling.
function SchedulerError(message) {
this.message = message;
Error.call(this);
}
SchedulerError.prototype = Object.create(Error.prototype);
const AsyncScheduler = require('rx.schedulers').AsyncScheduler;
const scheduler = new AsyncScheduler();
const catchScheduler = scheduler.catch(function (e) {
return e instanceof SchedulerError;
});
// Throws no exception
const d1 = catchScheduler.schedule(null, function () {
throw new SchedulerError('woops');
});Gets the current time according to the Scheduler implementation.
Number: The current time according to the Scheduler implementation.
const AsyncScheduler = require('rx.schedulers').AsyncScheduler;
const scheduler = new AsyncScheduler();
const now = scheduler.now();
console.log(now);
// => 1381806323143Schedules an action to be executed with state.
state: Any: State passed to the action to be executed.action:Function: Action to execute with the following arguments:scheduler:Scheduler- The current Schedulerstate:Any- The current state
Disposable: The disposable object used to cancel the scheduled action (best effort).
const SyncScheduler = require('rx.schedulers').SyncScheduler;
const scheduler = new SyncScheduler();
const disposable = scheduler.schedule('world', function (scheduler, x) {
console.log(`hello ${x}`);
});
// => hello world
// Tries to cancel but too late since it is immediate
disposable.dispose();Schedules an action to be executed at the specified relative due time. Note this only works with the built-in scheduler scheduler, as the rest will throw an exception as the framework does not allow for blocking.
stateAny: State passed to the action to be executed.dueTimeNumber|Date: Relative or absolute time at which to execute the action.action:Function: Action to execute with the following arguments:scheduler:Scheduler- The current Schedulerstate:Any- The current state
Disposable: The disposable object used to cancel the scheduled action (best effort).
const AsyncScheduler = require('rx.schedulers').AsyncScheduler;
const scheduler = new AsyncScheduler();
/* Relative schedule */
const disposable1 = scheduler.scheduleFuture(
'world',
5000, /* 5 seconds in the future */
function (scheduler, x) {
console.log(`hello ${x} after 5 seconds`);
}
);
// => hello world after 5 seconds
/* Absolute schedule */
const disposable2 = scheduler.scheduleFuture(
'world',
new Date(Date.now() + 5000), /* 5 seconds in the future */
(scheduler, x) => {
console.log(`hello ${x} after 5 seconds`);
}
);
// => hello world after 5 secondsSchedules an action to be executed with state.
stateAny: State passed to the action to be executed.action:Function: Action to execute with the following parameters:state:Any- The state passed inrecurse:Function- The action to execute for recursive actions which takes the form ofrecurse(newState)where the new state is passed to be executed again.
Disposable: The disposable object used to cancel the scheduled action (best effort).
const AsyncScheduler = require('rx.schedulers').AsyncScheduler;
const scheduler = new AsyncScheduler();
const disposable = scheduler.scheduleRecursive(
0,
(i, recurse) => {
console.log(i); if (++i < 3) { recurse(i); }
}
);
// => 0
// => 1
// => 2Schedules an action to be executed recursively at a specified absolute or relative due time. Note this only works with the built-in Scheduler.timeout scheduler, as the rest will throw an exception as the framework does not allow for blocking.
stateAny: State passed to the action to be executed.dueTimeNumber: Absolute time at which to execute the action for the first time.action:Function: Action to execute with the following parameters:state:Any- The state passed inrecurse:Function- The action to execute for recursive actions which takes the form ofrecurse(newState, dueTime).
Disposable: The disposable object used to cancel the scheduled action (best effort).
const AsyncScheduler = require('rx.schedulers').AsyncScheduler;
const scheduler = new AsyncScheduler();
/* Absolute recursive future */
const disposable1 = scheduler.scheduleRecursiveFuture(
0,
new Date(Date.now() + 5000), /* 5 seconds in the future */
(i, self) => {
console.log(i);
if (++i < 3) {
// Schedule mutliplied by a second by position
self(i, new Date(Date.now() + (i * 1000)));
}
}
);
// => 0
// => 1
// => 2
/* Relative recursive future */
const disposable2 = scheduler.scheduleRecursiveFuture(
0,
5000, /* 5 seconds in the future */
(i, self) => {
console.log(i);
if (++i < 3) {
// Schedule mutliplied by a second by position
self(i, i * 1000);
}
}
);
// => 0
// => 1
// => 2Schedules a periodic piece of work by dynamically discovering the scheduler's capabilities. The periodic task will be scheduled using window.setInterval for the base implementation.
stateAny: State passed to the action to be executed.periodNumber: Period for running the work periodically in ms.action:Function: Action to execute with the following parameters. Note that the return value from this function becomes the state in the next execution of the action.state:Any- The state passed in
Disposable: The disposable object used to cancel the scheduled action (best effort).
const AsyncScheduler = require('rx.schedulers').AsyncScheduler;
const scheduler = new AsyncScheduler();
const disposable = scheduler.schedulePeriodic(
0,
1000, /* 1 second */
(i) => {
console.log(i);
// After three times, dispose
if (++i > 3) { disposable.dispose(); }
return i;
});
// => 0
// => 1
// => 2
// => 3Normalizes the specified time span value to a positive value.
timeSpanNumber: The time span value to normalize.
Number: The specified time span value if it is zero or positive; otherwise, 0
const Scheduler = require('rx.schedulers').Scheduler;
const r1 = Scheduler.normalize(-1);
console.log(r1);
// => 0
const r2 = Scheduler.normalize(255);
console.log(r2);
// => 255Determines whether the given object is a Scheduler instance
objAny: The object to determine whether it is aSchedulerinstance
Boolean: Whether the given object is a Scheduler.
const AsyncScheduler = require('rx.schedulers').AsyncScheduler;
const scheduler = new AsyncScheduler();
const Scheduler = require('rx.schedulers').Scheduler;
const isScheduler = Scheduler.isScheduler(scheduler);
console.log(`Is scheduler? ${Scheduler.isScheduler(scheduler)}`);
// Is scheduler? true