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

Code before:
        let client_context_id =
            ClientContextId::build(pipeline_id.namespace_id.0, pipeline_id.index.0.get());
        let player = ServoMedia::get().create_player(
            &client_context_id,
            stream_type,
            action_sender,
            video_renderer,
            audio_renderer,
            Box::new(window.get_player_context()),
        );
        let player_id = {
            let player_guard = player.lock().unwrap();

            if let Err(error) = player_guard.set_mute(self.muted.get()) {
                warn!("Could not set mute state: {error:?}");
            }

            player_guard.get_id()
        };

        *self.player.borrow_mut() = Some(player);

        let event_handler = Arc::new(Mutex::new(HTMLMediaElementEventHandler::new(self)));

        let weak_event_handler = Arc::downgrade(&event_handler);

        *self.event_handler.borrow_mut() = Some(event_handler);

        let task_source = self
            .owner_global()
            .task_manager()
            .media_element_task_source()
            .to_sendable();
        ROUTER.add_typed_route(
            action_receiver,
            Box::new(move |message| {
                let event = message.unwrap();
                let weak_event_handler = weak_event_handler.clone();

                task_source.queue(task!(handle_player_event: move || {
                    trace!("Player event {:?}", event);

                    let Some(event_handler) = weak_event_handler.upgrade() else {
                        return;
                    };

                    event_handler.lock().unwrap().handle_player_event(player_id, event, CanGc::note());
                }));
            }),
        );


Review comment (suggestion):
```suggestion
        let event_handler = Arc::new(Mutex::new(HTMLMediaElementEventHandler::new(self)));
        let weak_event_handler = Arc::downgrade(&event_handler);
        *self.event_handler.borrow_mut() = Some(event_handler);

```

Code after:
        let client_context_id =
            ClientContextId::build(pipeline_id.namespace_id.0, pipeline_id.index.0.get());
        let player = ServoMedia::get().create_player(
            &client_context_id,
            stream_type,
            action_sender,
            video_renderer,
            audio_renderer,
            Box::new(window.get_player_context()),
        );
        let player_id = {
            let player_guard = player.lock().unwrap();

            if let Err(error) = player_guard.set_mute(self.muted.get()) {
                warn!("Could not set mute state: {error:?}");
            }

            player_guard.get_id()
        };

        *self.player.borrow_mut() = Some(player);

        let event_handler = Arc::new(Mutex::new(HTMLMediaElementEventHandler::new(self)));
        let weak_event_handler = Arc::downgrade(&event_handler);
        *self.event_handler.borrow_mut() = Some(event_handler);

        let task_source = self
            .owner_global()
            .task_manager()
            .media_element_task_source()
            .to_sendable();
        ROUTER.add_typed_route(
            action_receiver,
            Box::new(move |message| {
                let event = message.unwrap();
                let weak_event_handler = weak_event_handler.clone();

                task_source.queue(task!(handle_player_event: move || {
                    trace!("HTMLMediaElement event: {event:?}");

                    let Some(event_handler) = weak_event_handler.upgrade() else {
                        return;
                    };

                    event_handler.lock().unwrap().handle_player_event(player_id, event, CanGc::note());
                }));
            }),
        );

        let task_source = self
            .owner_global()