Skip to content

Commit

Permalink
Add optional AlarmInvocationInfo parameter to Durable Object alarm() …
Browse files Browse the repository at this point in the history
…method

The runtime passes in this parameter when invoking alarm(); the `retryCount`
property gives a count of the number of previous times the runtime has tried to
run this specific alarm event.
  • Loading branch information
jclee committed Dec 9, 2024
1 parent 93a6037 commit a568d59
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion types/defines/rpc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ declare module "cloudflare:workers" {
constructor(ctx: DurableObjectState, env: Env);

fetch?(request: Request): Response | Promise<Response>;
alarm?(): void | Promise<void>;
alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise<void>;
webSocketMessage?(
ws: WebSocket,
message: string | ArrayBuffer
Expand Down
22 changes: 22 additions & 0 deletions types/test/types/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,27 @@ class TestObject extends DurableObject {
}
}

class TestAlarmObject extends DurableObject {
// Can declare alarm method consuming optional alarmInfo parameter
async alarm(alarmInfo?: AlarmInvocationInfo) {
if (alarmInfo !== undefined) {
const _isRetry: boolean = alarmInfo.isRetry;
const _retryCount: number = alarmInfo.retryCount;
}
}

// User code can invoke alarm() directly, if desired.
async runAlarmVoid(): Promise<void> {
return await this.alarm();
}
async runAlarmInfo(): Promise<void> {
return await this.alarm({
isRetry: true,
retryCount: 1,
});
}
}

class TestNaughtyEntrypoint extends WorkerEntrypoint {
// Check incorrectly typed methods
// @ts-expect-error
Expand Down Expand Up @@ -351,6 +372,7 @@ interface Env {

REGULAR_OBJECT: DurableObjectNamespace;
RPC_OBJECT: DurableObjectNamespace<TestObject>;
ALARM_OBJECT: DurableObjectNamespace<TestAlarmObject>;
NAUGHTY_OBJECT: DurableObjectNamespace<TestNaughtyObject>;
// @ts-expect-error `BoringClass` isn't an RPC capable type
__INVALID_OBJECT_1: DurableObjectNamespace<BoringClass>;
Expand Down

0 comments on commit a568d59

Please sign in to comment.