Skip to content

Commit 4d16e9c

Browse files
committed
Remove @ts-expect-error and add some documentation
1 parent 4206a45 commit 4d16e9c

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

packages/vitest-pool-workers/src/worker/workflows.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
} from "@cloudflare/workflows-shared/src/instance";
55
import { runInRunnerObject } from "./durable-objects";
66
import { env, internalEnv } from "./env";
7+
import type { WorkflowBinding } from "@cloudflare/workflows-shared/src/binding";
78
import type {
89
StepSelector,
910
WorkflowInstanceModifier,
@@ -23,7 +24,7 @@ export interface WorkflowInstanceIntrospector {
2324
}
2425

2526
export async function introspectWorkflowInstance(
26-
workflow: Workflow,
27+
workflow: WorkflowBinding,
2728
instanceId: string
2829
): Promise<WorkflowInstanceIntrospector> {
2930
if (!workflow || !instanceId) {
@@ -37,15 +38,17 @@ export async function introspectWorkflowInstance(
3738
class WorkflowInstanceIntrospectorHandle
3839
implements WorkflowInstanceIntrospector
3940
{
40-
#workflow: Workflow;
41+
#workflow: WorkflowBinding;
4142
#instanceId: string;
4243
#instanceModifier: WorkflowInstanceModifier;
4344

44-
constructor(workflow: Workflow, instanceId: string) {
45+
constructor(workflow: WorkflowBinding, instanceId: string) {
4546
this.#workflow = workflow;
4647
this.#instanceId = instanceId;
47-
// @ts-expect-error not exposed
48-
this.#instanceModifier = workflow.unsafeGetInstanceModifier(instanceId);
48+
49+
this.#instanceModifier = workflow.unsafeGetInstanceModifier(
50+
instanceId
51+
) as WorkflowInstanceModifier;
4952
}
5053

5154
async modify(fn: ModifierCallback): Promise<WorkflowInstanceIntrospector> {
@@ -55,7 +58,6 @@ class WorkflowInstanceIntrospectorHandle
5558
}
5659

5760
async waitForStepResult(step: StepSelector): Promise<unknown> {
58-
// @ts-expect-error not exposed
5961
const stepResult = await this.#workflow.unsafeWaitForStepResult(
6062
this.#instanceId,
6163
step.name,
@@ -80,12 +82,10 @@ class WorkflowInstanceIntrospectorHandle
8082
// starts running, so waiting for it to be queued should always return
8183
return;
8284
}
83-
// @ts-expect-error not exposed
8485
await this.#workflow.unsafeWaitForStatus(this.#instanceId, status);
8586
}
8687

8788
async cleanUp(): Promise<void> {
88-
// @ts-expect-error not exposed
8989
await this.#workflow.unsafeAbort(this.#instanceId, "Instance clean up");
9090
}
9191

@@ -104,15 +104,15 @@ export interface WorkflowIntrospector {
104104
}
105105

106106
export async function introspectWorkflow(
107-
workflow: Workflow
107+
workflow: WorkflowBinding
108108
): Promise<WorkflowIntrospectorHandle> {
109109
if (!workflow) {
110110
throw new Error("[WorkflowIntrospector] Workflow binding is required.");
111111
}
112112

113113
const modifierCallbacks: ModifierCallback[] = [];
114114
const instanceIntrospectors: WorkflowInstanceIntrospector[] = [];
115-
// @ts-expect-error getBindingName not exposed
115+
116116
const bindingName = await workflow.unsafeGetBindingName();
117117
const internalOriginalWorkflow = internalEnv[bindingName] as Workflow;
118118
const externalOriginalWorkflow = env[bindingName] as Workflow;
@@ -214,13 +214,13 @@ export async function introspectWorkflow(
214214
}
215215

216216
class WorkflowIntrospectorHandle implements WorkflowIntrospector {
217-
workflow: Workflow;
217+
workflow: WorkflowBinding;
218218
#modifierCallbacks: ModifierCallback[];
219219
#instanceIntrospectors: WorkflowInstanceIntrospector[];
220220
#cleanupCallback: () => void;
221221

222222
constructor(
223-
workflow: Workflow,
223+
workflow: WorkflowBinding,
224224
modifierCallbacks: ModifierCallback[],
225225
instanceIntrospectors: WorkflowInstanceIntrospector[],
226226
cleanupCallback: () => void

packages/workflows-shared/src/modifier.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ export class WorkflowInstanceModifier extends RpcTarget {
7676
}
7777
}
7878

79+
// step.do() flow: It first checks if a result or error is already in the cache and, if so, returns it immediately.
80+
// If nothing is in the cache, it checks for remaining attempts and runs the user's code against the defined timeout.
81+
// Since `step.do()` performs this initial cache check, directly changing the `valueKey` would cause it to
82+
// assume the value was pre-cached, preventing it from writing any logs about the step's execution state.
83+
// Storing the value under a separate key is crucial because it ensures all execution logs for the step are
84+
// generated, rather than the step being skipped due to a premature cache hit.
7985
async mockStepResult(step: StepSelector, stepResult: unknown): Promise<void> {
8086
const valueKey = await this.#getStepCacheKey(step);
8187

@@ -88,6 +94,7 @@ export class WorkflowInstanceModifier extends RpcTarget {
8894
await this.#state.storage.put(`replace-result-${valueKey}`, stepResult);
8995
}
9096

97+
// Same logic of `mockStepResult` but stores an error instead of a value.
9198
async mockStepError(
9299
step: StepSelector,
93100
error: Error,

0 commit comments

Comments
 (0)