1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
# `Rx.VirtualTimeScheduler` class #
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`, `toAbsoluteTime` and `toRelativeTime` methods as described below.
```js
/* 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.toAbsoluteTime = function (absolute) {
return new Date(absolute);
};
/**
* 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.toRelativeTime = function (timeSpan) {
return timeSpan;
};
// Schedule some time
scheduler.scheduleAbsolute(null, new Date(1), function () { console.log('foo'); });
scheduler.scheduleAbsolute(null, new Date(2), function () { console.log('bar'); });
scheduler.scheduleAbsolute(null, new Date(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
File:
- [`virtualtimescheduler.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js)
Dist:
- [`rx.all.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/dist/rx.all.js)
- [`rx.all.compat.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/dist/rx.all.js)
- [`rx.virtualtime.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/dist/rx.virtualtime.js)
## `VirtualTimeScheduler Constructor` ##
- [`constructor`](#rxvirtualtimeschedulerinitialclock-comparer)
## `VirtualTimeScheduler Instance Methods` ##
- [`advanceBy`](#rxvirtualtimeschedulerprototypeadvancebytime)
- [`advanceTo`](#rxvirtualtimeschedulerprototypeadvancetotime)
- [`scheduleAbsolute`](#rxvirtualtimeschedulerprototypescheduleabsolutestate-duetime-action)
- [`scheduleRelative`](#rxvirtualtimeschedulerprototypeschedulerelativestate-duetime-action)
- [`sleep`](#rxvirtualtimeschedulerprototypesleeptime)
- [`start`](#rxvritualtimeschedulerprototypestart)
- [`stop`](#rxvritualtimeschedulerprototypestop)
## `VirtualTimeScheduler Instance Properties` ##
- [`isEnabled`](#isenabled)
## `VirtualTimeScheduler Protected Abstract Methods` ##
- [`add`](#rxvirtualtimeschedulerprototypeaddabsolute-relative)
- [`toAbsoluteTime`](#rxvirtualtimeschedulerprototypetoabsolutetimeabsolute)
- [`toRelativeTime`](#rxvirtualtimeschedulerprototypetorelativetimetimespan)
## `VirtualTimeScheduler Protected Methods` ##
- [`getNext`](#rxvirtualtimeschedulerprototypegetnext)
## Inherited Classes ##
- [`Rx.Scheduler`](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/schedulers/scheduler.md)
## _VirtualTimeScheduler Constructor_ ##
### <a id="rxvirtualtimescheduler"></a>`Rx.VirtualTimeScheduler(initialClock, comparer)`
<a href="#rxvirtualtimescheduler">#</a> [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L38-L44 "View in source")
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
```js
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 */
);
```
***
## _VirtualTimeScheduler Instance Methods_ ##
### <a id="rxvirtualtimeschedulerprototypeadvancebytime"></a>`Rx.VirtualTimeScheduler.prototype.advanceBy(time)`
<a href="#rxvirtualtimeschedulerprototypeadvancebytime">#</a> [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L166-L176 "View in source")
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
```js
var scheduler = new MyVirtualScheduler(
200 /* initial time */
);
scheduler.scheduleAbsolute(null, 250, function () {
console.log('hello');
});
scheduler.advanceBy(300);
// => hello
console.log(scheduler.clock);
// => 500
```
***
### <a id="rxvirtualtimeschedulerprototypeadvancetotime"></a>`Rx.VirtualTimeScheduler.prototype.advanceTo(time)`
<a href="#rxvirtualtimeschedulerprototypeadvancetotime">#</a> [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js "View in source")
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
```js
var scheduler = new MyVirtualScheduler(
0 /* initial time */
);
scheduler.scheduleAbsolute(null, 100, function () {
console.log('hello');
});
scheduler.scheduleAbsolute(null, 200, function () {
console.log('world');
});
scheduler.advanceBy(300);
// => hello
// => world
console.log(scheduler.clock);
// => 300
```
***
### <a id="rxvirtualtimeschedulerprototypescheduleabsolutestate-duetime-action"></a>`Rx.VirtualTimeScheduler.prototype.scheduleAbsolute(state, dueTime, action)`
<a href="#rxvirtualtimeschedulerprototypescheduleabsolutestate-duetime-action">#</a> [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js "View in source")
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 execute with the following arguments:
1. `scheduler`: `Scheduler` - The current Scheduler
2. `state`: `Any` - The current state
#### Returns
*(Disposable)*: The disposable object used to cancel the scheduled action (best effort).
#### Example
```js
var scheduler = new MyVirtualScheduler(
0 /* initial time */
);
scheduler.scheduleAbsolute('world', 100, function (scheduler, state) {
console.log('hello ' + state);
});
scheduler.scheduleAbsolute('moon', 200, function (scheduler, state) {
console.log('goodnight ' + state);
});
scheduler.start();
// => hello world
// => goodnight moon
console.log(scheduler.clock);
// => 200
```
***
### <a id="rxvirtualtimeschedulerprototypeschedulerelativestate-duetime-action"></a>`Rx.VirtualTimeScheduler.prototype.scheduleRelative(state, dueTime, action)`
<a href="#rxvirtualtimeschedulerprototypeschedulerelativestate-duetime-action">#</a> [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L89-L92 "View in source")
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 execute with the following arguments:
1. `scheduler`: `Scheduler` - The current Scheduler
2. `state`: `Any` - The current state
#### Returns
*(Disposable)*: The disposable object used to cancel the scheduled action (best effort).
#### Example
```js
var scheduler = new MyVirtualScheduler(
0 /* initial time */
);
scheduler.scheduleRelative('world', 100, function (scheduler, state) {
console.log('hello ' + state);
});
scheduler.scheduleRelative('moon', 200, function (scheduler, state) {
console.log('goodnight ' + state);
});
scheduler.start();
// => hello world
// => goodnight moon
console.log(scheduler.clock);
// => 300
```
***
### <a id="rxvritualtimeschedulerprototypesleeptime"></a>`Rx.VirtualTimeScheduler.prototype.sleep(time)`
<a href="#rxvritualtimeschedulerprototypesleeptime">#</a> [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L182-L190 "View in source")
Advances the scheduler's clock by the specified relative time.
#### Arguments
1. `time` *(Any)*: Relative time to advance the scheduler's clock by.
#### Example
```js
var scheduler = new MyVirtualScheduler(
0 /* initial time */
);
scheduler.sleep(400);
console.log(scheduler.clock);
// => 400
```
***
### <a id="rxvritualtimeschedulerprototypestart"></a>`Rx.VirtualTimeScheduler.prototype.start()`
<a href="#rxvritualtimeschedulerprototypestart">#</a> [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L107-L123 "View in source")
Starts the virtual time scheduler.
#### Example
```js
var scheduler = new MyVirtualScheduler(
0 /* initial time */
);
scheduler.scheduleRelative('world', 100, function (scheduler, state) {
console.log('hello ' + state);
});
scheduler.scheduleRelative('moon', 200, function (scheduler, state) {
console.log('goodnight ' + state);
});
scheduler.start();
// => hello world
// => goodnight moon
console.log(scheduler.clock);
// => 400
```
***
### <a id="rxvritualtimeschedulerprototypestop"></a>`Rx.VirtualTimeScheduler.prototype.stop()`
<a href="#rxvritualtimeschedulerprototypestop">#</a> [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L128-L130 "View in source")
Stops the virtual time scheduler.
#### Example
```js
var scheduler = new MyVirtualScheduler(
0 /* initial time */
);
scheduler.scheduleRelative('world', 100, function (scheduler, state) {
console.log('hello ' + state);
});
scheduler.scheduleRelative(null, 100, function (scheduler, state) {
scheduler.stop();
});
scheduler.scheduleRelative(null, 100, function (scheduler, state) {
console.log('goodbye cruel ' + state);
});
scheduler.start();
// => hello world
```
***
## _VirtualTimeScheduler Abstract Protected Methods_ ##
### <a id="rxvirtualtimeschedulerprototypeaddabsolute-relative"></a>`Rx.VirtualTimeScheduler.prototype.add(absolute, relative)`
<a href="#rxvirtualtimeschedulerprototypeaddabsolute-relative">#</a> [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/subjects/asyncsubject.js#L54 "View in source")
Adds a relative time value to an absolute time value. This method is used in several methods including `scheduleRelative`, `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:
```js
scheduler.add = function (absolute, relative) {
return absolute + relative;
};
```
***
### <a id="rxvirtualtimeschedulerprototypetoabsolutetimeabsolute"></a>`Rx.VirtualTimeScheduler.prototype.toAbsoluteTime(absolute)`
<a href="#rxvirtualtimeschedulerprototypetoabsolutetimeabsolute">#</a> [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/subjects/asyncsubject.js#L61 "View in source")
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:
```js
// String -> Number
scheduler.toAbsoluteTime = function (absolute) {
return absolute.length;
};
```
***
### <a id="rxvirtualtimeschedulerprototypetorelativetimetimespan"></a>`Rx.VirtualTimeScheduler.prototype.toRelativeTime(timeSpan)`
<a href="#rxvirtualtimeschedulerprototypetorelativetimetimespan">#</a> [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/subjects/asyncsubject.js#L61 "View in source")
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 `scheduleFuture`.
#### Returns
*(Number)*: Corresponding relative virtual time value.
#### Example
One possible implementation could be as simple as the following:
```js
// Number -> Number
scheduler.toRelativeTime = function (timeSpan) {
return timeSpan;
};
```
***
|