Delayer

Delayer

A class used to spread time or cpu intensive operations on multiple tasks in the event loop in order to avoid blocking other tasks that may need to be executed.

It is configured with a trigger time, which represents the maximum amount of time your tasks should monopolize the event loop. Choosing an appropriate trigger time is both important and hard. If too low it will impact the performances of your long running algorithm. If too high it will impact the other tasks that need to run in the event loop.

When using Delayer your code should contain frequent calls to await delayer.checkDelay(), usually at the end of every loop. checkDelay() will check the amount of time that ellasped since the last time your called it. If the amount of time is below the trigger time it returns immediately. If not it will call the delay() function that will retrigger the operation in a later task of the event loop.


Constructor

# new Delayer(triggerTime)

Constructs a new Delayer by specifying its trigger time.

Parameters:
Name Type Description
triggerTime number

The trigger time.

Source:
Example
import { Delayer, asyncRoot } from 'modern-async'

asyncRoot(async () => {
  const delayer = new Delayer(10) // a delayer with 10ms trigger time

  // some cpu intensive operation that will run for a long time
  for (let i = 0; i < 100000000; i += 1) {
    // some code
    await delayer.checkDelay()
  }
})

Members

# triggerTime :number

The trigger time of this Delayer in milliseconds. The trigger time represent the maximum amount of time before a call to checkDelay() decide to schedule a new task in the event loop.

Type:
  • number
Source:

Methods

# (async) checkDelay() → {boolean}

Checks if a delay must be applied according to the internal timer. If that's the case this method will call delay() and return true. If not it will do nothing and return false.

Source:
Returns:

true if a new task was scheduled in the event loop, false otherwise.

Type
boolean

# reset()

Resets the internal timer to the current time.

Source: