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

Use dashmap in widget_state #263

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ impl Tree {
if let Some(root_node) = self.root_node {
if root_node == index {
self.root_node = None;
self.parents.clear();
self.children.clear();
}
}
Vec::default()
Expand Down
70 changes: 25 additions & 45 deletions src/widget_state.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use std::sync::{Arc, RwLock};

use bevy::{
prelude::{BuildChildren, Commands, Component, Entity, Resource},
utils::HashMap,
};
use bevy::prelude::{BuildChildren, Commands, Component, Entity, Resource};
use dashmap::DashMap;
use std::sync::Arc;

/// Stores mappings between widget entities and their corresponding state entities.
#[derive(Resource, Default, Debug, Clone)]
pub struct WidgetState {
// Widget entity to state entity
mapping: Arc<RwLock<HashMap<Entity, Entity>>>,
mapping: Arc<DashMap<Entity, Entity>>,
// State entity to widget entity
reverse_mapping: Arc<RwLock<HashMap<Entity, Entity>>>,
reverse_mapping: Arc<DashMap<Entity, Entity>>,
}

impl WidgetState {
Expand All @@ -22,55 +19,38 @@ impl WidgetState {
widget_entity: Entity,
initial_state: State,
) -> Entity {
if let (Ok(mut mapping), Ok(mut reverse_mapping)) =
(self.mapping.try_write(), self.reverse_mapping.try_write())
{
if mapping.contains_key(&widget_entity) {
*mapping.get(&widget_entity).unwrap()
} else {
let mut state_entity = None;
commands
.entity(widget_entity)
.with_children(|child_builder| {
state_entity = Some(child_builder.spawn(initial_state).id());
mapping.insert(widget_entity, state_entity.unwrap());
reverse_mapping.insert(state_entity.unwrap(), widget_entity);
});
state_entity.expect("State entity did not spawn!")
}
if self.mapping.contains_key(&widget_entity) {
*self.mapping.get(&widget_entity).unwrap()
} else {
panic!("Couldn't get mapping lock!");
let mut state_entity = None;
commands
.entity(widget_entity)
.with_children(|child_builder| {
state_entity = Some(child_builder.spawn(initial_state).id());
self.mapping.insert(widget_entity, state_entity.unwrap());
self.reverse_mapping
.insert(state_entity.unwrap(), widget_entity);
});
state_entity.expect("State entity did not spawn!")
}
}

/// Attempts to get a state entity
pub fn get(&self, widget_entity: Entity) -> Option<Entity> {
if let Ok(mapping) = self.mapping.try_read() {
return mapping.get(&widget_entity).copied();
}

None
self.mapping.get(&widget_entity).map(|entry| *entry.value())
}

pub fn get_widget_entity(&self, state_entity: Entity) -> Option<Entity> {
if let Ok(reverse_mapping) = self.reverse_mapping.try_read() {
return reverse_mapping.get(&state_entity).copied();
}

None
self.reverse_mapping
.get(&state_entity)
.map(|entry| *entry.value())
}

pub fn remove(&self, widget_entity: Entity) -> Option<Entity> {
if let (Ok(mut mapping), Ok(mut reverse_mapping)) =
(self.mapping.try_write(), self.reverse_mapping.try_write())
{
let state_entity = mapping.remove(&widget_entity);
if let Some(state_entity) = state_entity {
reverse_mapping.remove(&state_entity);
}
return state_entity;
let state_entity = self.mapping.remove(&widget_entity).map(|(_, v)| v);
if let Some(state_entity) = state_entity {
self.reverse_mapping.remove(&state_entity);
}

None
state_entity
}
}