(&mut self, event: VmDispatchEvent)
| 392 | } |
| 393 | |
| 394 | fn dispatch_event(&mut self, event: VmDispatchEvent) { |
| 395 | let _ = self.tx.send(VmEvent::DispatchedEvent(event.seq)); |
| 396 | |
| 397 | let data = ScriptDispatchData { |
| 398 | data: serde_json::to_value(event.value).unwrap(), |
| 399 | name: event.name.to_string(), |
| 400 | }; |
| 401 | |
| 402 | let global_ctx = self.runtime.main_context(); |
| 403 | let ctx = global_ctx.open(self.runtime.v8_isolate()); |
| 404 | |
| 405 | let mut scope = self.runtime.handle_scope(); |
| 406 | let globals = ctx.global(&mut scope); |
| 407 | |
| 408 | let core_obj: v8::Local<v8::Object> = |
| 409 | if let Some(obj) = Self::get_property(&mut scope, globals, "BotloaderCore") { |
| 410 | if let Ok(v) = TryFrom::try_from(obj) { |
| 411 | v |
| 412 | } else { |
| 413 | error!("BotloaderCore is not an object, unable to dispatch events"); |
| 414 | return; |
| 415 | } |
| 416 | } else { |
| 417 | error!("BotloaderCore global not found, unable to dispatch events"); |
| 418 | return; |
| 419 | }; |
| 420 | |
| 421 | let dispatch_fn: v8::Local<v8::Function> = if let Some(field) = |
| 422 | Self::get_property(&mut scope, core_obj, "dispatchWrapper") |
| 423 | { |
| 424 | if let Ok(v) = TryFrom::try_from(field) { |
| 425 | v |
| 426 | } else { |
| 427 | error!( |
| 428 | "BotloaderCore.dispatchWrapper is not a function, unable to dispatch events" |
| 429 | ); |
| 430 | return; |
| 431 | } |
| 432 | } else { |
| 433 | error!("BotloaderCore.dispatchWrapper not defined, unable to dispatch events"); |
| 434 | return; |
| 435 | }; |
| 436 | |
| 437 | let v = deno_core::serde_v8::to_v8(&mut scope, &data).unwrap(); |
| 438 | let _ = dispatch_fn.call(&mut scope, globals.into(), &[v]); |
| 439 | |
| 440 | let elapsed = Utc::now().signed_duration_since(event.source_timestamp); |
| 441 | let millis = elapsed.num_milliseconds(); |
| 442 | let class = match event.source { |
| 443 | common::dispatch_event::EventSource::Discord => "discord", |
| 444 | common::dispatch_event::EventSource::Timer => "timer", |
| 445 | }; |
| 446 | |
| 447 | histogram!("dispatch_event_latency", "event_source" => class).record(millis as f64) |
| 448 | } |
| 449 | |
| 450 | fn get_property<'a>( |
| 451 | scope: &mut v8::HandleScope<'a>, |
no test coverage detected