Skip to content

Commit

Permalink
feat(template-compiler): allow removing template/script
Browse files Browse the repository at this point in the history
  • Loading branch information
LastLeaf committed Jul 26, 2024
1 parent 70dd3de commit c3ef844
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions glass-easel-template-compiler/glass_easel_template_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ TmplGroup tmpl_group_new();

TmplGroup tmpl_group_new_dev();

bool tmpl_group_remove_script(TmplGroup *self, const uint8_t *path_buf, size_t path_len);

bool tmpl_group_remove_tmpl(TmplGroup *self, const uint8_t *path_buf, size_t path_len);

void tmpl_group_set_extra_runtime_script(TmplGroup *self,
const uint8_t *content_buf,
size_t content_len);
Expand Down
20 changes: 20 additions & 0 deletions glass-easel-template-compiler/src/cbinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ impl TmplGroup {
)
}

#[no_mangle]
pub unsafe extern "C" fn tmpl_group_remove_tmpl(
&mut self,
path_buf: &u8,
path_len: usize,
) -> bool {
let path = String::from_utf8_lossy(slice::from_raw_parts(path_buf, path_len));
self.inner_mut().remove_tmpl(&path)
}

#[no_mangle]
pub unsafe extern "C" fn tmpl_group_add_script(
&mut self,
Expand All @@ -185,6 +195,16 @@ impl TmplGroup {
self.inner_mut().add_script(&path, &content);
}

#[no_mangle]
pub unsafe extern "C" fn tmpl_group_remove_script(
&mut self,
path_buf: &u8,
path_len: usize,
) -> bool {
let path = String::from_utf8_lossy(slice::from_raw_parts(path_buf, path_len));
self.inner_mut().remove_script(&path)
}

#[no_mangle]
pub unsafe extern "C" fn tmpl_group_get_direct_dependencies(
&self,
Expand Down
20 changes: 20 additions & 0 deletions glass-easel-template-compiler/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ impl TmplGroup {
ret
}

/// Remove a template from the group.
///
/// This simply removes a template path.
/// It is useful when doing hot-update debugging,
/// but not suitable for final builds since it does not do cleanups.
/// Returns true when a template is actually removed.
pub fn remove_tmpl(&mut self, path: &str) -> bool {
self.trees.remove(path).is_some()
}

/// Regenerate a template content string of the specified template.
pub fn stringify_tmpl(&mut self, path: &str) -> Option<String> {
let template = self.trees.get(path)?;
Expand Down Expand Up @@ -247,6 +257,16 @@ impl TmplGroup {
self.has_scripts = true;
}

/// Remove a script segment from the group.
///
/// This simply removes a script path.
/// It is useful when doing hot-update debugging,
/// but not suitable for final builds since it does not do cleanups.
/// Returns true when a script is actually removed.
pub fn remove_script(&mut self, path: &str) -> bool {
self.scripts.remove(path).is_some()
}

/// Set extra runtime JavaScript code as a string.
///
/// The `content` must be valid JavaScript statements, ended by semicolon.
Expand Down
12 changes: 12 additions & 0 deletions glass-easel-template-compiler/src/js_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ impl TmplGroup {
serde_wasm_bindgen::to_value(&ret).unwrap()
}

#[wasm_bindgen(js_name = removeTmpl)]
pub fn remove_tmpl(&mut self, path: &str) -> bool {
let path = crate::path::normalize(path);
self.group.remove_tmpl(&path)
}

/// Regenerate a template content string for the specified template.
#[wasm_bindgen(js_name = stringifyTmpl)]
pub fn stringify_tmpl(&mut self, path: &str) -> Option<String> {
Expand All @@ -97,6 +103,12 @@ impl TmplGroup {
self.group.add_script(&path, tmpl_str);
}

#[wasm_bindgen(js_name = removeScript)]
pub fn remove_script(&mut self, path: &str) -> bool {
let path = crate::path::normalize(path);
self.group.remove_script(&path)
}

#[wasm_bindgen(js_name = "getDirectDependencies")]
pub fn get_direct_dependencies(&self, path: &str) -> Result<js_sys::Array, JsError> {
let dependencies = self.group.direct_dependencies(&path)?;
Expand Down

0 comments on commit c3ef844

Please sign in to comment.