Skip to content

Class: IntervalRunner<TResult, TArgs> ​

Defined in: packages/rx/src/IntervalRunner.ts:109

A simple runner to execute a task periodically.

When to use IntervalRunner instead of IntervalSubject?

IntervalRunner is useful when the execution of the taskFn (and its onResult callback) needs to be precisely timed and/or excluded from the interval delay duration.

Examples ​

ts
When an API call needs to be made periodically and there's a possibility of delayed response (due to network issues
or longer backend execution time). In this case, using IntervalRunner with `sequential = true` will ensure the
delay is consistent between completion of current and start of the next API call.

Execute a function sequentially ​

Counting time will not start until function execution ends, maintaining the delay between end of execution consistent.

javascript
import fetch from '@superutils/fetch'
import { IntervalRunner } from '@superutils/rx'

const runner = new IntervalRunner(
    fetch.get, // function to execute
	   ['[DUMMYJSON-DOT-COM]/products'], // arguments to be provided to the function on each execution
    2000,
 )
runner.start(result => console.log({ result }))

Execute a function without enforcing sequential completion ​

Timing begins at the start of the task, ensuring a consistent interval between the start of each execution, regardless of when the task finishes.

javascript
import fetch from '@superutils/fetch'
import { IntervalRunner } from '@superutils/rx'

// Create a interval runner that retrieves products every 2 seconds
const runner = new IntervalRunner(
  fetch.get,
	 ['[DUMMYJSON-DOT-COM]/products'],
  2000,
  false,
 )
runner.start(result => console.log({ result }))

Execute a function and auto-retry on failure ​

javascript
import fetch from '@superutils/fetch'
import { IntervalRunner } from '@superutils/rx'

const runner = new IntervalRunner(
  () => retry(
    fetch.get, // function to execute
    { retry: 3 }, // retry maximum 3 times (max 5 attempts )
  ),
	 ['[DUMMYJSON-DOT-COM]/products'], // arguments to be provided to the function on each execution
  2000,
 )
runner.start(result => console.log({ result }))

Param ​

taskFn

task function to be executed periodically

Param ​

taskArgs

arguments (or a function that returns arguments) to be supplied to the task function.

Param ​

intervalMs

timer delay in milliseconds.

Param ​

sequential

true (default): will use setTimeout and will delay until execution is completed. This will ensure, in case the current execution takes longer, the following execution will not occur until current one is done and the interval delay is passed.

false: will use setInterval and the delay time to execute task will not affected. This may cause unwanted issues if the execution takes longer than the interval delay time. Use with caution.

Default: true

Param ​

preExecute

(optional) if true, will pre-execute task before starting the timer.

Default: true

Type Parameters ​

TResult ​

TResult = unknown

TArgs ​

TArgs extends unknown[] = unknown[]

Constructors ​

Constructor ​

new IntervalRunner<TResult, TArgs>(taskFn, taskArgs, intervalMs, sequential?, preExecute?): IntervalRunner<TResult, TArgs>

Defined in: packages/rx/src/IntervalRunner.ts:126

Parameters ​

taskFn ​

(...args) => TResult | Promise<TResult>

taskArgs ​

TArgs | ((this, runCount) => TArgs)

intervalMs ​

number | BehaviorSubject<number>

sequential? ​

boolean = true

preExecute? ​

boolean = true

Returns ​

IntervalRunner<TResult, TArgs>

Properties ​

intervalMs$ ​

readonly intervalMs$: BehaviorSubject<number>

Defined in: packages/rx/src/IntervalRunner.ts:121

RxJS BehaviorSubject to change timer delay (and restart the timer) on the fly


lastResult ​

lastResult: TResult | undefined

Defined in: packages/rx/src/IntervalRunner.ts:114


minIntervalMs ​

minIntervalMs: number = 1000

Defined in: packages/rx/src/IntervalRunner.ts:115


preExecute ​

readonly preExecute: boolean = true

Defined in: packages/rx/src/IntervalRunner.ts:136


sequential ​

readonly sequential: boolean = true

Defined in: packages/rx/src/IntervalRunner.ts:135


taskArgs ​

readonly taskArgs: TArgs | ((this, runCount) => TArgs)

Defined in: packages/rx/src/IntervalRunner.ts:128


taskFn ​

readonly taskFn: (...args) => TResult | Promise<TResult>

Defined in: packages/rx/src/IntervalRunner.ts:127

Parameters ​

args ​

...TArgs

Returns ​

TResult | Promise<TResult>

Accessors ​

runCount ​

Get Signature ​

get runCount(): number

Defined in: packages/rx/src/IntervalRunner.ts:214

Get number of times task has ran, excluding resets

Returns ​

number

Methods ​

executeOnce() ​

executeOnce(): Promise<TResult | undefined>

Defined in: packages/rx/src/IntervalRunner.ts:192

Executes the task function once, regardless of the interval runner's current state.

Returns ​

Promise<TResult | undefined>


isStarted() ​

isStarted(): boolean

Defined in: packages/rx/src/IntervalRunner.ts:196

Check if interval is running

Returns ​

boolean


restart() ​

restart(resetRunCount?): boolean

Defined in: packages/rx/src/IntervalRunner.ts:205

Restart interval

Parameters ​

resetRunCount? ​

boolean = false

(optional) whether to reset run count

Returns ​

boolean

indicates whether restart was successful


start() ​

start(onResult?, onBeforeExec?): boolean

Defined in: packages/rx/src/IntervalRunner.ts:229

Sets the onResult and onBeforeExec callbacks and starts the interval execution.

If the runner is already started, the new callbacks will be used for subsequent executions. To apply new callbacks immediately, call stop() first, then start(). In order to start using callbacks immediately, invoke the intervalRunner.stop() function first.

Parameters ​

onResult? ​

OnResultType<TResult>

(optional) function to be invoked whenever the task is succefully executed. See OnResultType

onBeforeExec? ​

onBeforeExecType

Returns ​

boolean

indicates whether starting interveral waa successful


stop() ​

stop(resetRunCount?): IntervalRunner<TResult, TArgs>

Defined in: packages/rx/src/IntervalRunner.ts:268

Stop interval runner

Parameters ​

resetRunCount? ​

boolean = false

(optional) whether to reset the run counter

Returns ​

IntervalRunner<TResult, TArgs>