Repository: tauri-apps/tauri
File: core/tauri-runtime-wry/src/lib.rs (Rust)

Code before:
        event,
        event_loop,
        control_flow,
        EventLoopIterationContext {
          callback: &mut callback,
          webview_id_map: webview_id_map.clone(),
          windows: windows.clone(),
          #[cfg(all(desktop, feature = "global-shortcut"))]
          global_shortcut_manager: global_shortcut_manager.clone(),
          #[cfg(all(desktop, feature = "global-shortcut"))]
          global_shortcut_manager_handle: &global_shortcut_manager_handle,
          #[cfg(feature = "clipboard")]
          clipboard_manager: clipboard_manager.clone(),
          #[cfg(all(desktop, feature = "system-tray"))]
          system_tray_manager: system_tray_manager.clone(),
        },
        &web_context,
      );
    })
  }
}

pub struct EventLoopIterationContext<'a, T: UserEvent> {
  pub callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
  pub webview_id_map: WebviewIdStore,
  pub windows: Arc<RefCell<BTreeMap<WebviewId, WindowWrapper>>>,
  #[cfg(all(desktop, feature = "global-shortcut"))]
  pub global_shortcut_manager: Rc<Mutex<WryShortcutManager>>,
  #[cfg(all(desktop, feature = "global-shortcut"))]
  pub global_shortcut_manager_handle: &'a GlobalShortcutManagerHandle<T>,
  #[cfg(feature = "clipboard")]
  pub clipboard_manager: Arc<Mutex<Clipboard>>,
  #[cfg(all(desktop, feature = "system-tray"))]
  pub system_tray_manager: SystemTrayManager,
}

struct UserMessageContext {
  windows: Arc<RefCell<BTreeMap<WebviewId, WindowWrapper>>>,
  webview_id_map: WebviewIdStore,
  #[cfg(all(desktop, feature = "global-shortcut"))]
  global_shortcut_manager: Rc<Mutex<WryShortcutManager>>,
  #[cfg(feature = "clipboard")]
  clipboard_manager: Arc<Mutex<Clipboard>>,
  #[cfg(all(desktop, feature = "system-tray"))]
  system_tray_manager: SystemTrayManager,
}

fn handle_user_message<T: UserEvent>(
  event_loop: &EventLoopWindowTarget<Message<T>>,
  message: Message<T>,
  context: UserMessageContext,

Review comment (suggestion):
```suggestion
  pub windows: Arc<Mutex<BTreeMap<WebviewId, WindowWrapper>>>,
```

We should use Mutex at this point even you want to use it in single thread.
This is already a control flow that has to work with tokio.
Adding lock instead of runtime check then panic is preferred behaviour imho.

Code after:
    let windows = self.context.main_thread.windows.clone();
    let webview_id_map = self.context.webview_id_map.clone();
    let web_context = self.context.main_thread.web_context;
    let mut plugins = self.plugins;

    #[cfg(feature = "tracing")]
    let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone();

    #[cfg(all(desktop, feature = "system-tray"))]
    let system_tray_manager = self.context.main_thread.system_tray_manager;

    #[cfg(all(desktop, feature = "global-shortcut"))]
    let global_shortcut_manager = self.context.main_thread.global_shortcut_manager.clone();
    #[cfg(all(desktop, feature = "global-shortcut"))]
    let global_shortcut_manager_handle = self.global_shortcut_manager_handle.clone();

    let proxy = self.event_loop.create_proxy();

    self.event_loop.run(move |event, event_loop, control_flow| {
      for p in &mut plugins {
        let prevent_default = p.on_event(
          &event,
          event_loop,
          &proxy,
          control_flow,
          EventLoopIterationContext {
            callback: &mut callback,
            webview_id_map: webview_id_map.clone(),
            windows: windows.clone(),
            #[cfg(all(desktop, feature = "global-shortcut"))]
            global_shortcut_manager: global_shortcut_manager.clone(),
            #[cfg(all(desktop, feature = "global-shortcut"))]
            global_shortcut_manager_handle: &global_shortcut_manager_handle,
            #[cfg(all(desktop, feature = "system-tray"))]
            system_tray_manager: system_tray_manager.clone(),
            #[cfg(feature = "tracing")]
            active_tracing_spans: active_tracing_spans.clone(),
          },
          &web_context,
        );
        if prevent_default {
          return;
        }
      }
      handle_event_loop(
        event,
        event_loop,
        control_flow,
        EventLoopIterationContext {
          callback: &mut callback,
          webview_id_map: webview_id_map.clone(),