Skip to content

Commit a317a5a

Browse files
committed
feat: add waitForEvent and sendEvent support for local dev
This uses some @ts-expect-errors since these are not in workers-types, yet.
1 parent 743c57d commit a317a5a

File tree

5 files changed

+7
-13
lines changed

5 files changed

+7
-13
lines changed

fixtures/workflow/tests/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe("Workflows", () => {
139139
]);
140140
});
141141

142-
test.only("waitForEvent should work", async ({ expect }) => {
142+
test("waitForEvent should work", async ({ expect }) => {
143143
await fetchJson(`http://${ip}:${port}/createDemo2?workflowName=something`);
144144

145145
await fetchJson(

packages/workflows-shared/src/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {
1818

1919
export type Event = {
2020
timestamp: Date;
21-
payload: Record<string, unknown>;
21+
payload: unknown;
2222
type: string;
2323
};
2424

@@ -605,8 +605,8 @@ export class Context extends RpcTarget {
605605
(a) => a.hash === cacheKey && a.type === "timeout"
606606
);
607607
if (
608-
// @ts-expect-error priorityQueue is initiated in init
609608
(timeoutEntryPQ === undefined &&
609+
this.#engine.priorityQueue !== undefined &&
610610
this.#engine.priorityQueue.checkIfExistedInPast({
611611
hash: cacheKey,
612612
type: "timeout",

packages/workflows-shared/src/engine.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ export class Engine extends DurableObject<Env> {
7979
timeoutHandler: GracePeriodSemaphore;
8080
priorityQueue: TimePriorityQueue | undefined;
8181

82-
// eslint-disable-next-line @typescript-eslint/ban-types
83-
waiters: Map<string, Array<Function>> = new Map();
82+
waiters: Map<string, Array<(event: Event | PromiseLike<Event>) => void>> =
83+
new Map();
8484
eventMap: Map<string, Array<Event>> = new Map();
8585

8686
constructor(state: DurableObjectState, env: Env) {
@@ -263,7 +263,6 @@ export class Engine extends DurableObject<Env> {
263263
throw new Error("Engine was never started");
264264
}
265265

266-
// @ts-expect-error event types are wrong but that's okay since we just take metadata :)
267266
void this.init(
268267
metadata.accountId,
269268
metadata.workflow,

packages/workflows-shared/src/instance.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
DatabaseVersion,
55
DatabaseWorkflow,
66
} from "./engine";
7+
import type { WorkflowEvent } from "cloudflare:workers";
78

89
export type Instance = {
910
id: string;
@@ -23,10 +24,7 @@ export type InstanceMetadata = {
2324
workflow: DatabaseWorkflow;
2425
version: DatabaseVersion;
2526
instance: DatabaseInstance;
26-
event: {
27-
payload: Record<string, unknown>;
28-
timestamp: Date;
29-
};
27+
event: WorkflowEvent<unknown>;
3028
};
3129

3230
export enum InstanceStatus {

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)