-
-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support custom handler of progress plugin (#7896)
- Loading branch information
1 parent
b0f8efb
commit fc3b9fa
Showing
7 changed files
with
186 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 34 additions & 16 deletions
50
crates/rspack_binding_options/src/options/raw_builtins/raw_progress.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,51 @@ | ||
use std::sync::Arc; | ||
|
||
use derivative::Derivative; | ||
use napi::Either; | ||
use napi_derive::napi; | ||
use rspack_plugin_progress::ProgressPluginOptions; | ||
use rspack_napi::threadsafe_function::ThreadsafeFunction; | ||
use rspack_plugin_progress::{ProgressPluginDisplayOptions, ProgressPluginOptions}; | ||
|
||
#[derive(Debug, Clone)] | ||
#[napi(object)] | ||
type HandlerFn = ThreadsafeFunction<(f64, String, Vec<String>), ()>; | ||
#[derive(Derivative)] | ||
#[derivative(Debug)] | ||
#[napi(object, object_to_js = false)] | ||
pub struct RawProgressPluginOptions { | ||
// the prefix name of progress bar | ||
pub prefix: String, | ||
pub prefix: Option<String>, | ||
// tells ProgressPlugin to collect profile data for progress steps. | ||
pub profile: bool, | ||
pub profile: Option<bool>, | ||
// the template of progress bar | ||
pub template: String, | ||
pub template: Option<String>, | ||
// the tick string sequence for spinners, if it's string then it will be split into characters | ||
pub tick: Option<Either<String, Vec<String>>>, | ||
// the progress characters | ||
pub progress_chars: String, | ||
pub progress_chars: Option<String>, | ||
// the handler for progress event | ||
#[derivative(Debug = "ignore")] | ||
#[napi(ts_type = "(percent: number, msg: string, items: string[]) => void")] | ||
pub handler: Option<HandlerFn>, | ||
} | ||
|
||
impl From<RawProgressPluginOptions> for ProgressPluginOptions { | ||
fn from(value: RawProgressPluginOptions) -> Self { | ||
Self { | ||
prefix: value.prefix, | ||
profile: value.profile, | ||
template: value.template, | ||
progress_chars: value.progress_chars, | ||
tick_strings: value.tick.map(|tick| match tick { | ||
Either::A(str) => str.chars().map(|c| c.to_string()).collect(), | ||
Either::B(vec) => vec, | ||
}), | ||
if let Some(f) = value.handler { | ||
Self::Handler(Arc::new(move |percent, msg, items| { | ||
f.blocking_call_with_sync((percent, msg, items)) | ||
})) | ||
} else { | ||
Self::Default(ProgressPluginDisplayOptions { | ||
prefix: value.prefix.unwrap_or_default(), | ||
profile: value.profile.unwrap_or_default(), | ||
template: value.template.unwrap_or( | ||
"● {prefix:.bold} {bar:25.green/white.dim} ({percent}%) {wide_msg:.dim}".to_string(), | ||
), | ||
progress_chars: value.progress_chars.unwrap_or("━━".to_string()), | ||
tick_strings: value.tick.map(|tick| match tick { | ||
Either::A(str) => str.chars().map(|c| c.to_string()).collect(), | ||
Either::B(vec) => vec, | ||
}), | ||
}) | ||
} | ||
} | ||
} |
Oops, something went wrong.
fc3b9fa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Benchmark detail: Open
fc3b9fa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Ran ecosystem CI: Open