@@ -35,7 +35,7 @@ pub struct ReadableStreamDefaultController {
35
35
/// All algoritems packed together:
36
36
/// - Close algorithm: A promise-returning algorithm, taking one argument (the cancel reason), which communicates a requested cancelation to the underlying source
37
37
/// - Pull algorithm: A promise-returning algorithm that pulls data from the underlying source
38
- algorithms : DomRefCell < Option < ControllerAlgorithms > > ,
38
+ algorithms : DomRefCell < ControllerAlgorithms > ,
39
39
/// A boolean flag indicating whether the stream has been closed by its underlying source, but still has chunks in its internal queue that have not yet been read
40
40
close_requested : Cell < bool > ,
41
41
/// A boolean flag set to true if the stream’s mechanisms requested a call to the underlying source's pull algorithm to pull more data, but the pull could not yet be done since a previous call is still executing
@@ -68,7 +68,7 @@ impl ReadableStreamDefaultController {
68
68
pulling : Cell :: new ( false ) ,
69
69
started : Cell :: new ( false ) ,
70
70
strategy_highwatermark : Cell :: new ( 0. ) ,
71
- algorithms : DomRefCell :: new ( None ) ,
71
+ algorithms : DomRefCell :: new ( ControllerAlgorithms :: Undefined ) ,
72
72
strategy_size_algorithm : DomRefCell :: new ( None ) ,
73
73
stream,
74
74
}
@@ -78,9 +78,6 @@ impl ReadableStreamDefaultController {
78
78
reflect_dom_object ( Box :: new ( Self :: new_inherited ( stream) ) , global)
79
79
}
80
80
81
- /// <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
82
- fn call_pull_if_needed ( & self ) { }
83
-
84
81
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-should-call-pull>
85
82
fn should_call_pull ( & self ) -> bool {
86
83
// Step 1
@@ -130,6 +127,11 @@ impl ReadableStreamDefaultController {
130
127
} ,
131
128
}
132
129
}
130
+
131
+ /// <https://streams.spec.whatwg.org/#readable-stream-default-controller-error>
132
+ fn error ( & self , e : SafeHandleValue ) {
133
+ // TODO
134
+ }
133
135
}
134
136
135
137
impl ReadableStreamDefaultControllerMethods for ReadableStreamDefaultController {
@@ -197,8 +199,7 @@ fn set_up_readable_stream_default_controller(
197
199
* controller. strategy_size_algorithm . borrow_mut ( ) = Some ( size_algorithm) ;
198
200
controller. strategy_highwatermark . set ( highwatermark) ;
199
201
// Step 6 & 7
200
- * controller. algorithms . borrow_mut ( ) = Some ( algorithms) ;
201
- //
202
+ * controller. algorithms . borrow_mut ( ) = algorithms;
202
203
// Step 8
203
204
controller
204
205
. stream
@@ -207,7 +208,7 @@ fn set_up_readable_stream_default_controller(
207
208
) ) ;
208
209
// Step 9
209
210
rooted ! ( in( * cx) let mut start_result = UndefinedValue ( ) ) ;
210
- controller. algorithms . borrow ( ) . as_ref ( ) . unwrap ( ) . start (
211
+ controller. algorithms . borrow ( ) . start (
211
212
cx,
212
213
ReadableStreamController :: ReadableStreamDefaultController ( controller. clone ( ) ) ,
213
214
start_result. handle_mut ( ) ,
@@ -239,43 +240,93 @@ fn set_up_readable_stream_default_controller(
239
240
}
240
241
241
242
impl Callback for ResolveHandler {
242
- fn callback ( & self , cx : SafeJSContext , v : SafeHandleValue , realm : InRealm ) {
243
+ fn callback ( & self , cx : SafeJSContext , _v : SafeHandleValue , _realm : InRealm ) {
243
244
// Step 11.1
244
245
self . controller . started . set ( true ) ;
245
246
// Step 11.2
246
247
assert ! ( !self . controller. pulling. get( ) ) ;
247
248
// Step 11.3
248
249
assert ! ( !self . controller. pull_again. get( ) ) ;
249
250
// Step 11.4
250
- self . controller . call_pull_if_needed ( ) ;
251
+ assert ! ( readable_stream_default_controller_call_pull_if_needed(
252
+ cx,
253
+ self . controller. clone( )
254
+ )
255
+ . is_ok( ) ) ;
256
+ }
257
+ }
258
+
259
+ Ok ( ( ) )
260
+ }
261
+
262
+ /// <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
263
+ fn readable_stream_default_controller_call_pull_if_needed (
264
+ cx : SafeJSContext ,
265
+ controller : DomRoot < ReadableStreamDefaultController > ,
266
+ ) -> Fallible < ( ) > {
267
+ // Step 1 & 2
268
+ if controller. should_call_pull ( ) {
269
+ // Step 3
270
+ if controller. pulling . get ( ) {
271
+ controller. pull_again . set ( true ) ;
272
+ } else {
273
+ // Step 4
274
+ assert ! ( !controller. pull_again. get( ) ) ;
275
+ // Step 5
276
+ controller. pulling . set ( true ) ;
277
+ // Step 6
278
+ let pull_promise = controller. algorithms . borrow ( ) . pull (
279
+ cx,
280
+ ReadableStreamController :: ReadableStreamDefaultController ( controller. clone ( ) ) ,
281
+ ) ?;
282
+ let global = & * controller. global ( ) ;
283
+ let realm = enter_realm ( & * global) ;
284
+ let comp = InRealm :: Entered ( & realm) ;
285
+ pull_promise. append_native_handler (
286
+ & PromiseNativeHandler :: new (
287
+ global,
288
+ Some ( ResolveHandler :: new ( controller. clone ( ) ) ) ,
289
+ Some ( RejectHandler :: new ( controller) ) ,
290
+ ) ,
291
+ comp,
292
+ ) ;
251
293
}
252
294
}
253
295
254
296
#[ derive( JSTraceable , MallocSizeOf ) ]
255
- struct RejectHandler {
297
+ struct ResolveHandler {
256
298
controller : DomRoot < ReadableStreamDefaultController > ,
257
299
}
258
300
259
- impl RejectHandler {
301
+ impl ResolveHandler {
260
302
pub fn new ( controller : DomRoot < ReadableStreamDefaultController > ) -> Box < dyn Callback > {
261
303
Box :: new ( Self { controller } )
262
304
}
263
305
}
264
306
265
- impl Callback for RejectHandler {
266
- fn callback ( & self , cx : SafeJSContext , v : SafeHandleValue , realm : InRealm ) {
267
- todo ! ( )
307
+ impl Callback for ResolveHandler {
308
+ fn callback ( & self , cx : SafeJSContext , _v : SafeHandleValue , _realm : InRealm ) {
309
+ // Step 7.1
310
+ self . controller . pulling . set ( false ) ;
311
+ // Step 7.2
312
+ if self . controller . pull_again . get ( ) {
313
+ self . controller . pull_again . set ( false ) ;
314
+ assert ! ( readable_stream_default_controller_call_pull_if_needed(
315
+ cx,
316
+ self . controller. clone( )
317
+ )
318
+ . is_ok( ) ) ;
319
+ }
268
320
}
269
321
}
270
-
271
322
Ok ( ( ) )
272
323
}
273
324
274
325
/// Algorithms for [setup_readable_stream_default_controller_from_underlying_source]
275
326
#[ derive( JSTraceable , MallocSizeOf ) ]
276
327
pub enum ControllerAlgorithms {
277
328
UnderlyingSource ( UnderlyingSourceAlgorithms ) ,
278
- None ,
329
+ Undefined ,
279
330
}
280
331
281
332
impl ControllerAlgorithms {
@@ -287,7 +338,7 @@ impl ControllerAlgorithms {
287
338
) -> Fallible < ( ) > {
288
339
match self {
289
340
ControllerAlgorithms :: UnderlyingSource ( s) => s. start ( cx, controller, retval) ,
290
- ControllerAlgorithms :: None => unreachable ! ( ) ,
341
+ ControllerAlgorithms :: Undefined => unreachable ! ( ) ,
291
342
}
292
343
}
293
344
@@ -298,14 +349,14 @@ impl ControllerAlgorithms {
298
349
) -> Fallible < Rc < Promise > > {
299
350
match self {
300
351
ControllerAlgorithms :: UnderlyingSource ( s) => s. pull ( cx, controller) ,
301
- ControllerAlgorithms :: None => unreachable ! ( ) ,
352
+ ControllerAlgorithms :: Undefined => unreachable ! ( ) ,
302
353
}
303
354
}
304
355
305
356
fn cancel ( & self , cx : SafeJSContext , reason : Option < HandleValue > ) -> Fallible < Rc < Promise > > {
306
357
match self {
307
358
ControllerAlgorithms :: UnderlyingSource ( s) => s. cancel ( cx, reason) ,
308
- ControllerAlgorithms :: None => unreachable ! ( ) ,
359
+ ControllerAlgorithms :: Undefined => unreachable ! ( ) ,
309
360
}
310
361
}
311
362
}
@@ -390,3 +441,20 @@ impl UnderlyingSourceAlgorithms {
390
441
}
391
442
}
392
443
}
444
+
445
+ #[ derive( JSTraceable , MallocSizeOf ) ]
446
+ struct RejectHandler {
447
+ controller : DomRoot < ReadableStreamDefaultController > ,
448
+ }
449
+
450
+ impl RejectHandler {
451
+ pub fn new ( controller : DomRoot < ReadableStreamDefaultController > ) -> Box < dyn Callback > {
452
+ Box :: new ( Self { controller } )
453
+ }
454
+ }
455
+
456
+ impl Callback for RejectHandler {
457
+ fn callback ( & self , cx : SafeJSContext , v : SafeHandleValue , realm : InRealm ) {
458
+ self . controller . error ( v) ;
459
+ }
460
+ }
0 commit comments