@weedzcokie/scheduler
Advanced tools
Comparing version 1.0.6 to 1.0.7
/// <reference types="node" resolution-mode="require"/> | ||
export interface Task { | ||
export interface Task<T = unknown> { | ||
name: string; | ||
fn: () => unknown | Promise<unknown>; | ||
fn: () => T | Promise<T>; | ||
timeout: undefined | NodeJS.Timeout; | ||
getNextExecutionTime: () => number; | ||
errorHandler?: (err: unknown) => unknown; | ||
successHandler?: (result: T) => unknown; | ||
} | ||
@@ -14,4 +15,9 @@ export declare const msInHour = 3600000; | ||
export declare function msUntilNextDay(): number; | ||
export declare function schedule(name: string, fn: () => number, period?: () => number, errorHandler?: Task["errorHandler"]): void; | ||
interface ScheduleOpts { | ||
errorHandler?: Task["errorHandler"]; | ||
successHandler?: Task["successHandler"]; | ||
} | ||
export declare function schedule(name: string, fn: () => number, period?: Task["getNextExecutionTime"], opts?: ScheduleOpts): void; | ||
export declare function stopTask(name: string): boolean; | ||
export declare function rescheduleTask(name: string, period?: Task["getNextExecutionTime"]): boolean; | ||
export {}; |
@@ -20,3 +20,4 @@ export const msInHour = 3600000; | ||
try { | ||
await task.fn(); | ||
const result = await task.fn(); | ||
task.successHandler?.(result); | ||
} | ||
@@ -32,3 +33,3 @@ catch (err) { | ||
const tasks = new Map(); | ||
export function schedule(name, fn, period = () => 1000, errorHandler) { | ||
export function schedule(name, fn, period = () => 1000, opts = {}) { | ||
const task = { | ||
@@ -39,3 +40,4 @@ name, | ||
getNextExecutionTime: period, | ||
errorHandler, | ||
errorHandler: opts.errorHandler, | ||
successHandler: opts.successHandler, | ||
}; | ||
@@ -42,0 +44,0 @@ task.timeout = setTimeout(runTask, task.getNextExecutionTime(), task); |
{ | ||
"name": "@weedzcokie/scheduler", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -1,1 +0,3 @@ | ||
# nodejs-scheduler | ||
# nodejs-scheduler | ||
[![npm](https://img.shields.io/npm/v/@weedzcokie/scheduler?style=flat-square)](https://www.npmjs.com/package/@weedzcokie/scheduler) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4709
86
4