Skip to content

Commit a0e8794

Browse files
committed
Using borrow for iterating registration
Signed-off-by: stevennovaryo <[email protected]>
1 parent 3a97171 commit a0e8794

File tree

3 files changed

+11
-45
lines changed

3 files changed

+11
-45
lines changed

components/script/dom/element.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ use xml5ever::serialize::TraversalScope::{
6565

6666
use super::customelementregistry::is_valid_custom_element_name;
6767
use super::htmltablecolelement::{HTMLTableColElement, HTMLTableColElementLayoutHelpers};
68-
use super::intersectionobserver::{
69-
IntersectionObserver, IntersectionObserverRegistration,
70-
};
68+
use super::intersectionobserver::{IntersectionObserver, IntersectionObserverRegistration};
7169
use crate::dom::activation::Activatable;
7270
use crate::dom::attr::{Attr, AttrHelpersForLayout};
7371
use crate::dom::bindings::cell::{ref_filter_map, DomRefCell, Ref, RefMut};
@@ -644,42 +642,20 @@ impl Element {
644642
}))
645643
}
646644

647-
pub(crate) fn get_intersection_observer_registration<'root>(
645+
pub(crate) fn get_intersection_observer_registration(
648646
&self,
649647
observer: &IntersectionObserver,
650-
cx: JSContext,
651-
unrooted: &'root mut Rooted<IntersectionObserverRegistration>,
652-
) -> Option<RootedGuard<'root, IntersectionObserverRegistration>> {
648+
) -> Option<Ref<IntersectionObserverRegistration>> {
653649
if let Some(registrations) = self.registered_intersection_observers() {
654650
registrations
655651
.iter()
656652
.position(|reg_obs| reg_obs.observer == observer)
657-
.map(|index: usize| {
658-
RootedGuard::new(
659-
*cx,
660-
unrooted,
661-
registrations[index].clone(),
662-
)})
653+
.map(|index| Ref::map(registrations, |registrations| &registrations[index]))
663654
} else {
664655
None
665656
}
666657
}
667658

668-
/// Update registration that has a same observer
669-
pub(crate) fn update_intersection_observer_registration(
670-
&self,
671-
registration: &IntersectionObserverRegistration,
672-
) {
673-
let mut registrations = self.registered_intersection_observers_mut();
674-
675-
if let Some(index) = registrations
676-
.iter_mut()
677-
.position(|reg_obs| reg_obs.observer == registration.observer)
678-
{
679-
registrations[index] = registration.clone();
680-
}
681-
}
682-
683659
/// Add a new IntersectionObserverRegistration to the element.
684660
pub(crate) fn add_initial_intersection_observer_registration(
685661
&self,

components/script/dom/intersectionobserver.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44

55
use std::cell::{Cell, RefCell};
6-
use std::ops::Deref;
76
use std::rc::Rc;
87
use std::time::Duration;
98

@@ -12,7 +11,6 @@ use base::cross_process_instant::CrossProcessInstant;
1211
use cssparser::{Parser, ParserInput};
1312
use dom_struct::dom_struct;
1413
use euclid::default::Rect;
15-
use js::jsapi::Rooted;
1614
use js::rust::{HandleObject, MutableHandleValue};
1715
use style::context::QuirksMode;
1816
use style::parser::{Parse, ParserContext};
@@ -26,7 +24,6 @@ use super::bindings::codegen::Bindings::IntersectionObserverBinding::{
2624
};
2725
use super::document::Document;
2826
use super::domrectreadonly::DOMRectReadOnly;
29-
use super::globalscope::GlobalScope;
3027
use super::intersectionobserverentry::IntersectionObserverEntry;
3128
use super::intersectionobserverrootmargin::IntersectionObserverRootMargin;
3229
use super::node::{Node, NodeTraits};
@@ -417,10 +414,7 @@ impl IntersectionObserver {
417414
/// scrollbar for element or document. However, the behaviour is not clearly defined in the spec.
418415
///
419416
/// <https://w3c.github.io/IntersectionObserver/#intersectionobserver-root-intersection-rectangle>
420-
pub(crate) fn root_intersection_rectangle(
421-
&self,
422-
document: &Document,
423-
) -> Rect<Au> {
417+
pub(crate) fn root_intersection_rectangle(&self, document: &Document) -> Rect<Au> {
424418
let intersection_rectangle = match &self.root {
425419
// > If the IntersectionObserver is an implicit root observer,
426420
None => {
@@ -489,11 +483,7 @@ impl IntersectionObserver {
489483
// > Let registration be the IntersectionObserverRegistration record in target’s internal
490484
// > [[RegisteredIntersectionObservers]] slot whose observer property is equal to observer.
491485
// We will clone now to prevent borrow error, and set the value in the end of the loop.
492-
let cx = GlobalScope::get_cx();
493-
let mut unrooted = Rooted::new_unrooted();
494-
let registration = target
495-
.get_intersection_observer_registration(self, cx, &mut unrooted)
496-
.unwrap();
486+
let registration = target.get_intersection_observer_registration(self).unwrap();
497487

498488
// Step 2
499489
// > If (time - registration.lastUpdateTime < observer.delay), skip further processing for target.
@@ -551,7 +541,9 @@ impl IntersectionObserver {
551541
// This is what we are currently using for getBoundingBox(). However, it is not correct,
552542
// mainly because it is not considering transform and scroll offset.
553543
// TODO: replace this once getBoundingBox() is implemented correctly.
554-
target_rect = target.upcast::<Node>().bounding_content_box_or_zero_no_reflow();
544+
target_rect = target
545+
.upcast::<Node>()
546+
.bounding_content_box_or_zero_no_reflow();
555547

556548
// Step 8
557549
// > Let intersectionRect be the result of running the compute the intersection algorithm on
@@ -656,9 +648,6 @@ impl IntersectionObserver {
656648
registration.previous_threshold_index.set(threshold_index);
657649
registration.previous_is_intersecting.set(is_intersecting);
658650
registration.previous_is_visible.set(is_visible);
659-
660-
// Update the registration inside the element.
661-
target.update_intersection_observer_registration(registration.deref());
662651
}
663652
}
664653
}

components/script/dom/node.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,8 @@ impl Node {
855855
}
856856

857857
pub(crate) fn bounding_content_box_or_zero_no_reflow(&self) -> Rect<Au> {
858-
self.bounding_content_box_no_reflow().unwrap_or_else(Rect::zero)
858+
self.bounding_content_box_no_reflow()
859+
.unwrap_or_else(Rect::zero)
859860
}
860861

861862
pub(crate) fn content_boxes(&self, can_gc: CanGc) -> Vec<Rect<Au>> {

0 commit comments

Comments
 (0)