@hypericon/pendulum
TypeScript icon, indicating that this package has built-in type declarations

0.4.3 • Public • Published

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();

Readme

Keywords

none

Package Sidebar

Install

npm i @hypericon/pendulum

Weekly Downloads

0

Version

0.4.3

License

MIT

Unpacked Size

27.3 kB

Total Files

12

Last publish

Collaborators

  • dyenamic
  • neonfish
  • unany