Skip to content

Commit

Permalink
feat: set up functional plugin actions for global state
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Jan 21, 2022
1 parent 82430db commit 6aa45aa
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 14 deletions.
2 changes: 2 additions & 0 deletions docs/next/en-US/plugins/functional.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ If you'd like to request that a new action, functional or control, be added, ple
- `before_build` -- runs arbitrary code just before the build process starts (e.g. to run a CSS preprocessor)
- `after_successful_build` -- runs arbitrary code after the build process has completed, if it was successful (e.g. copying custom files into `.perseus/dist/`)
- `after_failed_build` -- runs arbitrary code after the build process has completed, if it failed (e.g. to report the failed build to a server crash management system)
- `after_failed_global_state_creation` -- runs arbitrary code after if the build process failed to generate global state
- `export_actions` -- actions that'll be run when the user runs `perseus export`
- `before_export` -- runs arbitrary code just before the export process starts (e.g. to run a CSS preprocessor)
- `after_successful_build` -- runs arbitrary code after the build process has completed (inside the export process), if it was successful (e.g. copying custom files into `.perseus/dist/`)
Expand All @@ -26,6 +27,7 @@ If you'd like to request that a new action, functional or control, be added, ple
- `after_failed_static_alias_dir_copy` -- runs arbitrary code if the export process fails to copy a static alias that was a directory (e.g. to report the failed export to a server crash management system)
- `after_failed_static_alias_file_copy` -- runs arbitrary code if the export process fails to copy a static alias that was a file (e.g. to report the failed export to a server crash management system)
- `after_successful_export` -- runs arbitrary code after the export process has completed, if it was successful (e.g. copying custom files into `.perseus/dist/`)
- `after_failed_global_state_creation` -- runs arbitrary code if the export process failed to generate global state
- `export_error_page_actions` --- actions that'll be run when exporting an error page
- `before_export_error_page` --- runs arbitrary code before this process has started (providing the error code to be exported for and the output file)
- `after_successful_export_error_page` -- runs arbitrary code after this process has completed, if it was successful
Expand Down
8 changes: 6 additions & 2 deletions examples/basic/.perseus/builder/src/bin/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ async fn real_main() -> i32 {
let translations_manager = get_translations_manager().await;
let locales = get_locales(&plugins);
// Generate the global state
let gsc = get_global_state_creator(&plugins);
let gsc = get_global_state_creator();
let global_state = match gsc.get_build_state().await {
Ok(global_state) => global_state,
Err(err) => {
let err_msg = fmt_err(&err);
// TODO Functional action here
plugins
.functional_actions
.build_actions
.after_failed_global_state_creation
.run(err, plugins.get_plugin_data());
eprintln!("{}", err_msg);
return 1;
}
Expand Down
8 changes: 6 additions & 2 deletions examples/basic/.perseus/builder/src/bin/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ async fn build_and_export() -> i32 {
let translations_manager = get_translations_manager().await;
let locales = get_locales(&plugins);
// Generate the global state
let gsc = get_global_state_creator(&plugins);
let gsc = get_global_state_creator();
let global_state = match gsc.get_build_state().await {
Ok(global_state) => global_state,
Err(err) => {
let err_msg = fmt_err(&err);
// TODO Functional action here
plugins
.functional_actions
.export_actions
.after_failed_global_state_creation
.run(err, plugins.get_plugin_data());
eprintln!("{}", err_msg);
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/.perseus/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn get_props(is_standalone: bool) -> ServerProps<impl MutableStore, impl Transla
let app_root = get_app_root(&plugins);
let static_aliases = get_static_aliases(&plugins);
// Generate the global state
let global_state_creator = get_global_state_creator(&plugins);
let global_state_creator = get_global_state_creator();

let opts = ServerOptions {
// We don't support setting some attributes from `wasm-pack` through plugins/`define_app!` because that would require CLI changes as well (a job for an alternative engine)
Expand Down
8 changes: 1 addition & 7 deletions examples/basic/.perseus/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
pub use app::get_plugins;
use perseus::{
internal::i18n::Locales,
state::GlobalStateCreator,
stores::ImmutableStore,
templates::{ArcTemplateMap, TemplateMap},
ErrorPages, Html, PluginAction, Plugins,
};
use std::{collections::HashMap, rc::Rc, sync::Arc};

pub use app::{get_mutable_store, get_translations_manager};
pub use app::{get_global_state_creator, get_mutable_store, get_translations_manager};

// These functions all take plugins so we don't have to perform possibly very costly allocation more than once in an environment (e.g. browser, build process, export process, server)

Expand Down Expand Up @@ -166,8 +165,3 @@ pub fn get_error_pages_contained<G: Html>() -> ErrorPages<G> {
let plugins = get_plugins::<G>();
get_error_pages(&plugins)
}

pub fn get_global_state_creator<G: Html>(plugins: &Plugins<G>) -> GlobalStateCreator {
// TODO Control action to override
app::get_global_state_creator()
}
4 changes: 2 additions & 2 deletions packages/perseus/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ macro_rules! define_get_mutable_store {
}
};
}
/// An internal macro used for defining the global state creator.
// TODO Plugin extensibility?
/// An internal macro used for defining the global state creator. This cannot be altered in any way by plugins, except through
/// hooking into it through a generated template and modifying it.
#[doc(hidden)]
#[macro_export]
macro_rules! define_get_global_state_creator {
Expand Down
6 changes: 6 additions & 0 deletions packages/perseus/src/plugins/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ pub struct FunctionalPluginBuildActions {
pub after_successful_build: FunctionalPluginAction<(), ()>,
/// Runs after the build process if it fails.
pub after_failed_build: FunctionalPluginAction<crate::errors::ServerError, ()>,
/// Runs after the build process if it failed to generate global state.
pub after_failed_global_state_creation:
FunctionalPluginAction<crate::errors::GlobalStateError, ()>,
}
/// Functional actions that pertain to the export process.
#[derive(Default)]
Expand All @@ -141,6 +144,9 @@ pub struct FunctionalPluginExportActions {
pub after_failed_static_alias_file_copy: FunctionalPluginAction<std::io::Error, ()>,
/// Runs after the export process if it completes successfully.
pub after_successful_export: FunctionalPluginAction<(), ()>,
/// Runs after the export process if it failed to generate global state.
pub after_failed_global_state_creation:
FunctionalPluginAction<crate::errors::GlobalStateError, ()>,
}
/// Functional actions that pertain to the process of exporting an error page.
#[derive(Default)]
Expand Down

0 comments on commit 6aa45aa

Please sign in to comment.