Options
All
  • Public
  • Public/Protected
  • All
Menu

A Scheduler is an execution context that can execute units of work asynchronously, with a delay or periodically.

It replaces Javascript's setTimeout, which is desirable due to the provided utilities and because special behavior might be needed in certain specialized contexts (e.g. tests), even if the Scheduler.global reference is implemented with setTimeout.

Hierarchy

Index

Constructors

Protected constructor

Properties

batchIndex

batchIndex: number = 0

Index of the current cycle, incremented automatically (modulo the batch size) when doing execution by means of Scheduler.executeBatched and the Scheduler is configured with ExecutionModel.batched.

When observed as being zero, it means an async boundary just happened.

executeBatched

executeBatched: function

Executes tasks in batches, according to the rules set by the given ExecutionModel.

The rules, depending on the chosen ExecutionModel:

  • if synchronous, then all tasks are executed with Scheduler.trampoline
  • if asynchronous, then all tasks are executed with Scheduler.executeAsync
  • if batched(n), then n tasks will be executed with Scheduler.trampoline and then the next execution will force an asynchronous boundary by means of Scheduler.executeAsync

Thus, in case of batched execution, an internal counter gets incremented to keep track of how many tasks where executed immediately (trampolined), a counter that's reset when reaching the threshold or when an executeAsync happens.

Type declaration

    • (runnable: function): void
    • Parameters

      • runnable: function
          • (): void
          • Returns void

      Returns void

executionModel

executionModel: ExecutionModel

The ExecutionModel is a specification of how run-loops and producers should behave in regards to executing tasks either synchronously or asynchronously.

Static global

global: DynamicRef<Scheduler> = DynamicRef.of(() => globalSchedulerRef)

Exposes a reusable GlobalScheduler reference by means of a DynamicRef, which allows for lexically scoped bindings to happen.

const myScheduler = new GlobalScheduler(false)

Scheduler.global.bind(myScheduler, () => {
  Scheduler.global.get() // myScheduler
})

Scheduler.global.get() // default instance

Methods

Abstract currentTimeMillis

  • currentTimeMillis(): number
  • Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

    It's the equivalent of Date.now(). When wanting to measure time, do not use Date.now() directly, prefer this method instead, because then it can be mocked for testing purposes, or overridden for better precision.

    Returns number

Abstract executeAsync

  • executeAsync(runnable: function): void
  • Schedules the given command for async execution.

    In GlobalScheduler this method uses setImmediate when available. But given that setImmediate is a very non-standard operation that is currently implemented only by IExplorer and Node.js, on non-supporting environments we fallback on setTimeout. See the W3C proposal.

    Parameters

    • runnable: function

      is the thunk to execute asynchronously

        • (): void
        • Returns void

    Returns void

Abstract reportFailure

  • reportFailure(e: Throwable): void
  • Reports that an asynchronous computation failed.

    Parameters

    • e: Throwable

    Returns void

scheduleAtFixedRate

  • Schedules a periodic task that becomes enabled first after the given initial delay, and subsequently with the given period. Executions will commence after initialDelay then initialDelay + period, then initialDelay + 2 * period and so on.

    If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the scheduler. If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.

    For example the following schedules a message to be printed to standard output approximately every 10 seconds with an initial delay of 5 seconds:

    const task =
      s.scheduleAtFixedRate(Duration.seconds(5), Duration.seconds(10), () => {
        console.log("repeated message")
      })
    
      // later if you change your mind ...
      task.cancel()
    

    Parameters

    • initialDelay: number | Duration

      is the time to wait until the first execution happens

    • period: number | Duration

      is the time to wait between 2 successive executions of the task

    • runnable: function

      is the thunk to be executed

        • (): void
        • Returns void

    Returns ICancelable

    a cancelable that can be used to cancel the execution of this repeated task at any time.

Abstract scheduleOnce

  • Schedules a task to run in the future, after delay.

    For example the following schedules a message to be printed to standard output after 5 minutes:

    const task =
      scheduler.scheduleOnce(Duration.minutes(5), () => {
        console.log("Hello, world!")
      })
    
    // later if you change your mind ... task.cancel()
    

    Parameters

    • delay: number | Duration

      is the time to wait until the execution happens; if specified as a number, then it's interpreted as milliseconds; for readability, prefer passing Duration values

    • runnable: function

      is the callback to be executed

        • (): void
        • Returns void

    Returns ICancelable

    a Cancelable that can be used to cancel the created task before execution.

scheduleWithFixedDelay

  • Schedules for execution a periodic task that is first executed after the given initial delay and subsequently with the given delay between the termination of one execution and the commencement of the next.

    For example the following schedules a message to be printed to standard output every 10 seconds with an initial delay of 5 seconds:

    const task =
      s.scheduleWithFixedDelay(Duration.seconds(5), Duration.seconds(10), () => {
        console.log("repeated message")
      })
    
    // later if you change your mind ...
    task.cancel()
    

    Parameters

    • initialDelay: number | Duration

      is the time to wait until the first execution happens

    • delay: number | Duration

      is the time to wait between 2 successive executions of the task

    • runnable: function

      is the thunk to be executed

        • (): void
        • Returns void

    Returns ICancelable

    a cancelable that can be used to cancel the execution of this repeated task at any time.

Abstract trampoline

  • trampoline(runnable: function): void
  • Execute the given runnable on the current call stack by means of a "trampoline", preserving stack safety.

    This is an alternative to executeAsync for triggering light asynchronous boundaries.

    Parameters

    • runnable: function
        • (): void
        • Returns void

    Returns void

Abstract withExecutionModel

  • Given a function that will receive the underlying ExecutionModel, returns a new Scheduler reference, based on the source that exposes the new ExecutionModel value when queried by means of the Scheduler.executionModel property.

    This method enables reusing global scheduler references in a local scope, but with a modified execution model to inject.

    The contract of this method (things you can rely on):

    1. the source Scheduler must not be modified in any way
    2. the implementation should wrap the source efficiently, such that the result mirrors the implementation of the source Scheduler in every way except for the execution model

    Sample:

    import { Scheduler, ExecutionModel } from "funfix"
    
    const scheduler = Schedule.global()
      .withExecutionModel(ExecutionModel.trampolined())
    

    Parameters

    Returns Scheduler

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc