Repository: servo/servo
File: components/background_hang_monitor/background_hang_monitor.rs (Rust)

Code before:
                (instant - self.sampling_baseline).as_millis(),
                serde_json::to_string(&profile.backtrace).unwrap(),
            );
            bytes.extend(json.as_bytes());
            first = false;
        }

        bytes.extend(b"\n] }");
        let _ = self
            .constellation_chan
            .send(HangMonitorAlert::Profile(bytes));
    }

    fn run(&mut self) -> bool {
        let tick = if let Some(duration) = self.sampling_duration {
            let duration = duration
                .checked_sub(Instant::now() - self.last_sample)
                .unwrap_or_else(|| Duration::from_millis(0));
            after(duration)
        } else if self.monitoring_enabled {
            after(Duration::from_millis(100))
        } else {
            never()
        };

        // Helper enum to collect messages from different ports depeneding
        // on the BHM state.
        enum BhmMessage {
            ComponentMessage((MonitoredComponentId, MonitoredComponentMsg)),
            ToggleSampler(Duration, Duration),
            Exit,
            ControlError(String),
            ControlDisconnected,
            Tick,
            PortDisconnected,
        }

        let result = if let Some(ref control_port) = self.control_port {
            select! {
                recv(self.port) -> event => {
                    match event {
                        Ok(msg) => BhmMessage::ComponentMessage(msg),
                        Err(_) => BhmMessage::PortDisconnected,
                    }
                },
                recv(control_port) -> event => {
                    match event {
                        Ok(Ok(BackgroundHangMonitorControlMsg::ToggleSampler(rate, max_duration))) => {
                            BhmMessage::ToggleSampler(rate, max_duration)
                        },
                        Ok(Ok(BackgroundHangMonitorControlMsg::Exit)) => BhmMessage::Exit,

Review comment (refactor):
```suggestion
        // Helper enum to collect messages from different ports depending
```

Code after:
                id,
                (instant - self.sampling_baseline).as_millis(),
                serde_json::to_string(&profile.backtrace).unwrap(),
            );
            bytes.extend(json.as_bytes());
            first = false;
        }

        bytes.extend(b"\n] }");
        let _ = self
            .constellation_chan
            .send(HangMonitorAlert::Profile(bytes));
    }

    fn run(&mut self) -> bool {
        let tick = if let Some(duration) = self.sampling_duration {
            let duration = duration
                .checked_sub(Instant::now() - self.last_sample)
                .unwrap_or_else(|| Duration::from_millis(0));
            after(duration)
        } else if self.monitoring_enabled {
            after(Duration::from_millis(100))
        } else {
            never()
        };

        // Helper enum to collect messages from different ports depending
        // on the BHM state.
        enum BhmMessage {
            ComponentMessage((MonitoredComponentId, MonitoredComponentMsg)),
            ToggleSampler(Duration, Duration),
            Exit,
            ControlError(String),
            ControlDisconnected,
            Tick,
            PortDisconnected,
        }

        let result = if let Some(ref control_port) = self.control_port {
            select! {
                recv(self.port) -> event => {
                    match event {
                        Ok(msg) => BhmMessage::ComponentMessage(msg),
                        Err(_) => BhmMessage::PortDisconnected,
                    }
                },
                recv(control_port) -> event => {
                    match event {
                        Ok(Ok(BackgroundHangMonitorControlMsg::ToggleSampler(rate, max_duration))) => {
                            BhmMessage::ToggleSampler(rate, max_duration)
                        },