Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a simple scheduler

Tags:

rxjs

How would I go about creating a simple Scheduler that say, delays every item by a second? I want to use it for an Observable, and yes, I know that can be done in multiple other ways, I jsut want to accomplish it using a custom Scheduler.

There's some related tutorial here: http://codebetter.com/matthewpodwysocki/2010/05/12/introduction-to-the-reactive-extensions-for-javascript-custom-schedulers/ but it is quite outdated and the API looks very different now.

The current docs are not very usefuleither, but I guess I should be using Rx.Scheduler.prototype.schedulePeriodic, although I don't know what the action parameter should be.

like image 533
Sergi Mansilla Avatar asked Dec 05 '22 03:12

Sergi Mansilla


1 Answers

To create a new scheduler from the base scheduler you should have a look at scheduler.js. Essentially you need to understand how to do 4 things and you will automatically get all the periodic, recursive, exception handling extensions for free.

The function signature of Scheduler is

function Scheduler(now, schedule, scheduleRelative, scheduleAbsolute){}

To break it down:

now - A function that represents the schedulers notion of time, at any point when this is called it should return what the scheduler thinks now is. The default is to simply return new Date()

schedule - A function called when the action should be executed as soon as possible This function has the signature

function scheduleNow(state, action) {}

where action will have the signature

function action(scheduler, state) {}

It is used to schedule immediate actions on the scheduler. Immediate will have different meanings depending on your scheduler, however, for most cases (immediateScheduler aside) you will want that to occur in the next tick, whatever that means to your scheduler. You can have a look at the defaultScheduler, which does a little work to figure out what the best method would be in the environment (setImmediate is the first choice). In your case, since immediate will really mean "one second from now", you could probably just route it to scheduleRelative with this.scheduleRelativeWithState(state, 1000, action)

scheduleRelative This is called when the action should occur sometime in the future relative to now:

function scheduleRelative(state, dueTime, action) {}

Again this will likely use setTimeout with dueTime as the time parameter.

scheduleAbsolute This is probably the easiest to implement, it has the same signature as scheduleRelative, however instead of taking a time relative to now it is taking an absolute time irrespective of now (usually a Date object), to convert it you really just need to subtract now from it and pass it into this.scheduleWithRelativeAndState (see I told you you'll get free stuff).

In all cases the 3 schedule methods return a Disposable, this is used for best effort cancellation of the action. In the case of setTimeout this would be clearing the timeout using the returned id.

To finally answer your question, if you wanted to delay everything by 1 second the best way will probably be to apply a shift in scheduleRelative adding 1 second/1000 milliseconds to each scheduled event.

like image 79
paulpdaniels Avatar answered Mar 16 '23 22:03

paulpdaniels