#![allow(non_snake_case)]
#[cfg(not(target_arch = "wasm32"))]
use crate::fs_cache::PathMapFn;
use crate::memory_cache::InMemoryCache;
use crate::IncrementalRenderer;
use std::{
path::{Path, PathBuf},
time::Duration,
};
#[derive(Clone)]
pub struct IncrementalRendererConfig {
static_dir: PathBuf,
memory_cache_limit: usize,
invalidate_after: Option<Duration>,
clear_cache: bool,
pre_render: bool,
#[cfg(not(target_arch = "wasm32"))]
map_path: Option<PathMapFn>,
}
impl Default for IncrementalRendererConfig {
fn default() -> Self {
Self::new()
}
}
impl IncrementalRendererConfig {
pub fn new() -> Self {
Self {
static_dir: PathBuf::from("./static"),
memory_cache_limit: 10000,
invalidate_after: None,
clear_cache: true,
pre_render: false,
#[cfg(not(target_arch = "wasm32"))]
map_path: None,
}
}
pub fn clear_cache(mut self, clear_cache: bool) -> Self {
self.clear_cache = clear_cache;
self
}
#[cfg(not(target_arch = "wasm32"))]
pub fn map_path<F: Fn(&str) -> PathBuf + Send + Sync + 'static>(mut self, map_path: F) -> Self {
self.map_path = Some(std::sync::Arc::new(map_path));
self
}
pub fn static_dir<P: AsRef<Path>>(mut self, static_dir: P) -> Self {
self.static_dir = static_dir.as_ref().to_path_buf();
self
}
pub const fn memory_cache_limit(mut self, memory_cache_limit: usize) -> Self {
self.memory_cache_limit = memory_cache_limit;
self
}
pub fn invalidate_after(mut self, invalidate_after: Duration) -> Self {
self.invalidate_after = Some(invalidate_after);
self
}
pub fn pre_render(mut self, pre_render: bool) -> Self {
self.pre_render = pre_render;
self
}
pub fn build(self) -> IncrementalRenderer {
let mut renderer = IncrementalRenderer {
#[cfg(not(target_arch = "wasm32"))]
file_system_cache: crate::fs_cache::FileSystemCache::new(
self.static_dir.clone(),
self.map_path,
self.invalidate_after,
),
memory_cache: InMemoryCache::new(self.memory_cache_limit, self.invalidate_after),
invalidate_after: self.invalidate_after,
};
if self.clear_cache {
renderer.invalidate_all();
}
renderer
}
}