Skip to content

Commit 87ea4bb

Browse files
authored
feat: add on_resolve hook to Reporter (#603)
1 parent 58e22f0 commit 87ea4bb

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

src/graph.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4806,6 +4806,9 @@ impl<'a, 'graph> Builder<'a, 'graph> {
48064806
name: package_req.name.clone(),
48074807
version,
48084808
};
4809+
if let Some(reporter) = &self.reporter {
4810+
reporter.on_resolve(package_req, &package_nv);
4811+
}
48094812
self
48104813
.graph
48114814
.packages

src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,11 @@ mod tests {
179179

180180
use super::*;
181181
use deno_error::JsErrorBox;
182+
use deno_semver::package::PackageNv;
183+
use deno_semver::package::PackageReq;
182184
use indexmap::IndexMap;
183185
use indexmap::IndexSet;
186+
use parking_lot::Mutex;
184187
use pretty_assertions::assert_eq;
185188
use serde_json::json;
186189
use source::tests::MockResolver;
@@ -189,7 +192,6 @@ mod tests {
189192
use source::ResolutionMode;
190193
use source::Source;
191194
use source::DEFAULT_JSX_IMPORT_SOURCE_MODULE;
192-
use std::cell::RefCell;
193195
use std::collections::BTreeMap;
194196

195197
type Sources<'a> = Vec<(&'a str, Source<&'a str>)>;
@@ -2590,9 +2592,10 @@ export const foo = 'bar';"#,
25902592
);
25912593
}
25922594

2593-
#[derive(Debug)]
2595+
#[derive(Debug, Default)]
25942596
struct CollectingReporter {
2595-
on_loads: RefCell<Vec<(ModuleSpecifier, usize, usize)>>,
2597+
on_loads: Mutex<Vec<(ModuleSpecifier, usize, usize)>>,
2598+
on_resolves: Mutex<Vec<(PackageReq, PackageNv)>>,
25962599
}
25972600

25982601
impl source::Reporter for CollectingReporter {
@@ -2602,12 +2605,19 @@ export const foo = 'bar';"#,
26022605
modules_done: usize,
26032606
modules_total: usize,
26042607
) {
2605-
self.on_loads.borrow_mut().push((
2608+
self.on_loads.lock().push((
26062609
specifier.clone(),
26072610
modules_done,
26082611
modules_total,
26092612
));
26102613
}
2614+
2615+
fn on_resolve(&self, req: &PackageReq, package_nv: &PackageNv) {
2616+
self
2617+
.on_resolves
2618+
.lock()
2619+
.push((req.clone(), package_nv.clone()));
2620+
}
26112621
}
26122622

26132623
#[tokio::test]
@@ -2661,9 +2671,7 @@ export const foo = 'bar';"#,
26612671
);
26622672
let root_specifier =
26632673
ModuleSpecifier::parse("file:///a/test01.ts").expect("bad url");
2664-
let reporter = CollectingReporter {
2665-
on_loads: RefCell::new(vec![]),
2666-
};
2674+
let reporter = CollectingReporter::default();
26672675
let mut graph = ModuleGraph::new(GraphKind::All);
26682676
graph
26692677
.build(

src/source/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,8 @@ impl Loader for MemoryLoader {
770770

771771
/// A trait which can be used to allow the module graph to report status events
772772
/// to the user.
773-
pub trait Reporter: fmt::Debug {
773+
pub trait Reporter: fmt::Debug + Send + Sync {
774+
#[allow(unused_variables)]
774775
/// A handler that is called after each load of a module. It contains the
775776
/// module specifier of the module that was loaded, and the number of modules
776777
/// seen (total number of unique specifiers seen), and the number of modules
@@ -781,7 +782,14 @@ pub trait Reporter: fmt::Debug {
781782
specifier: &ModuleSpecifier,
782783
modules_done: usize,
783784
modules_total: usize,
784-
);
785+
) {
786+
}
787+
788+
#[allow(unused_variables)]
789+
/// A handler that is called after each resolution of a package requirement.
790+
/// It contains the package requirement and the package name and version that
791+
/// was resolved.
792+
fn on_resolve(&self, req: &PackageReq, package_nv: &PackageNv) {}
785793
}
786794

787795
/// Resolve a media type and optionally the charset from a module specifier and

0 commit comments

Comments
 (0)