Skip to content

Overhaul stats: Extract events package (part 1) #1478

Closed

Description

@josecelano

Relates to:

The Keeper type is responsible for:

  • Task 1. Creating the event broadcast channel.
  • Task 2. Creating the sender.
  • Task 3. Creating the receivers.
  • Task 4. Run the event listener to update the stats.

Since our goal is to decouple events from stats, I will:

  • Remove Task 4 from the Keeper.
  • Extract a new package with shared code for events, excluding logic for statistics.

I think we can also rename the keeper, the final code would be:

use std::sync::Arc;

use tokio::sync::broadcast::Receiver;

use crate::event::sender::{self, Broadcaster};
use crate::event::Event;

pub struct Keeper {
    pub enable_sender: bool,
    pub broadcaster: Broadcaster,
}

impl Default for Keeper {
    fn default() -> Self {
        let enable_sender = true;
        let broadcaster = Broadcaster::default();

        Self::new(enable_sender, broadcaster)
    }
}

impl Keeper {
    #[must_use]
    pub fn new(enable_sender: bool, broadcaster: Broadcaster) -> Self {
        Self {
            enable_sender,
            broadcaster,
        }
    }

    #[must_use]
    pub fn sender(&self) -> Arc<Option<Box<dyn sender::Sender>>> {
        if self.enable_sender {
            Arc::new(Some(Box::new(self.broadcaster.clone())))
        } else {
            Arc::new(None)
        }
    }

    #[must_use]
    pub fn receiver(&self) -> Receiver<Event> {
        self.broadcaster.subscribe()
    }
}

I think a good name could be "EventBus".

cc @da2ce7

Activity

  1. added
    - Developer -
    Code Cleanup / Refactoring
    on Apr 25, 2025
  2. self-assigned this
    on Apr 25, 2025
  3. added theissue type on Apr 25, 2025
  4. added a commit that references this issue on Apr 25, 2025
  5. added 10 commits that reference this issue on Apr 25, 2025
  6. changed the title [-]Overhaul stats: Extract events package[/-] [+]Overhaul stats: Extract events package (part 1)[/+] on Apr 25, 2025
  7. added a commit that references this issue on Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

- Developer -Code Cleanup / Refactoring

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions