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 = { } ) {
@@ -62,7 +90,7 @@ class UserWorker {
62
90
}
63
91
64
92
const responsePromise = op_user_worker_fetch_send (
65
- this . key ,
93
+ this . # key,
66
94
requestRid ,
67
95
requestBodyRid ,
68
96
tag . streamRid ,
@@ -75,6 +103,7 @@ class UserWorker {
75
103
] ) ;
76
104
77
105
if ( requestBodyPromiseResult . status === "rejected" ) {
106
+ // TODO(Nyannyacha): Link it with the tracing for telemetry.
78
107
// console.warn(requestBodyPromiseResult.reason);
79
108
}
80
109
@@ -114,6 +143,26 @@ class UserWorker {
114
143
} ) ;
115
144
}
116
145
146
+ /** @returns {boolean } */
147
+ get active ( ) {
148
+ if ( this . #disposed) {
149
+ return false ;
150
+ }
151
+
152
+ return op_user_worker_is_active ( this . #rid) ;
153
+ }
154
+
155
+ dispose ( ) {
156
+ if ( ! this . #disposed) {
157
+ core . tryClose ( this . #rid) ;
158
+ this . #disposed = true ;
159
+ }
160
+ }
161
+
162
+ [ SymbolDispose ] ( ) {
163
+ this . dispose ( ) ;
164
+ }
165
+
117
166
static async create ( opts ) {
118
167
const readyOptions = {
119
168
noModuleCache : false ,
@@ -136,9 +185,9 @@ class UserWorker {
136
185
throw new TypeError ( "service path must be defined" ) ;
137
186
}
138
187
139
- const key = await op_user_worker_create ( readyOptions ) ;
188
+ const [ key , rid ] = await op_user_worker_create ( readyOptions ) ;
140
189
141
- return new UserWorker ( key ) ;
190
+ return new UserWorker ( key , rid ) ;
142
191
}
143
192
}
144
193
0 commit comments