(&mut self, cmd: VmCommand)
| 261 | } |
| 262 | |
| 263 | async fn handle_cmd(&mut self, cmd: VmCommand) { |
| 264 | match cmd { |
| 265 | VmCommand::Restart(new_scripts) => { |
| 266 | self.restart(new_scripts).await; |
| 267 | } |
| 268 | VmCommand::DispatchEvent(dispatch_event) => self.dispatch_event(dispatch_event), |
| 269 | VmCommand::LoadScript(script) => { |
| 270 | if let Some(script) = self.compile_script(script) { |
| 271 | self.run_script(script.script.id).await |
| 272 | } |
| 273 | } |
| 274 | VmCommand::UpdateScript(script) => { |
| 275 | let mut cloned_scripts = self |
| 276 | .script_store |
| 277 | .borrow() |
| 278 | .scripts |
| 279 | .iter() |
| 280 | .map(|v| v.script.clone()) |
| 281 | .collect::<Vec<_>>(); |
| 282 | |
| 283 | let mut need_reset = false; |
| 284 | for old in &mut cloned_scripts { |
| 285 | if old.id == script.id { |
| 286 | *old = script.clone(); |
| 287 | need_reset = true; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if need_reset { |
| 292 | self.restart(cloned_scripts).await; |
| 293 | } |
| 294 | } |
| 295 | VmCommand::UnloadScripts(scripts) => { |
| 296 | let new_scripts = self |
| 297 | .script_store |
| 298 | .borrow() |
| 299 | .scripts |
| 300 | .iter() |
| 301 | .filter_map(|sc| { |
| 302 | if !scripts.iter().any(|isc| isc.id == sc.script.id) { |
| 303 | Some(sc.script.clone()) |
| 304 | } else { |
| 305 | None |
| 306 | } |
| 307 | }) |
| 308 | .collect::<Vec<_>>(); |
| 309 | |
| 310 | self.restart(new_scripts).await; |
| 311 | } |
| 312 | }; |
| 313 | // self._dump_heap_stats(); |
| 314 | } |
| 315 | |
| 316 | #[instrument(skip_all, fields(num_scripts=scripts.len()))] |
| 317 | fn compile_scripts(&self, scripts: &Vec<Script>) { |
no test coverage detected