0.4.3 • Published 1 year ago

@hypericon/pendulum v0.4.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Pendulum

A simple recurring task scheduler.

npm (scoped)

npm install @hypericon/pendulum

Quickstart

import { Pendulum, PendulumTask } from "@hypericon/pendulum";

const scheduler = new Pendulum();

const task: PendulumTask = {
  label: "My task",
  dueTime: Date.now() + 5_000, // 5 seconds in the future
  taskFn: () => {
    console.log("My task has been executed!");
  },
};

scheduler.scheduleTask(task);
scheduler.start();

// 5 seconds later...
// [console]: "My task has been executed!"

const recurringTask: PendulumTask = {
  label: "My recurring task",
  dueTime: 0, // set to zero to calculate automatically for recurring tasks
  rrule: {
    freq: RRule.SECONDLY,
    interval: 2,
  },
  taskFn: () => {
    console.log("My recurring task has been executed!");
  },
};
scheduler.scheduleTask(recurringTask);

// [console every 2 seconds]: "My recurring task has been executed!"

const taskWithCustomRecurrence: PendulumTask = {
  label: "Another recurring task",
  dueTime: 0,
  recurrence: {
    getDescription() => `on the hour`,
    // Return a `Date` when the task will next become due after the parameter
    findNext: (after: Date) => {
      // If the task is being checked on the hour, execute it now
      if (after.getMinutes() === 0 && after.getSeconds() === 0) {
        return after;
      }
      // Calculate when the next hour will start
      const d = new Date(after);
      d.setHours(after.getHours() + 1);
      d.setMinutes(0);
      d.setSeconds(0);
      return d;
    },
  },
  taskFn: () => {
    console.log("My recurring task has been executed!");
  },
};
scheduler.scheduleTask(taskWithCustomRecurrence);

// [console on the hour]: "My recurring task has been executed!"

scheduler.stop();
0.4.3

1 year ago

0.4.1

1 year ago

0.4.2

1 year ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago