Skip to content

Commit af6536d

Browse files
ImriKochWixclaude
andauthored
feat(actor): sync Actor shim with deployed runtime (#243)
Align the type-only Actor shim with the current Durable Object implementation: drop members the runtime no longer exposes and add the scheduled-wake API. - Remove Conn.userId/appId/instanceId - Remove startLoop/stopLoop (now platform-managed) - Add handleWake, schedule, cancelSchedule Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 441840e commit af6536d

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

src/actor.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ import type { Base44Client } from "./client";
1717
*/
1818
export interface Conn<Send = unknown> {
1919
/** Unique per-connection id (one per socket/tab), the same value the client
20-
* receives from `subscribe()`. Use this — not userId — to identify a distinct
21-
* client, so multiple tabs of the same user are separate connections. */
20+
* receives from `subscribe()`. Identifies a distinct client, so multiple
21+
* tabs are separate connections. */
2222
id: string;
23-
userId: string;
24-
appId: string;
25-
instanceId: string;
2623
send(data: Send): void;
2724
reject(code: number, reason: string): void;
2825
}
@@ -63,12 +60,33 @@ export abstract class Actor<Incoming = unknown, Outgoing = unknown> {
6360
*/
6461
handleStart(): void | Promise<void> {}
6562

63+
/**
64+
* Optional handler for scheduled wakes. Runs when a timer armed via
65+
* {@link schedule} comes due, receiving the same `key` that was scheduled.
66+
* The schedule is one-shot: it fires once and is cleared before this runs.
67+
*/
68+
protected handleWake(_key: string): void | Promise<void> {}
69+
70+
/**
71+
* Arm a one-shot wake at `at` (epoch ms or a `Date`), identified by `key`.
72+
* When it comes due the platform calls {@link handleWake} with this `key`.
73+
* Scheduling the same `key` again reschedules it.
74+
*/
75+
protected schedule(_key: string, _at: number | Date): Promise<void> {
76+
throw new Error("Actor.schedule() is only available inside a deployed actor");
77+
}
78+
79+
/** Cancel a pending wake previously armed with {@link schedule}. */
80+
protected cancelSchedule(_key: string): Promise<void> {
81+
throw new Error("Actor.cancelSchedule() is only available inside a deployed actor");
82+
}
83+
6684
/**
6785
* Managed ticker (opt-in). Override {@link shouldTick} and the platform runs
6886
* {@link handleTick} on a timer of {@link tickIntervalMs} while it returns true,
6987
* and stops (letting the Durable Object hibernate — no compute cost) when it
7088
* returns false. The platform owns scheduling, rescheduling, self-heal, and
71-
* error-safety — you don't call {@link startLoop}/{@link stopLoop}.
89+
* error-safety.
7290
*
7391
* Re-evaluated after every connect/message/close and on every tick, so keep it
7492
* cheap and pure (no async, no side effects). Example: `return this.players >= 2`.
@@ -84,14 +102,6 @@ export abstract class Actor<Incoming = unknown, Outgoing = unknown> {
84102
throw new Error("Actor.getConnections() is only available inside a deployed actor");
85103
}
86104

87-
protected startLoop(_ms: number): Promise<void> {
88-
throw new Error("Actor.startLoop() is only available inside a deployed actor");
89-
}
90-
91-
protected stopLoop(): Promise<void> {
92-
throw new Error("Actor.stopLoop() is only available inside a deployed actor");
93-
}
94-
95105
protected get instanceId(): string {
96106
throw new Error("Actor.instanceId is only available inside a deployed actor");
97107
}

0 commit comments

Comments
 (0)