Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fix for duplicate entries in visualisation list. #5808

1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions app/gui/view/graph-editor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ serde-wasm-bindgen = { workspace = true }
[dependencies.web-sys]
version = "0.3.4"
features = ["TextMetrics", 'CanvasRenderingContext2d', 'HtmlHeadElement']

[dev-dependencies]
wasm-bindgen-test = "0.3.8"
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ impl Definition {
}
}

impl PartialEq for Definition {
fn eq(&self, other: &Self) -> bool {
self.signature == other.signature && Rc::ptr_eq(&self.constructor, &other.constructor)
}
}

impl Eq for Definition {}


// === Result ===

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ impl Registry {
type_map.get(tp).cloned().unwrap_or_default();
if tp != &any_type {
if let Some(vis_for_any) = type_map.get(&any_type) {
result.extend(vis_for_any.iter().cloned());
for vis in vis_for_any {
if !result.contains(vis) {
result.push(vis.clone_ref());
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may easily speed it up by keeping hash set of paths. AFAIK the path identifies the visualization.

}
}
result
Expand Down Expand Up @@ -130,3 +134,27 @@ impl Default for Registry {
Registry::new()
}
}

#[cfg(test)]
mod tests {
use super::*;

use wasm_bindgen_test::wasm_bindgen_test;

fn assert_no_duplicates<T: Eq + Hash + Debug + Clone>(items: &[T]) {
let vec_length = items.len();
let set: HashSet<T> = items.iter().cloned().collect();
assert_eq!(set.len(), vec_length, "Duplicate entries found: {items:?}");
}

#[wasm_bindgen_test]
fn assert_no_duplicate_default_visualisations() {
let registry = Registry::new();
registry.add_default_visualizations();

for entry in registry.type_map.borrow().values() {
let signatures = entry.iter().map(|class| class.signature.clone()).collect_vec();
assert_no_duplicates(&signatures);
}
}
}