Class: Scheduler

bitbucket/util/scheduler. Scheduler

Provides a way of scheduling a job that can be backed off. A schedule can be configured
to automatically start backing off when the user blurs the application or is inactive.

User activity means scrolling, mouse movement, and keyboard input.

When a user becomes active again after being inactive for a specified period of time
(the inactivityTime) then the schedule will start up again after the given immediateTime.

new Scheduler(schedule)

Schedule a schedule

Parameters:
Name Type Argument Default Description
schedule Object <optional>
Scheduler.defaults

the schedule

Properties
Name Type Argument Description
backoff Object <optional>

the backoff configuration

Properties
Name Type Argument Description
onBlur boolean <optional>

should back off on blur?

onInactive boolean <optional>

should back off on inactivity?

jitter boolean <optional>

when enabled a random range is added to the scheduler when it starts
to reduce the likelihood that clients will all reconnect at the same time after an outage.

interval number <optional>

how often to run the schedule

immediateTime number <optional>

how long to wait to run a schedule "immediately". This is a buffer time
taken to wait when starting the schedule

maxInterval number <optional>

the max time the schedule will back off to.

inactivityTime number <optional>

the time at which a user is considered inactive.

job Schedulerjob

the function that will be invoked every time the schedule runs. The scheduler
will wait for the job function's Deferred to settle before continuing.

Source:
Examples
// ES5

define('bitbucket/plugin/my-plugin', [
    'bitbucket/util/scheduler'
], function(
    Scheduler
) {
    var schedule = new Scheduler({
        backoff: {
            onBlur: true,
            onInactive: true,
        },
        maxInterval: 10 * Scheduler.MINUTE,
        interval: 30 * Scheduler.SECOND,
        job: function() {
            return getSomeDataFromServer(arg1, arg2);
        }
    });

    schedule.start();
});
// ES2015+

import Schedule, { MINUTE, SECOND } from 'bitbucket/util/scheduler';

const schedule = new Scheduler({
    backoff: {
        onBlur: true,
        onInactive: true,
    },
    maxInterval: 10 * MINUTE,
    interval: 30 * SECOND,
    job: () => getSomeDataFromServer(arg1, arg2),
});

schedule.start();

Members

<static> defaults :Object

Type:
  • Object
Properties:
Name Type Argument Default Description
backoff Object <optional>

the backoff configuration

Properties
Name Type Argument Default Description
onBlur boolean <optional>
true

should back off on blur?

onInactive boolean <optional>
true

should back off on inactivity?

jitter boolean <optional>
true

when enabled a random range is added to the scheduler when it starts
to reduce the likelihood that clients will all reconnect at the same time after an outage.

interval number <optional>
10 * SECOND

how often to run the schedule (milliseconds)

immediateTime number <optional>
SECOND

how long to wait to run a schedule "immediately".
This is a buffer time taken to wait when starting the schedule (milliseconds).

maxInterval number <optional>
5 * MINUTE

the max time the schedule will back off to. (milliseconds)

inactivityTime number <optional>
2 * MINUTE

the time at which a user is considered inactive. (milliseconds)

Source:

Methods

destroy()

Stop the scheduler and remove its installed event handlers.

Source:

getBackoffTime() → {number}

Get the backoff time based on the last time the user was active and the current level of backoff

Source:
Returns:
Type
number

run(immediate, jitterAmount)

Run with a given timeout and execute the configured job.
The schedule will be rescheduled once the job's Deferred settles.

Parameters:
Name Type Argument Default Description
immediate boolean <optional>
false
jitterAmount number <optional>
0
Source:

start(immediate)

Start the scheduler.
Starting a scheduler implies that the user is currently active and optionally immediately starts the run rather
than waiting for the timer to reach its first interval. This immediate run will start after the schedule's immediateTime.
When starting a scheduler an explicit stop is issued to prevent rogue deferreds/timers from firing.

Parameters:
Name Type Argument Description
immediate boolean <optional>
Source:

stop()

Stop the scheduler. This will abort any in-flight jobs if they are abortable and stop the current run.

Source:

Type Definitions

Schedulerjob() → {Deferred}

Source:
Returns:
Type
Deferred