75

I'v seen the term Scheduler very frequently in the documentation.

But, what does this term mean? I even don't know how to use a so called Scheduler. The official documentation didn't tell me what a Scheduler exactly is. Is this just a common concept or a specific concept in RxJS?

1 Answer 1

136

Rx schedulers provide an abstraction that allows work to be scheduled to run, possibly in the future, without the calling code needing to be aware of the mechanism used to schedule the work.

Whenever an Rx method needs to generate a notification, it schedules the work on a scheduler. By supplying a scheduler to the Rx method instead of using the default, you can subtly control how those notifications are sent out.

In server-side implementations of Rx (such as Rx.NET), schedulers play an important role. They allow you to schedule heavy duty work on the thread pool or dedicated threads, and run the final subscription on the UI thread so you can update your UI.

When using RxJs, it is actually pretty rare that you need to worry about the scheduler argument to most methods. Since JavaScript is essentially single-threaded, there are not a lot of options for scheduling and the default schedulers are usually the right choice.

The only real choices are:

  • immediateScheduler - Runs the work synchronously and immediately. Sort of like not using a scheduler at all. Work scheduled thus is guaranteed to run synchronously.
  • currentThreadScheduler - Similar to immediateScheduler in that the work is run immediately. However, it does not run work recursively. So, if the work is running and schedules more work, then that additional work is put into queue to be run after the current work finishes. Thus work sometimes runs synchronously and sometimes asynchronously. This scheduler is useful to avoid stack overflows or infinite recursion. For example Rx.Observable.of(42).repeat().subscribe() would cause infinite recursion if it ran on the immediate scheduler, but since return runs on the currentThread scheduler by default, infinite recursion is avoided.
  • timeoutScheduler - The only scheduler that supports work scheduled to be run in the future. Essentially uses setTimeout to schedule all work (though if you schedule the work to be run "now", then it uses other faster asynchronous methods to schedule the work). Any work scheduled on this scheduler is guaranteed to be run asynchronously.

There may be some more now, such as a scheduler that schedules work on the browser animation frames, etc.

If you are trying to write testable code, then you almost always want to supply the scheduler argument. This is because in your unit tests, you will be creating testScheduler instances, which will let your unit test control the clock used by your Rx code (and thus control the exact timing of the operations).

3
  • 3
    Excellent answer. This should go in a wiki somewhere. Commented Apr 24, 2015 at 22:44
  • 5
    Hi @Brandon - I've been researching Rxjs for use in my company for some time, I am surprised that Schedulers do not make use of Web Workers developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/… (although workers are separate .js files) - But it seems to me that Web Workers would be an idea fit to Schedulers Commented Feb 1, 2016 at 18:41
  • 6
    @LukeTO'Brien - Web Worker schedulers would be difficult since you'd be executing "random" functions that might have closures. Not clear how you'd serialize that action and send it to a web worker to be executed. However, there are RxJS factory methods that let you treat web workers as Subjects that consume/produce messages.
    – Brandon
    Commented Sep 12, 2016 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.