RxJs Book
  • Introduction
  • Summary
    • Why RxJS?
    • RxJS Design Guidelines
      • Introduction
      • When To Use RxJS
      • The RxJS Contract
      • Using RxJS
      • Operator Implementations
    • Getting Started With RxJS
      • What Are The Reactive Extensions?
      • Exploring Major Concepts In RxJS
      • Creating And Querying Observable Sequences
        • Creating And Subscribing To Simple Observable Sequences
        • Bridging To Events
        • Bridging To Callbacks
        • Bridging To Promises
        • Generators And Observable Sequences
        • Querying Observable Sequences
        • Error Handling With Observable Sequences
        • Transducers With Observable Sequences
        • Backpressure With Observable Sequences
        • Operators By Category
      • Subjects
      • Scheduling And Concurrency
      • Testing And Debugging
      • Implementing Your Own Operators
    • How Do I...?
      • How Do I Wrap An Existing API?
      • How Do I Integrate jQuery With RxJS?
      • How Do I Integrate Angular.js With RxJS?
      • How Do I Create A Simple Event Emitter?
    • Mapping RxJS From Different Libraries
      • For Bacon.js Users
      • For Async.js Users
    • Config
      • Promise
      • useNativeEvents
    • Helpers
      • defaultComparer
      • defaultSubComparer
      • defaultError
      • identity
      • just
      • isPromise
      • noop
      • pluck
    • Observable
      • Observable Methods
        • amb
        • case
        • catch
        • combineLatest
        • concat
        • create
        • defer
        • empty
        • for | forIn
        • forkJoin
        • from
        • fromCallback
        • fromEvent
        • fromEventPattern
        • fromNodeCallback
        • fromPromise
        • generate
        • generateWithAbsoluteTime
        • generateWithRelativeTime
        • if
        • interval
        • isObservable
        • just | return
        • merge
        • mergeDelayError
        • never
        • of
        • ofWithScheduler
        • onErrorResumeNext
        • pairs
        • range
        • repeat
        • spawn
        • start
        • startAsync
        • throw
        • timer
        • toAsync
        • using
        • when
        • while | whileDo
        • wrap
        • zip
      • Observable Instance Methods
        • amb
        • and
        • asObservable
        • average
        • buffer
        • bufferWithCount
        • bufferWithTime
        • bufferWithTimeOrCount
        • catch
        • combineLatest
        • concat
        • concatAll
        • concatMapObserver | selectConcatObserver
        • controlled
        • count
        • debounce
        • defaultIfEmpty
        • delay
        • delaySubscription
        • dematerialize
        • distinct
        • distinctUntilChanged
        • do | doAction | tap
        • doOnCompleted | tapOnCompleted
        • doOnError | tapOnError
        • doOnNext | tapOnNext
        • doWhile
        • elementAt
        • every
        • expand
        • extend | manySelect
        • filter | where
        • finally
        • find
        • findIndex
        • first
        • flatMap | selectMany
        • flatMapConcat | concatMap
        • flatMapFirst | selectSwitchFirst
        • flatMapLatest
        • flatMapObserver | selectManyObserver
        • flatMapWithMaxConcurrent
        • forkJoin
        • groupBy
        • groupByUntil
        • groupJoin
        • ignoreElements
        • includes
        • indexOf
        • isEmpty
        • join
        • jortSort
        • jortSortUntil
        • last
        • lastIndexOf
        • let | letBind
        • map | select
        • materialize
        • max
        • maxBy
        • merge
        • mergeAll
        • min
        • minBy
        • multicast
        • observeOn
        • onErrorResumeNext
        • pairwise
        • partition
        • pausable
        • pausableBuffered
        • pipe
        • pluck
        • publish
        • publishLast
        • publishValue
        • reduce
        • repeat
        • replay
        • retry
        • retryWhen
        • scan
        • sequenceEqual
        • share
        • shareReplay
        • shareValue
        • single
        • singleInstance
        • skip
        • skipLast
        • skipLastWithTime
        • skipUntil
        • skipUntilWithTime
        • skipWhile
        • skipWithTime
        • slice
        • some
        • startWith
        • subscribe | forEach
        • subscribeOn
        • subscribeOnCompleted
        • subscribeOnError
        • subscribeOnNext
        • sum
        • switch
        • switchFirst
        • take
        • takeLast
        • takeLastBuffer
        • takeLastBufferWithTime
        • takeLastWithTime
        • takeUntil
        • takeUntilWithTime
        • takeWhile
        • takeWithTime
        • thenDo
        • throttle
        • throttleLatest | sample
        • timeInterval
        • timeout
        • timestamp
        • toArray
        • toMap
        • toPromise
        • toSet
        • transduce
        • window
        • windowWithCount
        • windowWithTime
        • windowWithTimeOrCount
        • withLatestFrom
        • zip
        • zipIterable
    • Observer
      • Observer Methods
        • create
        • fromNotifier
      • Observer Instance Methods
        • asObserver
        • checked
        • notifyOn
        • onCompleted
        • onError
        • onNext
        • toNotifier
    • Notification
      • Notification Methods
        • createOnNext
        • createOnError
        • createOnCompleted
      • Notification Instance Methods
        • accept
        • toObservable
      • Notification Properties
        • exception
        • hasValue
        • kind
        • value
    • Subjects
      • Rx.AsyncSubject
      • Rx.BehaviorSubject
      • Rx.ReplaySubject
      • Rx.Subject
    • Schedulers
      • Rx.HistoricalScheduler
      • Rx.Scheduler
      • Rx.VirtualTimeScheduler
    • Disposables
      • Rx.CompositeDisposable
      • Rx.Disposable
      • Rx.RefCountDisposable
      • Rx.SerialDisposable
      • Rx.SingleAssignmentDisposable
    • Testing
      • Rx.ReactiveTest
      • Rx.Recorded
      • Rx.Subscription
      • Rx.TestScheduler
    • Bindings
      • DOM
        • Ajax
          • ajax
          • ajaxCold
          • get
          • get_Json
          • post
        • JSONP
          • jsonpRequest
          • jsonpRequestCold
        • Web Sockets
          • fromWebSocket
        • Web Workers
          • fromWebWorker
        • Mutation Observers
          • fromMutationObserver
        • Geolocation
          • getCurrentPosition
          • watchPosition
        • Schedulers
          • requestAnimationFrame
          • mutationObserver
      • jQuery
      • AngularJS
        • Factories
          • rx
          • observeOnScope
        • Observable Methods
          • safeApply
        • $rootScope Methods
          • $toObservable
          • $eventToObservable
          • $createObservableFunction
      • Facebook React
      • Ractive.js
      • Node.js
        • Callback Handlers
          • fromCallback
          • fromNodeCallback
        • Event Handlers
          • fromEvent
          • toEventEmitter
        • Stream Handlers
          • fromStream
          • fromReadableStream
          • fromWritableStream
          • fromTransformStream
          • writeToStream
  • Recipes
  • Which Operator do I use?
    • Creation Operators
    • Instance Operators
Powered by GitBook
On this page
  • Usage ##
  • Location
  • VirtualTimeScheduler Constructor ##
  • VirtualTimeScheduler Instance Methods ##
  • VirtualTimeScheduler Instance Properties ##
  • VirtualTimeScheduler Protected Abstract Methods ##
  • VirtualTimeScheduler Protected Methods ##
  • Inherited Classes ##
  • VirtualTimeScheduler Constructor ##
  • Rx.VirtualTimeScheduler(initialClock, comparer)
  • Location
  • VirtualTimeScheduler Instance Methods ##
  • Rx.VirtualTimeScheduler.prototype.advanceBy(time)
  • Location
  • Rx.VirtualTimeScheduler.prototype.advanceTo(time)
  • Location
  • Rx.VirtualTimeScheduler.prototype.scheduleAbsolute(dueTime, action)
  • Location
  • Rx.VirtualTimeScheduler.prototype.scheduleAbsoluteWithState(state, dueTime, action)
  • Location
  • Rx.VirtualTimeScheduler.prototype.scheduleRelative(dueTime, action)
  • Location
  • Rx.VirtualTimeScheduler.prototype.scheduleRelativeWithState(state, dueTime, action)
  • Location
  • Rx.VirtualTimeScheduler.prototype.sleep(time)
  • Location
  • Rx.VirtualTimeScheduler.prototype.start()
  • Location
  • Rx.VirtualTimeScheduler.prototype.stop()
  • Location
  • VirtualTimeScheduler Abstract Protected Methods ##
  • Rx.VirtualTimeScheduler.prototype.add(absolute, relative)
  • Arguments
  • Location
  • Rx.VirtualTimeScheduler.prototype.toDateTimeOffset(absolute)
  • Arguments
  • Location
  • Rx.VirtualTimeScheduler.prototype.toRelative(timeSpan)
  • Arguments
  • Location

Was this helpful?

  1. Summary
  2. Schedulers

Rx.VirtualTimeScheduler

Base class for providing scheduling in virtual time. This inherits from the Rx.Scheduler class.

Usage ##

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

Location

  • rx.virtualtime.js

VirtualTimeScheduler Constructor ##

VirtualTimeScheduler Instance Methods ##

VirtualTimeScheduler Instance Properties ##

VirtualTimeScheduler Protected Abstract Methods ##

VirtualTimeScheduler Protected Methods ##

Inherited Classes ##

VirtualTimeScheduler Constructor ##

Rx.VirtualTimeScheduler(initialClock, comparer)

Creates a new virtual time scheduler with the specified initial clock value and absolute time comparer.

Arguments

  1. initialClock (Function): Initial value for the clock.

  2. comparer (Function): Comparer to determine causality of events based on absolute time.

Example

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 */
);

Location

  • rx.virtualtime.js

VirtualTimeScheduler Instance Methods ##

Rx.VirtualTimeScheduler.prototype.advanceBy(time)

Advances the scheduler's clock by the specified relative time, running all work scheduled for that timespan.

Arguments

  1. time (Any): Relative time to advance the scheduler's clock by.

Example

var scheduler = new MyVirtualScheduler(
    200 /* initial time */
);

scheduler.scheduleAbsolute(250, function () {
    console.log('hello');
});

scheduler.advanceBy(300);
// => hello

console.log(scheduler.clock);
// => 500

Location

  • rx.virtualtime.js

Rx.VirtualTimeScheduler.prototype.advanceTo(time)

Advances the scheduler's clock to the specified time, running all work till that point.

Arguments

  1. time (Any): Absolute time to advance the scheduler's clock to.

Example

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

Location

  • rx.virtualtime.js

Rx.VirtualTimeScheduler.prototype.scheduleAbsolute(dueTime, action)

Schedules an action to be executed at dueTime.

Arguments

  1. dueTime (Any): Absolute time at which to execute the action.

  2. action (Function): Action to be executed.

Returns

(Disposable): The disposable object used to cancel the scheduled action (best effort).

Example

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

Location

  • rx.virtualtime.js

Rx.VirtualTimeScheduler.prototype.scheduleAbsoluteWithState(state, dueTime, action)

Schedules an action to be executed at dueTime.

Arguments

  1. state: (Any): State passed to the action to be executed.

  2. dueTime (Any): Absolute time at which to execute the action.

  3. action (Function): Action to be executed.

Returns

(Disposable): The disposable object used to cancel the scheduled action (best effort).

Example

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

Location

  • rx.virtualtime.js

Rx.VirtualTimeScheduler.prototype.scheduleRelative(dueTime, action)

Schedules an action to be executed at dueTime.

Arguments

  1. dueTime (Any): Relative time after which to execute the action.

  2. action (Function): Action to be executed.

Returns

(Disposable): The disposable object used to cancel the scheduled action (best effort).

Example

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

Location

  • rx.virtualtime.js

Rx.VirtualTimeScheduler.prototype.scheduleRelativeWithState(state, dueTime, action)

Schedules an action to be executed at dueTime.

Arguments

  1. state: (Any): State passed to the action to be executed.

  2. dueTime (Any): Relative time after which to execute the action.

  3. action (Function): Action to be executed.

Returns

(Disposable): The disposable object used to cancel the scheduled action (best effort).

Example

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

Location

  • rx.virtualtime.js

Rx.VirtualTimeScheduler.prototype.sleep(time)

Advances the scheduler's clock by the specified relative time.

Arguments

  1. time (Any): Relative time to advance the scheduler's clock by.

Example

var scheduler = new MyVirtualScheduler(
    0 /* initial time */
);

scheduler.sleep(400);

console.log(scheduler.clock);
// => 400

Location

  • rx.virtualtime.js

Rx.VirtualTimeScheduler.prototype.start()

Starts the virtual time scheduler.

Example

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

Location

  • rx.virtualtime.js

Rx.VirtualTimeScheduler.prototype.stop()

Stops the virtual time scheduler.

Example

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

Location

  • rx.virtualtime.js

VirtualTimeScheduler Abstract Protected Methods ##

Rx.VirtualTimeScheduler.prototype.add(absolute, relative)

Adds a relative time value to an absolute time value. This method is used in several methods including scheduleRelativeWithState, advanceBy and sleep.

Arguments

  1. absolute (Any): Absolute virtual time value.

  2. relative (Any): Relative virtual time value.

Returns

(Any): Resulting absolute virtual time sum value.

Example

One possible implementation could be as simple as the following:

scheduler.add = function (absolute, relative) {
    return absolute + relative;
};

Location

  • rx.virtualtime.js

Rx.VirtualTimeScheduler.prototype.toDateTimeOffset(absolute)

Converts an absolute time to a number. This is used directly in the now method on the Rx.Scheduler

Arguments

  1. absolute (Any): The absolute time to convert.

Returns

(Number): The absolute time in ms.

Example

One possible implementation could be as simple as the following:

// String -> Number
scheduler.toDateTimeOffset = function (absolute) {
    return return absolute.length;
};

Location

  • rx.virtualtime.js

Rx.VirtualTimeScheduler.prototype.toRelative(timeSpan)

Converts the time span number/Date to a relative virtual time value.

Arguments

  1. timeSpan (Any): The time span number value to convert. This is used directly in scheduleWithRelativeAndState and scheduleWithAbsoluteAndState.

Returns

(Number): Corresponding relative virtual time value.

Example

One possible implementation could be as simple as the following:

// Number -> Number
scheduler.toRelative = function (timeSpan) {
    return timeSpan;
};

Location

  • rx.virtualtime.js

PreviousRx.SchedulerNextDisposables

Last updated 5 years ago

Was this helpful?

Rx.Scheduler
constructor
advanceBy
advanceTo
scheduleAbsolute
scheduleAbsoluteWithState
scheduleRelative
scheduleRelativeWithState
sleep
start
stop
isEnabled
add
toDateTimeOffset
toRelative
getNext
Ⓢ
#
Ⓢ
#
Ⓢ
#
Ⓢ
#
Ⓢ
#
Ⓢ
#
Ⓢ
#
Ⓢ
#
Ⓢ
#
Ⓢ
#
Ⓢ
#
Ⓢ
#
Ⓢ
#