@cloudflare/actors
Version:
An easier way to build with Cloudflare Durable Objects
86 lines • 3.23 kB
TypeScript
import { DurableObject } from "cloudflare:workers";
/**
* Represents a scheduled task within an Actor
* @template T Type of the payload data
* @template K Type of the callback
*/
export type Schedule<T = string, K extends keyof any = string> = {
/** Unique identifier for the schedule */
id: string;
/** Name of the method to be called */
callback: K;
/** Data to be passed to the callback */
payload: T;
} & ({
/** Type of schedule for one-time execution at a specific time */
type: "scheduled";
/** Timestamp when the task should execute */
time: number;
} | {
/** Type of schedule for delayed execution */
type: "delayed";
/** Timestamp when the task should execute */
time: number;
/** Number of seconds to delay execution */
delayInSeconds: number;
} | {
/** Type of schedule for recurring execution based on cron expression */
type: "cron";
/** Timestamp for the next execution */
time: number;
/** Cron expression defining the schedule */
cron: string;
});
export declare class Alarms<P extends DurableObject<any>> {
private parent;
storage: DurableObjectStorage | undefined;
constructor(ctx: DurableObjectState | undefined, parent: P);
/**
* Schedule a task to be executed in the future
* @template T Type of the payload data
* @param when When to execute the task (Date, seconds delay, or cron expression)
* @param callback Name of the method to call
* @param payload Data to pass to the callback
* @returns Schedule object representing the scheduled task
*/
schedule<T = string>(when: Date | string | number, callback: keyof typeof this.parent, payload?: T): Promise<Schedule<T>>;
/**
* Get a scheduled task by ID
* @template T Type of the payload data
* @param id ID of the scheduled task
* @returns The Schedule object or undefined if not found
*/
getSchedule<T = string>(id: string): Promise<Schedule<T> | undefined>;
/**
* Get scheduled tasks matching the given criteria
* @template T Type of the payload data
* @param criteria Criteria to filter schedules
* @returns Array of matching Schedule objects
*/
getSchedules<T = string>(criteria?: {
id?: string;
type?: "scheduled" | "delayed" | "cron";
timeRange?: {
start?: Date;
end?: Date;
};
}): Schedule<T>[];
/**
* Cancel a scheduled task
* @param id ID of the task to cancel
* @returns true if the task was cancelled, false otherwise
*/
cancelSchedule(id: string): Promise<boolean>;
private _scheduleNextAlarm;
readonly alarm: (alarmInfo?: AlarmInvocationInfo) => Promise<void>;
/**
* Execute SQL queries against the Agent's database
* @template T Type of the returned rows
* @param strings SQL query template strings
* @param values Values to be inserted into the query
* @returns Array of query results
*/
sql<T = Record<string, string | number | boolean | null>>(strings: TemplateStringsArray, ...values: (string | number | boolean | null)[]): T[];
private _tryCatch;
}
//# sourceMappingURL=index.d.ts.map