Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 082d166

Browse files
committed
Implement alarm methods for other drivers
1 parent d3a3b81 commit 082d166

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

packages/actor-core/src/test/driver/actor.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface ActorDriverContext {
77

88
export class TestActorDriver implements ActorDriver {
99
#state: TestGlobalState;
10+
#alarms: Map<string, { timeout: NodeJS.Timeout; timestamp: number }> = new Map();
1011

1112
constructor(state: TestGlobalState) {
1213
this.#state = state;
@@ -64,9 +65,31 @@ export class TestActorDriver implements ActorDriver {
6465
}
6566

6667
async setAlarm(actor: AnyActorInstance, timestamp: number): Promise<void> {
67-
setTimeout(() => {
68+
// Clear any existing alarm for this actor
69+
await this.deleteAlarm(actor);
70+
71+
const expires = Math.max(timestamp - Date.now(), 0);
72+
// Set new alarm
73+
const timeout = setTimeout(() => {
6874
actor.onAlarm();
69-
}, timestamp - Date.now());
75+
this.#alarms.delete(actor.id);
76+
}, expires);
77+
78+
this.#alarms.set(actor.id, { timeout, timestamp });
79+
}
80+
81+
getAlarm(actor: AnyActorInstance): Promise<number | null> {
82+
const alarm = this.#alarms.get(actor.id);
83+
return Promise.resolve(alarm ? alarm.timestamp : null);
84+
}
85+
86+
deleteAlarm(actor: AnyActorInstance): Promise<void> {
87+
const alarm = this.#alarms.get(actor.id);
88+
if (alarm) {
89+
clearTimeout(alarm.timeout);
90+
this.#alarms.delete(actor.id);
91+
}
92+
return Promise.resolve();
7093
}
7194

7295
// Simple key serialization without depending on keys.ts

packages/drivers/memory/src/actor.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface ActorDriverContext {
77

88
export class MemoryActorDriver implements ActorDriver {
99
#state: MemoryGlobalState;
10+
#alarms = new Map<string, { timeout: NodeJS.Timeout; timestamp: number }>();
1011

1112
constructor(state: MemoryGlobalState) {
1213
this.#state = state;
@@ -64,9 +65,29 @@ export class MemoryActorDriver implements ActorDriver {
6465
}
6566

6667
async setAlarm(actor: AnyActorInstance, timestamp: number): Promise<void> {
67-
setTimeout(() => {
68+
// Clear any existing alarm
69+
await this.deleteAlarm(actor);
70+
71+
// Set new alarm
72+
const timeout = setTimeout(() => {
6873
actor.onAlarm();
74+
this.#alarms.delete(actor.id);
6975
}, timestamp - Date.now());
76+
77+
this.#alarms.set(actor.id, { timeout, timestamp });
78+
}
79+
80+
async getAlarm(actor: AnyActorInstance): Promise<number | null> {
81+
const alarm = this.#alarms.get(actor.id);
82+
return alarm?.timestamp ?? null;
83+
}
84+
85+
async deleteAlarm(actor: AnyActorInstance): Promise<void> {
86+
const alarm = this.#alarms.get(actor.id);
87+
if (alarm) {
88+
clearTimeout(alarm.timeout);
89+
this.#alarms.delete(actor.id);
90+
}
7091
}
7192

7293
// Simple key serialization without depending on keys.ts

packages/platforms/cloudflare-workers/src/actor_driver.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ActorDriver, KvKey, KvValue, AnyActorInstance } from "actor-core/driver-helpers";
1+
import type { ActorDriver, KvKey, KvValue, AnyActorInstance } from "actor-core/driver-helpers";
22

33
export interface ActorDriverContext {
44
ctx: DurableObjectState;
@@ -57,6 +57,14 @@ export class CloudflareWorkersActorDriver implements ActorDriver {
5757
await this.#doCtx.storage.setAlarm(timestamp);
5858
}
5959

60+
async getAlarm(_actor: AnyActorInstance): Promise<number | null> {
61+
return this.#doCtx.storage.getAlarm();
62+
}
63+
64+
async deleteAlarm(_actor: AnyActorInstance): Promise<void> {
65+
await this.#doCtx.storage.deleteAlarm();
66+
}
67+
6068
#serializeKey(key: KvKey): string {
6169
return JSON.stringify(key);
6270
}

packages/platforms/rivet/src/actor_driver.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface ActorDriverContext {
77

88
export class RivetActorDriver implements ActorDriver {
99
#ctx: ActorContext;
10+
#alarm: { timeout: ReturnType<typeof setTimeout>; timestamp: number } | null = null;
1011

1112
constructor(ctx: ActorContext) {
1213
this.#ctx = ctx;
@@ -48,9 +49,25 @@ export class RivetActorDriver implements ActorDriver {
4849
}
4950

5051
async setAlarm(actor: AnyActorInstance, timestamp: number): Promise<void> {
52+
await this.deleteAlarm(actor);
53+
5154
const timeout = Math.max(0, timestamp - Date.now());
52-
setTimeout(() => {
55+
const timeoutId = setTimeout(() => {
5356
actor.onAlarm();
57+
this.#alarm = null;
5458
}, timeout);
59+
60+
this.#alarm = { timeout: timeoutId, timestamp };
61+
}
62+
63+
async getAlarm(_actor: AnyActorInstance): Promise<number | null> {
64+
return this.#alarm?.timestamp ?? null;
65+
}
66+
67+
async deleteAlarm(_actor: AnyActorInstance): Promise<void> {
68+
if (this.#alarm) {
69+
clearTimeout(this.#alarm.timeout);
70+
this.#alarm = null;
71+
}
5572
}
5673
}

0 commit comments

Comments
 (0)