1
1
import { primordials , core } from "ext:core/mod.js" ;
2
2
import { readableStreamForRid , writableStreamForRid } from "ext:deno_web/06_streams.js" ;
3
3
import { getSupabaseTag } from "ext:sb_core_main_js/js/http.js" ;
4
+ import { SymbolDispose } from "ext:deno_web/00_infra.js" ;
4
5
5
6
const ops = core . ops ;
6
7
@@ -9,6 +10,8 @@ const { TypeError } = primordials;
9
10
const {
10
11
op_user_worker_fetch_send,
11
12
op_user_worker_create,
13
+ op_user_user_worker_wait_token_cancelled,
14
+ op_user_worker_is_active,
12
15
} = ops ;
13
16
14
17
const NO_SUPABASE_TAG_WARN_MSG = `Unable to find the supabase tag from the request instance.\n\
@@ -24,8 +27,33 @@ function redirectStatus(status) {
24
27
}
25
28
26
29
class UserWorker {
27
- constructor ( key ) {
28
- this . key = key ;
30
+ /** @type {string } */
31
+ #key = "" ;
32
+
33
+ /** @type {number | null } */
34
+ #rid = null ;
35
+
36
+ /** @type {boolean } */
37
+ #disposed = false ;
38
+
39
+ /**
40
+ * @param {string } key
41
+ * @param {number } rid
42
+ */
43
+ constructor ( key , rid ) {
44
+ this . #key = key ;
45
+ this . #rid = rid ;
46
+
47
+ const self = this ;
48
+
49
+ setTimeout ( async ( ) => {
50
+ try {
51
+ await op_user_user_worker_wait_token_cancelled ( rid ) ;
52
+ self . dispose ( ) ;
53
+ } catch {
54
+ // TODO(Nyannyacha): Link it with the tracing for telemetry.
55
+ }
56
+ } ) ;
29
57
}
30
58
31
59
async fetch ( request , options = { } ) {
@@ -63,7 +91,7 @@ class UserWorker {
63
91
}
64
92
65
93
const responsePromise = op_user_worker_fetch_send (
66
- this . key ,
94
+ this . # key,
67
95
requestRid ,
68
96
requestBodyRid ,
69
97
tag . streamRid ,
@@ -76,6 +104,7 @@ class UserWorker {
76
104
] ) ;
77
105
78
106
if ( requestBodyPromiseResult . status === "rejected" ) {
107
+ // TODO(Nyannyacha): Link it with the tracing for telemetry.
79
108
// console.warn(requestBodyPromiseResult.reason);
80
109
}
81
110
@@ -115,6 +144,26 @@ class UserWorker {
115
144
} ) ;
116
145
}
117
146
147
+ /** @returns {boolean } */
148
+ get active ( ) {
149
+ if ( this . #disposed) {
150
+ return false ;
151
+ }
152
+
153
+ return op_user_worker_is_active ( this . #rid) ;
154
+ }
155
+
156
+ dispose ( ) {
157
+ if ( ! this . #disposed) {
158
+ core . tryClose ( this . #rid) ;
159
+ this . #disposed = true ;
160
+ }
161
+ }
162
+
163
+ [ SymbolDispose ] ( ) {
164
+ this . dispose ( ) ;
165
+ }
166
+
118
167
static async create ( opts ) {
119
168
const readyOptions = {
120
169
memoryLimitMb : 512 ,
@@ -142,9 +191,9 @@ class UserWorker {
142
191
throw new TypeError ( "service path must be defined" ) ;
143
192
}
144
193
145
- const key = await op_user_worker_create ( readyOptions ) ;
194
+ const [ key , rid ] = await op_user_worker_create ( readyOptions ) ;
146
195
147
- return new UserWorker ( key ) ;
196
+ return new UserWorker ( key , rid ) ;
148
197
}
149
198
}
150
199
0 commit comments