Skip to content

Commit 94bae1d

Browse files
committed
Add Option to algorithm fields since they can be undefined
1 parent 8beb3f7 commit 94bae1d

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

components/script/dom/readablestreamdefaultcontroller.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct ReadableStreamDefaultController {
3535
/// All algoritems packed together:
3636
/// - Close algorithm: A promise-returning algorithm, taking one argument (the cancel reason), which communicates a requested cancelation to the underlying source
3737
/// - Pull algorithm: A promise-returning algorithm that pulls data from the underlying source
38-
algorithms: ControllerAlgorithms,
38+
algorithms: DomRefCell<Option<ControllerAlgorithms>>,
3939
/// 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
4040
close_requested: Cell<bool>,
4141
/// 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
@@ -53,17 +53,13 @@ pub struct ReadableStreamDefaultController {
5353
///
5454
/// If missing use default value (1) per https://streams.spec.whatwg.org/#make-size-algorithm-from-size-function
5555
#[ignore_malloc_size_of = "Rc is hard"]
56-
strategy_size_algorithm: Rc<QueuingStrategySize>,
56+
strategy_size_algorithm: DomRefCell<Option<Rc<QueuingStrategySize>>>,
5757
/// The ReadableStream instance controlled
5858
stream: DomRoot<ReadableStream>,
5959
}
6060

6161
impl ReadableStreamDefaultController {
62-
fn new_inherited(
63-
algorithms: ControllerAlgorithms,
64-
size: Rc<QueuingStrategySize>,
65-
stream: DomRoot<ReadableStream>,
66-
) -> Self {
62+
fn new_inherited(stream: DomRoot<ReadableStream>) -> Self {
6763
Self {
6864
reflector_: Reflector::new(),
6965
queue: Default::default(),
@@ -72,22 +68,14 @@ impl ReadableStreamDefaultController {
7268
pulling: Cell::new(false),
7369
started: Cell::new(false),
7470
strategy_highwatermark: Cell::new(0.),
75-
algorithms,
76-
strategy_size_algorithm: size,
71+
algorithms: DomRefCell::new(None),
72+
strategy_size_algorithm: DomRefCell::new(None),
7773
stream,
7874
}
7975
}
8076

81-
fn new(
82-
global: &GlobalScope,
83-
algorithms: ControllerAlgorithms,
84-
size: Rc<QueuingStrategySize>,
85-
stream: DomRoot<ReadableStream>,
86-
) -> DomRoot<Self> {
87-
reflect_dom_object(
88-
Box::new(Self::new_inherited(algorithms, size, stream)),
89-
global,
90-
)
77+
fn new(global: &GlobalScope, stream: DomRoot<ReadableStream>) -> DomRoot<Self> {
78+
reflect_dom_object(Box::new(Self::new_inherited(stream)), global)
9179
}
9280

9381
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
@@ -175,23 +163,25 @@ pub fn setup_readable_stream_default_controller_from_underlying_source(
175163
let algorithms = UnderlyingSourceAlgorithms::new(underlying_source_dict, underlying_source_obj);
176164

177165
// Step 1
178-
let controller = ReadableStreamDefaultController::new(
179-
&*stream.global(),
180-
ControllerAlgorithms::UnderlyingSource(algorithms),
181-
size_algorithm,
182-
stream,
183-
);
166+
let controller = ReadableStreamDefaultController::new(&*stream.global(), stream);
184167

185168
// Step 8
186-
set_up_readable_stream_default_controller(cx, controller, highwatermark)
169+
set_up_readable_stream_default_controller(
170+
cx,
171+
controller,
172+
ControllerAlgorithms::UnderlyingSource(algorithms),
173+
highwatermark,
174+
size_algorithm,
175+
)
187176
}
188177

189178
/// <https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller>
190179
fn set_up_readable_stream_default_controller(
191180
cx: SafeJSContext,
192-
// stream: DomRoot<ReadableStream>,
193181
mut controller: DomRoot<ReadableStreamDefaultController>,
182+
algorithms: ControllerAlgorithms,
194183
highwatermark: f64,
184+
size_algorithm: Rc<QueuingStrategySize>,
195185
) -> Fallible<()> {
196186
// Step 1
197187
assert!(controller.stream.controller().is_none());
@@ -204,9 +194,11 @@ fn set_up_readable_stream_default_controller(
204194
controller.pull_again.set(false);
205195
controller.pulling.set(false);
206196
// Step 5
207-
// Note sizeAlgorithm is set in ReadableStreamDefaultController::new already.
197+
*controller.strategy_size_algorithm.borrow_mut() = Some(size_algorithm);
208198
controller.strategy_highwatermark.set(highwatermark);
209-
// Step 6 & 7 are done in ReadableStreamDefaultController::new already.
199+
// Step 6 & 7
200+
*controller.algorithms.borrow_mut() = Some(algorithms);
201+
//
210202
// Step 8
211203
controller
212204
.stream
@@ -215,7 +207,7 @@ fn set_up_readable_stream_default_controller(
215207
));
216208
// Step 9
217209
rooted!(in(*cx) let mut start_result = UndefinedValue());
218-
controller.algorithms.start(
210+
controller.algorithms.borrow().as_ref().unwrap().start(
219211
cx,
220212
ReadableStreamController::ReadableStreamDefaultController(controller.clone()),
221213
start_result.handle_mut(),

0 commit comments

Comments
 (0)