| 1 |
import { AsyncAction } from './AsyncAction'; |
| 2 |
import { AsyncScheduler } from './AsyncScheduler'; |
| 3 |
|
| 4 |
/** |
| 5 |
* |
| 6 |
* Async Scheduler |
| 7 |
* |
| 8 |
* <span class="informal">Schedule task as if you used setTimeout(task, duration)</span> |
| 9 |
* |
| 10 |
* `async` scheduler schedules tasks asynchronously, by putting them on the JavaScript |
| 11 |
* event loop queue. It is best used to delay tasks in time or to schedule tasks repeating |
| 12 |
* in intervals. |
| 13 |
* |
| 14 |
* If you just want to "defer" task, that is to perform it right after currently |
| 15 |
* executing synchronous code ends (commonly achieved by `setTimeout(deferredTask, 0)`), |
| 16 |
* better choice will be the {@link asapScheduler} scheduler. |
| 17 |
* |
| 18 |
* ## Examples |
| 19 |
* Use async scheduler to delay task |
| 20 |
* ```ts |
| 21 |
* import { asyncScheduler } from 'rxjs'; |
| 22 |
* |
| 23 |
* const task = () => console.log('it works!'); |
| 24 |
* |
| 25 |
* asyncScheduler.schedule(task, 2000); |
| 26 |
* |
| 27 |
* // After 2 seconds logs: |
| 28 |
* // "it works!" |
| 29 |
* ``` |
| 30 |
* |
| 31 |
* Use async scheduler to repeat task in intervals |
| 32 |
* ```ts |
| 33 |
* import { asyncScheduler } from 'rxjs'; |
| 34 |
* |
| 35 |
* function task(state) { |
| 36 |
* console.log(state); |
| 37 |
* this.schedule(state + 1, 1000); // `this` references currently executing Action, |
| 38 |
* // which we reschedule with new state and delay |
| 39 |
* } |
| 40 |
* |
| 41 |
* asyncScheduler.schedule(task, 3000, 0); |
| 42 |
* |
| 43 |
* // Logs: |
| 44 |
* // 0 after 3s |
| 45 |
* // 1 after 4s |
| 46 |
* // 2 after 5s |
| 47 |
* // 3 after 6s |
| 48 |
* ``` |
| 49 |
*/ |
| 50 |
|
| 51 |
export const asyncScheduler = new AsyncScheduler(AsyncAction); |
| 52 |
|
| 53 |
/** |
| 54 |
* @deprecated Renamed to {@link asyncScheduler}. Will be removed in v8. |
| 55 |
*/ |
| 56 |
export const async = asyncScheduler; |