Repository: parcel-bundler/parcel
File: crates/parcel/src/request_tracker/request_tracker.rs (Rust)

Code before:
  request_index: HashMap<u64, NodeIndex>,
}

impl<T: Clone> Default for RequestTracker<T> {
  fn default() -> Self {
    RequestTracker::new(Vec::new())
  }
}

impl<T: Clone> RequestTracker<T> {
  pub fn new(reporters: Vec<Box<dyn ReporterPlugin>>) -> Self {
    let mut graph = StableDiGraph::<RequestNode<T>, RequestEdgeType>::new();
    graph.add_node(RequestNode::Root);
    RequestTracker {
      graph,
      reporters,
      request_index: HashMap::new(),
    }
  }

  pub fn report(&self, event: ReporterEvent) {
    let event = Arc::new(event);

    for reporter in self.reporters.iter() {
      // TODO Decide how to handle errors
      let _ = reporter.report(Arc::clone(&event));
    }
  }

  pub fn run_request(&mut self, request: &impl Request<T>) -> anyhow::Result<T> {
    self.run_child_request(request, None)
  }

  pub fn run_child_request(
    &mut self,
    request: &impl Request<T>,
    parent_request_hash: Option<u64>,
  ) -> anyhow::Result<T> {
    let request_id = request.id();

    if self.prepare_request(request_id.clone())? {
      let result = request.run(RunRequestContext::new(Some(request_id), self));
      self.store_request(&request_id, result)?;
    }

    Ok(self.get_request(parent_request_hash, &request_id)?)
  }

  fn prepare_request(&mut self, request_id: u64) -> anyhow::Result<bool> {
    let node_index = self
      .request_index

Review comment (suggestion):
We should just pass the event by reference instead of using `Arc`

`Arc` is for multi-threaded scenarios ; for single-threaded reference counting we can use `Rc`

But for this use-case we can be passing the events by reference into the reporters.

Code after:
  request_index: HashMap<u64, NodeIndex>,
}

impl<T: Clone> Default for RequestTracker<T> {
  fn default() -> Self {
    RequestTracker::new(Vec::new())
  }
}

impl<T: Clone> RequestTracker<T> {
  pub fn new(reporters: Vec<Box<dyn ReporterPlugin>>) -> Self {
    let mut graph = StableDiGraph::<RequestNode<T>, RequestEdgeType>::new();
    graph.add_node(RequestNode::Root);
    RequestTracker {
      graph,
      reporters,
      request_index: HashMap::new(),
    }
  }

  pub fn report(&self, event: ReporterEvent) {
    for reporter in self.reporters.iter() {
      // TODO Decide how to handle errors
      let _ = reporter.report(&event);
    }
  }

  pub fn run_request(&mut self, request: &impl Request<T>) -> anyhow::Result<T> {
    self.run_child_request(request, None)
  }

  pub fn run_child_request(
    &mut self,
    request: &impl Request<T>,
    parent_request_hash: Option<u64>,
  ) -> anyhow::Result<T> {
    let request_id = request.id();

    if self.prepare_request(request_id.clone())? {
      let result = request.run(RunRequestContext::new(Some(request_id), self));
      self.store_request(&request_id, result)?;
    }

    Ok(self.get_request(parent_request_hash, &request_id)?)
  }

  fn prepare_request(&mut self, request_id: u64) -> anyhow::Result<bool> {
    let node_index = self
      .request_index
      .entry(request_id)
      .or_insert_with(|| self.graph.add_node(RequestNode::Incomplete));