-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move token to being a userdata called TemplateContext to *heavily* cl…
…eanup template token system.
- Loading branch information
Showing
12 changed files
with
118 additions
and
181 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use mlua::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
use std::sync::Arc; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct TemplateContext { | ||
pub template_data: Arc<super::state::TemplateData>, | ||
|
||
#[serde(skip)] | ||
#[serde(default = "std::sync::Mutex::default")] | ||
/// The cached serialized value of the template data | ||
cached_template_data: std::sync::Mutex<Option<LuaValue>>, | ||
} | ||
|
||
impl TemplateContext { | ||
pub fn new(template_data: super::state::TemplateData) -> Self { | ||
Self { | ||
template_data: Arc::new(template_data), | ||
cached_template_data: std::sync::Mutex::default(), | ||
} | ||
} | ||
} | ||
|
||
pub type TemplateContextRef = LuaUserDataRef<TemplateContext>; | ||
|
||
impl LuaUserData for TemplateContext { | ||
fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) { | ||
fields.add_field_method_get("template_data", |lua, this| { | ||
// Check for cached serialized data | ||
let mut cached_data = this | ||
.cached_template_data | ||
.lock() | ||
.map_err(|e| LuaError::external(e.to_string()))?; | ||
|
||
if let Some(v) = cached_data.as_ref() { | ||
return Ok(v.clone()); | ||
} | ||
|
||
log::debug!("TemplateContext: Serializing data"); | ||
let v = lua.to_value(&this.template_data)?; | ||
|
||
*cached_data = Some(v.clone()); | ||
|
||
Ok(v) | ||
}); | ||
} | ||
} |
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
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
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
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
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
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
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
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
Oops, something went wrong.