From e4f35b29bdcea096b97d62106e0bab7a7416fc0d Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Sat, 26 May 2018 02:28:34 -0700 Subject: [PATCH] Deletion, wrangling parents a little bit --- plugin/src/Session.lua | 8 ++++++++ server/src/file_route.rs | 14 ++++++++++++++ server/src/rbx_session.rs | 39 +++++++++++++++++++++++++++++---------- server/src/web.rs | 9 +++++++++ 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/plugin/src/Session.lua b/plugin/src/Session.lua index 2570d9f0..edfab7b0 100644 --- a/plugin/src/Session.lua +++ b/plugin/src/Session.lua @@ -21,7 +21,9 @@ function Session.new() return api:readAll() :andThen(function(instances) + local visited = {} for id, instance in pairs(instances) do + visited[id] = true if id ~= "0" then local existing = created[id] if existing ~= nil then @@ -45,6 +47,12 @@ function Session.new() created[id].Parent = created[tostring(instance.parent)] end end + + for id, object in pairs(created) do + if not visited[id] then + object:Destroy() + end + end end) end diff --git a/server/src/file_route.rs b/server/src/file_route.rs index 9eddc7ef..3bd3b38e 100644 --- a/server/src/file_route.rs +++ b/server/src/file_route.rs @@ -30,6 +30,20 @@ impl FileRoute { }) } + pub fn parent(&self) -> Option { + if self.route.len() == 0 { + return None; + } + + let mut new_route = self.route.clone(); + new_route.pop(); + + Some(FileRoute { + partition: self.partition.clone(), + route: new_route, + }) + } + /// Creates a PathBuf out of the `FileRoute` based on the given partition /// `Path`. pub fn to_path_buf(&self, partition_path: &Path) -> PathBuf { diff --git a/server/src/rbx_session.rs b/server/src/rbx_session.rs index d1d766d6..360fcfd2 100644 --- a/server/src/rbx_session.rs +++ b/server/src/rbx_session.rs @@ -20,18 +20,24 @@ fn file_to_instances( let mut properties = HashMap::new(); properties.insert("Value".to_string(), contents.clone()); - let (primary_id, parent) = match instances_by_route.get(&file_item.get_route()) { - Some(&id) => { - (id, output.get(&id).unwrap().parent) - }, + let primary_id = match instances_by_route.get(&file_item.get_route()) { + Some(&id) => id, None => { let id = get_id(); instances_by_route.insert(route.clone(), id); - (id, None) + id }, }; + 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 { name: route.name(partition).to_string(), class_name: "StringValue".to_string(), @@ -42,18 +48,24 @@ fn file_to_instances( (primary_id, vec![primary_id]) }, FileItem::Directory { children, route } => { - let (primary_id, parent) = match instances_by_route.get(&file_item.get_route()) { - Some(&id) => { - (id, output.get(&id).unwrap().parent) - }, + let primary_id = match instances_by_route.get(&file_item.get_route()) { + Some(&id) => id, None => { let id = get_id(); instances_by_route.insert(route.clone(), id); - (id, None) + id }, }; + 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 { name: route.name(partition).to_string(), class_name: "Folder".to_string(), @@ -141,6 +153,13 @@ impl RbxSession { self.message_session.push_messages(&messages); }, FileChange::Deleted(route) => { + match self.instances_by_route.get(route) { + Some(id) => { + self.instances.remove(id); + self.message_session.push_messages(&[Message::InstanceChanged { id: *id }]); + }, + None => (), + } }, FileChange::Moved(from_route, to_route) => { }, diff --git a/server/src/web.rs b/server/src/web.rs index 3246db02..1d172c6f 100644 --- a/server/src/web.rs +++ b/server/src/web.rs @@ -68,6 +68,15 @@ pub fn start(config: WebConfig) { // Did the client miss any messages since the last subscribe? { let messages = config.message_session.messages.read().unwrap(); + + if cursor > messages.len() as i32 { + return json_response(SubscribeResponse { + server_id: &server_id, + messages: &[], + message_cursor: messages.len() as i32 - 1, + }); + } + if cursor < messages.len() as i32 - 1 { let new_messages = &messages[(cursor + 1) as usize..]; let new_cursor = cursor + new_messages.len() as i32;