Skip to content

Commit

Permalink
new: Add AppExtension. (#54)
Browse files Browse the repository at this point in the history
* Add ext.

* Add tests.
  • Loading branch information
milesj authored Jan 30, 2024
1 parent 6664bc7 commit 1d2a9c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
9 changes: 9 additions & 0 deletions crates/framework/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub enum Phase {
Shutdown,
}

pub trait AppExtension {
fn extend(self, app: &mut App) -> AppResult<()>;
}

#[derive(Debug)]
pub struct App {
// Data
Expand Down Expand Up @@ -78,6 +82,11 @@ impl App {
crate::tracing::setup_tracing(options);
}

/// Extend the app with an extension that contains a set of systems.
pub fn extend(&mut self, extension: impl AppExtension) -> AppResult<()> {
extension.extend(self)
}

/// Add a system function that runs during the startup phase.
pub fn startup<S: SystemFunc + 'static>(&mut self, system: S) -> &mut Self {
self.add_system(Phase::Startup, CallbackSystem::new(system))
Expand Down
29 changes: 28 additions & 1 deletion crates/framework/tests/app_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code)]

use miette::{bail, IntoDiagnostic};
use starbase::{App, AppPhase, Emitters, Resources, States, SystemResult};
use starbase::{App, AppExtension, AppPhase, Emitters, Resources, States, SystemResult};
use starbase_macros::*;
use std::time::Duration;
use tokio::task;
Expand Down Expand Up @@ -623,3 +623,30 @@ async fn tracks_app_state() {
vec!["Startup", "Analyze", "Execute", "Shutdown"]
);
}

struct TestExtension;

impl AppExtension for TestExtension {
fn extend(self, app: &mut App) -> miette::Result<()> {
app.startup(extract_app_state);
app.analyze(extract_app_state);
app.execute(extract_app_state);
app.shutdown(extract_app_state);

Ok(())
}
}

#[tokio::test]
async fn extension_can_register_systems() {
let mut app = App::new();
app.startup(setup_state);
app.extend(TestExtension).unwrap();

let states = app.run().await.unwrap();

assert_eq!(
states.get::<RunOrder>().0,
vec!["Startup", "Analyze", "Execute", "Shutdown"]
);
}

0 comments on commit 1d2a9c5

Please sign in to comment.