controller-repeater
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

controller-repeater

A decorator set to automatically call class methods with an interval

@Repeater class decorator sets up a loop (via setInterval) with a heartbeat interval (default: 100ms). On every heartbeat it executes every method marked by a @RepeaterTask. Particular tasks can be limited by minimum time intervals between them (this interval can't be lesser than the heartbeat interval). A task will not be executed again until previous call is finished.

Note: tasks are also run immediately at the next process tick (via setImmediate) before loop start.

Decorators

  • @Repeater is attached to a class.
export function Repeater(options?: {
  heartbeatInterval?: number;
  errorCatcher?: ErrorCatcher;
}): ClassDecorator;
  • @RepeaterTask is attached to a class method.
export function RepeaterTask(payload?: TaskPayload): PropertyDecorator;
export function RepeaterTask(interval: number, payload?: TaskPayload): PropertyDecorator;

Basic example

@Repeater()
class Test1 {
  public counter = 0;

  @RepeaterTask()
  public increment() {
    this.counter++;
  }
}

const t = new Test1();

// wait...

console.log(t.counter); // 1

Instance management

// Get an internal repeater instance
const instance = getRepeaterInstance(t);

// Stop repeater internal loop
instance.stop(); 

// Add a task to multiply a value by 2 every second
let myValue = 1;
instance.addTask('multiply', (factor) => myValue *= factor, 1000, [2]);

// Start repeater internal loop again
instance.start();

Dependencies (0)

    Dev Dependencies (6)

    Package Sidebar

    Install

    npm i controller-repeater

    Weekly Downloads

    1

    Version

    1.0.4

    License

    ISC

    Unpacked Size

    17.3 kB

    Total Files

    17

    Last publish

    Collaborators

    • ars9