Repository: servo/servo
File: components/script/dom/document.rs (Rust)

Code before:
        //
        // At least time seconds have elapsed since document's completely loaded time,
        // adjusted to take into account user or user agent preferences.
        if let Some(DeclarativeRefresh::PendingLoad { url, time }) =
            &*self.declarative_refresh.borrow()
        {
            self.window.as_global_scope().schedule_callback(
                OneshotTimerCallback::RefreshRedirectDue(RefreshRedirectDue {
                    window: DomRoot::from_ref(self.window()),
                    url: url.clone(),
                }),
                Duration::from_secs(*time),
            );
        }
    }

    /// Step 9 of <https://html.spec.whatwg.org/multipage/#the-end>
    fn queue_document_completion(&self) {
        self.loader.borrow_mut().inhibit_events();

        // The rest will ever run only once per document.

        // Step 9. Queue a global task on the DOM manipulation task source given
        // the Document's relevant global object to run the following steps:
        debug!("Document loads are complete.");
        let document = Trusted::new(self);
        self.owner_global()
            .task_manager()
            .dom_manipulation_task_source()
            .queue(task!(fire_load_event: move || {
                let document = document.root();
                // Step 9.3. Let window be the Document's relevant global object.
                let window = document.window();
                if !window.is_alive() {
                    return;
                }

                // Step 9.1. Update the current document readiness to "complete".
                document.set_ready_state(DocumentReadyState::Complete, CanGc::note());

                // Step 9.2. If the Document object's browsing context is null, then abort these steps.
                if document.browsing_context().is_none() {
                    return;
                }

                // Step 9.4. Set the Document's load timing info's load event start time to the current high resolution time given window.
                update_with_current_instant(&document.load_event_start);

                // Step 9.5. Fire an event named load at window, with legacy target override flag set.
                let load_event = Event::new(
                    window.upcast(),

Review comment (bug):
This is the main benefit: we don't have these convulated checks for various pieces anymore. Additionally, we no longer need to go via the script_thread, which had different preconditions which were also wrong.

Code after:
        // adjusted to take into account user or user agent preferences.
        if let Some(DeclarativeRefresh::PendingLoad { url, time }) =
            &*self.declarative_refresh.borrow()
        {
            self.window.as_global_scope().schedule_callback(
                OneshotTimerCallback::RefreshRedirectDue(RefreshRedirectDue {
                    window: DomRoot::from_ref(self.window()),
                    url: url.clone(),
                }),
                Duration::from_secs(*time),
            );
        }
    }

    /// Step 9 of <https://html.spec.whatwg.org/multipage/#the-end>
    fn queue_document_completion(&self) {
        self.loader.borrow_mut().inhibit_events();

        // The rest will ever run only once per document.

        // Step 9. Queue a global task on the DOM manipulation task source given
        // the Document's relevant global object to run the following steps:
        debug!("Document loads are complete.");
        let document = Trusted::new(self);
        self.owner_global()
            .task_manager()
            .dom_manipulation_task_source()
            .queue(task!(fire_load_event: move || {
                let document = document.root();
                // Step 9.3. Let window be the Document's relevant global object.
                let window = document.window();
                if !window.is_alive() {
                    return;
                }

                // Step 9.1. Update the current document readiness to "complete".
                document.set_ready_state(DocumentReadyState::Complete, CanGc::note());

                // Step 9.2. If the Document object's browsing context is null, then abort these steps.
                if document.browsing_context().is_none() {
                    return;
                }

                // Step 9.4. Set the Document's load timing info's load event start time to the current high resolution time given window.
                update_with_current_instant(&document.load_event_start);

                // Step 9.5. Fire an event named load at window, with legacy target override flag set.
                let load_event = Event::new(
                    window.upcast(),
                    atom!("load"),
                    EventBubbles::DoesNotBubble,