@@ -17,10 +17,16 @@ type Env = {
17
17
18
18
// this.env.WORKFLOW is WorkflowBinding
19
19
export class WorkflowBinding extends WorkerEntrypoint < Env > implements Workflow {
20
+ constructor ( ctx : ExecutionContext , env : Env ) {
21
+ super ( ctx , env ) ;
22
+ }
23
+
20
24
public async create ( {
21
25
id = crypto . randomUUID ( ) ,
22
26
params = { } ,
23
- } : WorkflowInstanceCreateOptions = { } ) : Promise < WorkflowInstance > {
27
+ } : WorkflowInstanceCreateOptions = { } ) : Promise <
28
+ WorkflowInstance & Disposable
29
+ > {
24
30
if ( ! isValidWorkflowInstanceId ( id ) ) {
25
31
throw new WorkflowError ( "Workflow instance has invalid id" ) ;
26
32
}
@@ -51,10 +57,11 @@ export class WorkflowBinding extends WorkerEntrypoint<Env> implements Workflow {
51
57
restart : handle . restart . bind ( handle ) ,
52
58
status : handle . status . bind ( handle ) ,
53
59
sendEvent : handle . sendEvent . bind ( handle ) ,
60
+ [ Symbol . dispose ] : handle . dispose . bind ( handle ) ,
54
61
} ;
55
62
}
56
63
57
- public async get ( id : string ) : Promise < WorkflowInstance > {
64
+ public async get ( id : string ) : Promise < WorkflowInstance & Disposable > {
58
65
const engineStubId = this . env . ENGINE . idFromName ( id ) ;
59
66
const engineStub = this . env . ENGINE . get ( engineStubId ) ;
60
67
const handle = new WorkflowHandle ( id , engineStub ) ;
@@ -73,6 +80,7 @@ export class WorkflowBinding extends WorkerEntrypoint<Env> implements Workflow {
73
80
restart : handle . restart . bind ( handle ) ,
74
81
status : handle . status . bind ( handle ) ,
75
82
sendEvent : handle . sendEvent . bind ( handle ) ,
83
+ [ Symbol . dispose ] : handle . dispose . bind ( handle ) ,
76
84
} ;
77
85
}
78
86
public async createBatch (
@@ -84,14 +92,21 @@ export class WorkflowBinding extends WorkerEntrypoint<Env> implements Workflow {
84
92
) ;
85
93
}
86
94
87
- return await Promise . all ( batch . map ( ( val ) => this . create ( val ) ) ) ;
95
+ return await Promise . all (
96
+ batch . map ( async ( val ) => {
97
+ using res = await this . create ( val ) ;
98
+ return res ;
99
+ } )
100
+ ) ;
88
101
}
89
102
90
- public unsafeGetBindingName ( ) : string {
103
+ public async unsafeGetBindingName ( ) : Promise < string > {
104
+ // async because of rpc
91
105
return this . env . BINDING_NAME ;
92
106
}
93
107
94
- public unsafeGetInstanceModifier ( instanceId : string ) : unknown {
108
+ public async unsafeGetInstanceModifier ( instanceId : string ) : Promise < unknown > {
109
+ // async because of rpc
95
110
const stubId = this . env . ENGINE . idFromName ( instanceId ) ;
96
111
const stub = this . env . ENGINE . get ( stubId ) ;
97
112
@@ -108,7 +123,8 @@ export class WorkflowBinding extends WorkerEntrypoint<Env> implements Workflow {
108
123
const stubId = this . env . ENGINE . idFromName ( instanceId ) ;
109
124
const stub = this . env . ENGINE . get ( stubId ) ;
110
125
111
- return await stub . waitForStepResult ( name , index ) ;
126
+ using res = await stub . waitForStepResult ( name , index ) ;
127
+ return res ;
112
128
}
113
129
114
130
public async unsafeAbort ( instanceId : string , reason ?: string ) : Promise < void > {
@@ -207,4 +223,9 @@ export class WorkflowHandle extends RpcTarget implements WorkflowInstance {
207
223
timestamp : new Date ( ) ,
208
224
} ) ;
209
225
}
226
+
227
+ public dispose ( ) {
228
+ // @ts -expect-error - DurableObjectStub should be disposable but types don't reflect it
229
+ this . stub [ Symbol . dispose ] ?.( ) ;
230
+ }
210
231
}
0 commit comments