Files
servo/components/script/dom/abortcontroller.rs
Sam 62ae12b6a5 script: Pass &mut JSContext to timers, abort controller, some transfers and some streams (#42616)
I just wanted to do abort signal, but then this happend.

Testing: Just refactor, should be covered by WPT tests
Part of #40600

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2026-02-14 10:03:25 +00:00

91 lines
3.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use js::realm::CurrentRealm;
use js::rust::{HandleObject, HandleValue};
use crate::dom::abortsignal::AbortSignal;
use crate::dom::bindings::codegen::Bindings::AbortControllerBinding::AbortControllerMethods;
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::CanGc;
/// <https://dom.spec.whatwg.org/#abortcontroller>
#[dom_struct]
pub(crate) struct AbortController {
reflector_: Reflector,
/// <https://dom.spec.whatwg.org/#dom-abortcontroller-signal>
signal: Dom<AbortSignal>,
}
impl AbortController {
/// <https://dom.spec.whatwg.org/#dom-abortcontroller-abortcontroller>
fn new_inherited(signal: &AbortSignal) -> AbortController {
// Note: continuation of the constructor steps.
// Step 2. Set thiss signal to signal.
AbortController {
reflector_: Reflector::new(),
signal: Dom::from_ref(signal),
}
}
/// <https://dom.spec.whatwg.org/#dom-abortcontroller-abortcontroller>
pub(crate) fn new_with_proto(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
) -> DomRoot<AbortController> {
// Step 1. Let signal be a new AbortSignal object.
let signal = AbortSignal::new_with_proto(global, None, can_gc);
// Step 2. Set thiss signal to signal.
reflect_dom_object_with_proto(
Box::new(AbortController::new_inherited(&signal)),
global,
proto,
can_gc,
)
}
/// <https://dom.spec.whatwg.org/#abortcontroller-signal-abort>
pub(crate) fn signal_abort(&self, cx: &mut CurrentRealm, reason: HandleValue) {
// To signal abort on an AbortController controller with an optional reason,
// signal abort on controllers signal with reason if it is given.
self.signal.signal_abort(cx, reason);
}
/// <https://dom.spec.whatwg.org/#abortcontroller-signal>
pub(crate) fn signal(&self) -> DomRoot<AbortSignal> {
// The signal getter steps are to return thiss signal.
self.signal.as_rooted()
}
}
impl AbortControllerMethods<crate::DomTypeHolder> for AbortController {
/// <https://dom.spec.whatwg.org/#dom-abortcontroller-abortcontroller>
fn Constructor(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
) -> DomRoot<AbortController> {
AbortController::new_with_proto(global, proto, can_gc)
}
/// <https://dom.spec.whatwg.org/#dom-abortcontroller-abort>
fn Abort(&self, cx: &mut CurrentRealm, reason: HandleValue) {
// The abort(reason) method steps are
// to signal abort on this with reason if it is given.
self.signal_abort(cx, reason);
}
/// <https://dom.spec.whatwg.org/#dom-abortcontroller-signal>
fn Signal(&self) -> DomRoot<AbortSignal> {
// The signal getter steps are to return thiss signal.
self.signal.as_rooted()
}
}