Switch from 'parent' property to 'children' property for tracking instances

This commit is contained in:
Lucien Greathouse 2018-05-30 23:34:45 -07:00
parent 313e617181
commit 1137584cd3
3 changed files with 26 additions and 42 deletions

View file

@ -14,11 +14,9 @@ pub struct RbxInstance {
/// Maps to the `ClassName` property on Instance. /// Maps to the `ClassName` property on Instance.
pub class_name: String, pub class_name: String,
/// Maps to the `Parent` property on Instance.
pub parent: Option<Id>,
/// Contains all other properties of an Instance. /// Contains all other properties of an Instance.
pub properties: HashMap<String, RbxValue>, pub properties: HashMap<String, RbxValue>,
// TODO: children property, Vec<Id> /// All of the children of this instance. Order is relevant to preserve!
pub children: Vec<Id>,
} }

View file

@ -32,19 +32,11 @@ fn file_to_instances(
}, },
}; };
let parent = match file_item.get_route().parent() {
Some(parent_route) => match instances_by_route.get(&parent_route) {
Some(parent_id) => Some(*parent_id),
None => None,
},
None => None,
};
output.insert(primary_id, RbxInstance { output.insert(primary_id, RbxInstance {
name: route.name(partition).to_string(), name: route.name(partition).to_string(),
class_name: "StringValue".to_string(), class_name: "StringValue".to_string(),
parent,
properties, properties,
children: Vec::new(),
}); });
(primary_id, vec![primary_id]) (primary_id, vec![primary_id])
@ -60,28 +52,14 @@ fn file_to_instances(
}, },
}; };
let parent = match file_item.get_route().parent() { let mut child_ids = Vec::new();
Some(parent_route) => match instances_by_route.get(&parent_route) {
Some(parent_id) => Some(*parent_id),
None => None,
},
None => None,
};
output.insert(primary_id, RbxInstance {
name: route.name(partition).to_string(),
class_name: "Folder".to_string(),
parent,
properties: HashMap::new(),
});
let mut changed_ids = vec![primary_id]; let mut changed_ids = vec![primary_id];
for child_file_item in children.values() { for child_file_item in children.values() {
let (child_id, mut child_changed_ids) = file_to_instances(child_file_item, partition, output, instances_by_route); let (child_id, mut child_changed_ids) = file_to_instances(child_file_item, partition, output, instances_by_route);
output.get_mut(&child_id).unwrap().parent = Some(primary_id); child_ids.push(child_id);
changed_ids.push(child_id); changed_ids.push(child_id);
// TODO: Should I stop using drain on Vecs of Copyable types? // TODO: Should I stop using drain on Vecs of Copyable types?
@ -90,6 +68,13 @@ fn file_to_instances(
} }
} }
output.insert(primary_id, RbxInstance {
name: route.name(partition).to_string(),
class_name: "Folder".to_string(),
properties: HashMap::new(),
children: child_ids,
});
(primary_id, changed_ids) (primary_id, changed_ids)
}, },
} }
@ -125,6 +110,19 @@ impl RbxSession {
} }
} }
pub fn retrieve_instance<'a, 'b>(&'a self, id: Id, output: &'b mut HashMap<Id, &'a RbxInstance>) {
let requested_instance = match self.instances.get(&id) {
Some(instance) => instance,
None => return,
};
output.insert(id, requested_instance);
for child_id in &requested_instance.children {
self.retrieve_instance(*child_id, output);
}
}
pub fn read_partitions(&mut self) { pub fn read_partitions(&mut self) {
let vfs_session_arc = self.vfs_session.clone(); let vfs_session_arc = self.vfs_session.clone();
let vfs_session = vfs_session_arc.read().unwrap(); let vfs_session = vfs_session_arc.read().unwrap();

View file

@ -162,19 +162,7 @@ pub fn start(config: WebConfig) {
let mut instances = HashMap::new(); let mut instances = HashMap::new();
for requested_id in &requested_ids { for requested_id in &requested_ids {
let requested_instance = match rbx_session.instances.get(requested_id) { rbx_session.retrieve_instance(*requested_id, &mut instances);
Some(instance) => instance,
None => continue,
};
instances.insert(*requested_id, requested_instance);
// Oops; this needs to be recursive.
for (child_id, instance) in &rbx_session.instances {
if instance.parent == Some(*requested_id) {
instances.insert(*child_id, instance);
}
}
} }
Response::json(&ReadResponse { Response::json(&ReadResponse {