Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add node builder helpers #11731

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions crates/node/builder/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ impl<ChainSpec> NodeBuilder<(), ChainSpec> {
{
f(self)
}

/// Apply a function to the builder, if the condition is `true`.
pub fn apply_if<F>(self, cond: bool, f: F) -> Self
where
F: FnOnce(Self) -> Self,
{
if cond {
f(self)
} else {
self
}
}
}

impl<DB, ChainSpec> NodeBuilder<DB, ChainSpec> {
Expand Down Expand Up @@ -421,6 +433,18 @@ where
f(self)
}

/// Apply a function to the builder, if the condition is `true`.
pub fn apply_if<F>(self, cond: bool, f: F) -> Self
where
F: FnOnce(Self) -> Self,
{
if cond {
f(self)
} else {
self
}
}

/// Sets the hook that is run once the node's components are initialized.
pub fn on_component_initialized<F>(self, hook: F) -> Self
where
Expand Down Expand Up @@ -482,6 +506,24 @@ where
}
}

/// Installs an `ExEx` (Execution Extension) in the node if the condition is true.
///
/// # Note
///
/// The `ExEx` ID must be unique.
pub fn install_exex_if<F, R, E>(self, cond: bool, exex_id: impl Into<String>, exex: F) -> Self
where
F: FnOnce(ExExContext<NodeAdapter<T, CB::Components>>) -> R + Send + 'static,
R: Future<Output = eyre::Result<E>> + Send,
E: Future<Output = eyre::Result<()>> + Send,
{
if cond {
self.install_exex(exex_id, exex)
} else {
self
}
}

/// Launches the node with the given launcher.
pub async fn launch_with<L>(self, launcher: L) -> eyre::Result<L::Node>
where
Expand Down
Loading