Provides a set of static methods to access commonly used schedulers and a base class for all schedulers.
The follow example shows the basic usage of an Rx.Scheduler
.
var disposable = Rx.Scheduler.default.scheduleWithState(
'world',
function (scheduler, x) {
console.log('hello ' + x);
}
);
// => hello world
- rx.js
schedule
scheduleWithState
scheduleWithAbsolute
scheduleWithAbsoluteAndState
scheduleWithRelative
scheduleWithRelativeAndState
scheduleRecursive
scheduleRecursiveWithState
scheduleRecursiveWithAbsolute
scheduleRecursiveWithAbsoluteAndState
scheduleRecursiveWithRelative
scheduleRecursiveWithRelativeAndState
currentThread
immediate
default
timeout
DEPRECATED
Initializes a new instance of the Rx.Scheduler
. This is meant for Scheduler implementers and not normal usage.
now
(Function): Function which gets the current time according to the local machine's system clock.schedule
(Function): Function to schedule an action immediately.scheduleRelative
(Function): Function used to schedule an action in relative time.scheduleAbsolute
(Function): Function used to schedule an action in absolute time.
// Used for scheduling immediately
function schedule(state, action) {
var scheduler = this, disposable = new Rx.SingleAssignmentDisposable();
var id = setTimeout(function () {
if (!disposable.isDisposed) {
disposable.setDisposable(action(scheduler, state));
}
}, 0);
return new Rx.CompositeDisposable(disposable, Rx.Disposable.create(function () {
clearMethod(id);
}));
}
// Used for scheduling relative to now
function scheduleRelative(state, dueTime, action) {
var scheduler = this,
dt = Rx.Scheduler.normalize(dueTime);
// Shortcut if already 0
if (dt === 0) {
return scheduler.scheduleWithState(state, action);
}
var disposable = new Rx.SingleAssignmentDisposable();
var id = window.setTimeout(function () {
if (!disposable.isDisposed) {
disposable.setDisposable(action(scheduler, state));
}
}, dt);
return new Rx.CompositeDisposable(disposable, Rx.Disposable.create(function () {
window.clearTimeout(id);
}));
}
// Used for scheduling in absolute time
function scheduleAbsolute(state, dueTime, action) {
return this.scheduleWithRelativeAndState(state, dueTime - this.now(), action);
}
var timeoutScheduler = new Rx.Scheduler(
Date.now,
schedule,
scheduleRelative,
scheduleAbsolute
);
var handle = timeoutScheduler.schedule(function () {
console.log('hello');
});
// => hello
- rx.js
Returns a scheduler that wraps the original scheduler, adding exception handling for scheduled actions. There is an alias called catchError
for browsers < IE9.
handler
(Function): Handler that's run if an exception is caught. The exception will be rethrown if the handler returnsfalse
.
(Scheduler): Wrapper around the original scheduler, enforcing exception handling.
function inherits (ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
}
inherits (SchedulerError, Error);
function SchedulerError(message) {
Error.call(this, message);
}
var scheduler = Rx.Scheduler.timeout;
var catchScheduler = scheduler.catch(function (e) {
return e instanceof SchedulerError;
});
// Throws no exception
var d1 = catchScheduler.schedule(function () {
throw new SchedulerError('woops');
});
var d2 = catchScheduler.schedule(function () {
throw new Error('woops');
});
// => Uncaught Error: woops
- rx.js
Gets the current time according to the Scheduler implementation.
(Number): The current time according to the Scheduler implementation.
var now = Rx.Scheduler.default.now();
console.log(now);
// => 1381806323143
- rx.js
Schedules an action to be executed.
action
(Function): Action to execute.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var disposable = Rx.Scheduler.immediate.schedule(function () {
console.log('hello');
});
// => hello
// Tries to cancel but too late since it is immediate
disposable.dispose();
- rx.js
Schedules an action to be executed with state.
state
(Any): State passed to the action to be executed.action
(Function): Action to execute.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var disposable = Rx.Scheduler.immediate.scheduleWithState('world', function (scheduler, x) {
console.log('hello ' + x);
});
// => hello world
// Tries to cancel but too late since it is immediate
disposable.dispose();
- rx.js
Schedules an action to be executed at the specified absolute due time. Note this only works with the built-in Rx.Scheduler.timeout
scheduler, as the rest will throw an exception as the framework does not allow for blocking.
dueTime
(Number): Absolute time at which to execute the action.action
(Function): Action to execute.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var disposable = Rx.Scheduler.default.scheduleWithAbsolute(
Date.now() + 5000, /* 5 seconds in the future */
function () {
console.log('hello');
}
);
// => hello
- rx.js
Schedules an action to be executed at the specified absolute due time. Note this only works with the built-in Rx.Scheduler.timeout
scheduler, as the rest will throw an exception as the framework does not allow for blocking.
state
(Any): State passed to the action to be executed.dueTime
(Number): Absolute time at which to execute the action.action
(Function): Action to execute.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var disposable = Rx.Scheduler.default.scheduleWithAbsoluteAndState(
'world',
Date.now() + 5000, /* 5 seconds in the future */
function (scheduler, x) {
console.log('hello ' + x);
}
);
// => hello world
- rx.js
Schedules an action to be executed after the specified relative due time. Note this only works with the built-in Rx.Scheduler.timeout
scheduler, as the rest will throw an exception as the framework does not allow for blocking.
dueTime
(Number): Relative time at which to execute the action.action
(Function): Action to execute.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var disposable = Rx.Scheduler.default.scheduleWithRelative(
5000, /* 5 seconds in the future */
function () {
console.log('hello');
}
);
// => hello
- rx.js
Schedules an action to be executed at the specified relative due time. Note this only works with the built-in Rx.Scheduler.timeout
scheduler, as the rest will throw an exception as the framework does not allow for blocking.
state
(Any): State passed to the action to be executed.dueTime
(Number): Relative time at which to execute the action.action
(Function): Action to execute.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var disposable = Rx.Scheduler.default.scheduleWithRelativeAndState(
'world',
5000, /* 5 seconds in the future */
function (scheduler, x) {
console.log('hello ' + x);
}
);
// => hello world
- rx.js
Schedules an action to be executed recursively.
action
(Function): Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var i = 0;
var disposable = Rx.Scheduler.immediate.scheduleRecursive(
function (self) {
console.log(i);
if (++i < 3) {
self(i);
}
}
);
// => 0
// => 1
// => 2
- rx.js
Schedules an action to be executed with state.
state
(Any): State passed to the action to be executed.action
(Function): Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in recursive invocation state.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var disposable = Rx.Scheduler.immediate.scheduleRecursiveWithState(
0,
function (i, self) {
console.log(i);
if (++i < 3) {
self(i);
}
}
);
// => 0
// => 1
// => 2
- rx.js
Schedules an action to be executed recursively at a specified absolute due time. Note this only works with the built-in Rx.Scheduler.timeout
scheduler, as the rest will throw an exception as the framework does not allow for blocking.
dueTime
(Number): Absolute time at which to execute the action for the first time.action
(Function): Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action at the specified absolute time.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var i = 0;
var disposable = Rx.Scheduler.default.scheduleRecursiveWithAbsolute(
Date.now() + 5000, /* 5 seconds in the future */
function (self) {
console.log(i);
if (++i < 3) {
// Schedule mutliplied by a second by position
self(Date.now() + (i * 1000));
}
}
);
// => 0
// => 1
// => 2
- rx.js
Schedules an action to be executed recursively at a specified absolute due time. Note this only works with the built-in Rx.Scheduler.timeout
scheduler, as the rest will throw an exception as the framework does not allow for blocking.
state
(Any): State passed to the action to be executed.dueTime
(Number): Absolute time at which to execute the action for the first time.action
(Function): Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in the recursive due time and invocation state.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var disposable = Rx.Scheduler.default.scheduleRecursiveWithAbsoluteAndState(
0,
Date.now() + 5000, /* 5 seconds in the future */
function (i, self) {
console.log(i);
if (++i < 3) {
// Schedule mutliplied by a second by position
self(i, Date.now() + (i * 1000));
}
}
);
// => 0
// => 1
// => 2
- rx.js
Schedules an action to be executed recursively at a specified relative due time. Note this only works with the built-in Rx.Scheduler.timeout
scheduler, as the rest will throw an exception as the framework does not allow for blocking.
dueTime
(Number): Relative time at which to execute the action for the first time.action
(Function): Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action at the specified relative time.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var i = 0;
var disposable = Rx.Scheduler.default.scheduleRecursiveWithRelative(
5000, /* 5 seconds in the future */
function (self) {
console.log(i);
if (++i < 3) {
// Schedule mutliplied by a second by position
self(i * 1000);
}
}
);
// => 0
// => 1
// => 2
- rx.js
Schedules an action to be executed recursively at a specified relative due time. Note this only works with the built-in Rx.Scheduler.timeout
scheduler, as the rest will throw an exception as the framework does not allow for blocking.
state
(Any): State passed to the action to be executed.dueTime
(Number): Relative time at which to execute the action for the first time.action
(Function): Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in the recursive due time and invocation state.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var disposable = Rx.Scheduler.default.scheduleRecursiveWithRelativeAndState(
0,
5000, /* 5 seconds in the future */
function (i, self) {
console.log(i);
if (++i < 3) {
// Schedule mutliplied by a second by position
self(i, i * 1000);
}
}
);
// => 0
// => 1
// => 2
- rx.js
Schedules 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.
period
(Number): Period for running the work periodically in ms.action
(Function): Action to execute.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var i = 0;
var disposable = Rx.Scheduler.default.schedulePeriodic(
1000, /* 1 second */
function () {
console.log(i);
// After three times, dispose
if (++i > 3) {
disposable.dispose();
}
});
// => 0
// => 1
// => 2
// => 3
- rx.js
Schedules 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.
state
(Any): State passed to the action to be executed.period
(Number): Period for running the work periodically in ms.action
(Function): Action to execute.
(Disposable): The disposable object used to cancel the scheduled action (best effort).
var disposable = Rx.Scheduler.default.schedulePeriodicWithState(
0,
1000, /* 1 second */
function (i) {
console.log(i);
// After three times, dispose
if (++i > 3) {
disposable.dispose();
}
return i;
});
// => 0
// => 1
// => 2
// => 3
- rx.js
Normalizes the specified time span value to a positive value.
timeSpan
(Number): The time span value to normalize.
(Number): The specified time span value if it is zero or positive; otherwise, 0
var r1 = Rx.Scheduler.normalize(-1);
console.log(r1);
// => 0
var r2 = Rx.Scheduler.normalize(255);
console.log(r2);
// => 255
- rx.js
Gets a scheduler that schedules work as soon as possible on the current thread. This implementation does not support relative and absolute scheduling due to thread blocking required.
var scheduler = Rx.Scheduler.currentThread;
var disposable = scheduler.scheduleWithState(
'world',
function (scheduler, x) {
console.log('hello ' + x);
});
// => hello world
- rx.js
Gets a scheduler that schedules work immediately on the current thread.
var scheduler = Rx.Scheduler.immediate;
var disposable = scheduler.scheduleRecursiveWithState(
0,
function (x, self) {
console.log(x);
if (++x < 3) {
self(x);
}
}
);
// => 0
// => 1
// => 2
- rx.js
Gets a scheduler that schedules work via a timed callback based upon platform.
For all schedule calls, it defaults to:
- Node.js: uses
setImmediate
for newer builds, andprocess.nextTick
for older versions. - Browser: depending on platform may use
setImmediate
,MessageChannel
,window.postMessage
and for older versions of IE, it will default toscript.onreadystatechanged
, else falls back towindow.setTimeout
.
For all relative and absolute scheduling, it defaults to using window.setTimeout
. This is now called the default scheduler and the usage of Scheduler.timeout
is now deprecated.
var scheduler = Rx.Scheduler.default;
var disposable = scheduler.scheduleWithState(
0,
function (scheduler, x) {
console.log(x);
}
);
// => 0
- rx.js