Skip to content

Commit 73d9e26

Browse files
committed
perf(core): only create client when running update
1 parent 8074cc7 commit 73d9e26

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/context.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use {
44
dependency::UpdateUrl,
55
instance::Instance,
66
packages::Packages,
7-
registry_client::{LiveRegistryClient, PackageMeta, RegistryClient, RegistryError},
7+
registry_client::{PackageMeta, RegistryClient, RegistryError},
88
specifier::Specifier,
99
version_group::VersionGroup,
1010
},
@@ -33,7 +33,7 @@ pub struct Context {
3333
/// Every package.json in the project
3434
pub packages: Packages,
3535
/// Registry client for fetching package metadata
36-
pub registry_client: Arc<dyn RegistryClient>,
36+
pub registry_client: Option<Arc<dyn RegistryClient>>,
3737
/// All updates from the npm registry which have been chosen either by the
3838
/// user via a prompt or automatically by choosing the latest version
3939
pub updates_by_internal_name: HashMap<String, Vec<Specifier>>,
@@ -44,7 +44,6 @@ pub struct Context {
4444
impl Context {
4545
pub fn create(config: Config, packages: Packages, registry_client: Option<Arc<dyn RegistryClient>>) -> Self {
4646
let mut instances = vec![];
47-
let registry_client = registry_client.unwrap_or_else(|| Arc::new(LiveRegistryClient::new()));
4847
let updates_by_internal_name = HashMap::new();
4948
let all_dependency_types = config.rcfile.get_all_dependency_types();
5049
let cli_filters = config.cli.get_filters(&packages, &all_dependency_types);
@@ -100,7 +99,7 @@ impl Context {
10099
/// Fetch every version specifier ever published for all updateable
101100
/// dependencies in the project.
102101
pub async fn fetch_all_updates(&mut self) {
103-
let client = Arc::clone(&self.registry_client);
102+
let client = Arc::clone(self.registry_client.as_ref().expect("Registry client not initialized"));
104103
let semaphore = Arc::new(Semaphore::new(self.config.rcfile.max_concurrent_requests));
105104
let progress_bars = Arc::new(MultiProgress::new());
106105
let mut handles: Vec<(String, JoinHandle<Result<PackageMeta, RegistryError>>)> = vec![];

src/main.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ use {
44
config::Config,
55
effects::{fix, format, lint, update},
66
packages::Packages,
7+
registry_client::LiveRegistryClient,
78
},
89
context::Context,
910
effects::list,
1011
log::{debug, error},
11-
std::process::exit,
12+
std::{process::exit, sync::Arc},
1213
visit_formatting::visit_formatting,
1314
visit_packages::visit_packages,
1415
};
@@ -51,6 +52,7 @@ async fn main() {
5152
logger::init(&cli);
5253

5354
let config = Config::from_cli(cli);
55+
let is_update_command = matches!(&config.cli.subcommand, Subcommand::Update);
5456

5557
debug!("Command: {:?}", config.cli.subcommand);
5658
debug!("{:#?}", config.cli);
@@ -66,7 +68,15 @@ async fn main() {
6668
len => debug!("Found {len} package.json files"),
6769
}
6870

69-
let ctx = Context::create(config, packages, None);
71+
let ctx = Context::create(
72+
config,
73+
packages,
74+
if is_update_command {
75+
Some(Arc::new(LiveRegistryClient::new()))
76+
} else {
77+
None
78+
},
79+
);
7080

7181
let _exit_code = match ctx.config.cli.subcommand {
7282
Subcommand::Fix => {

0 commit comments

Comments
 (0)