Jelajahi Sumber

fix multiwindow due to mounts not dropping in virtualdom (#4351)

* fix handlers not dropping thanks to mounts

* use `desktop`

* Add tasks and effects

* only destroy on virtualdom destroy

* fix clippy
Jonathan Kelley 1 hari lalu
induk
melakukan
1db4b2a13d
3 mengubah file dengan 16 tambahan dan 7 penghapusan
  1. 10 0
      examples/popup.rs
  2. 1 7
      examples/window_focus.rs
  3. 5 0
      packages/core/src/virtual_dom.rs

+ 10 - 0
examples/popup.rs

@@ -43,10 +43,20 @@ fn app() -> Element {
 
 fn popup(send: Rc<dyn Fn(String)>) -> Element {
     let mut user_input = use_signal(String::new);
+    let window = dioxus::desktop::use_window();
+
+    let close_window = move |_| {
+        println!("Attempting to close Window B");
+        window.close();
+    };
 
     rsx! {
         div {
             h1 { "Compose a new email" }
+            button {
+                onclick: close_window,
+                "Close Window B (button)"
+            }
             button {
                 onclick: move |_| {
                     send(user_input.cloned());

+ 1 - 7
examples/window_focus.rs

@@ -8,16 +8,10 @@
 use dioxus::desktop::tao::event::Event as WryEvent;
 use dioxus::desktop::tao::event::WindowEvent;
 use dioxus::desktop::use_wry_event_handler;
-use dioxus::desktop::{Config, DefaultWindowCloseBehaviour};
 use dioxus::prelude::*;
 
 fn main() {
-    dioxus::LaunchBuilder::desktop()
-        .with_cfg(
-            Config::new()
-                .with_default_window_close_behaviour(DefaultWindowCloseBehaviour::WindowsCloses),
-        )
-        .launch(app)
+    dioxus::launch(app);
 }
 
 fn app() -> Element {

+ 5 - 0
packages/core/src/virtual_dom.rs

@@ -773,6 +773,11 @@ impl Drop for VirtualDom {
         for scope in scopes.into_iter().rev() {
             drop(scope);
         }
+
+        // Drop the mounts, tasks, and effects, releasing any `Rc<Runtime>` references
+        self.runtime.pending_effects.borrow_mut().clear();
+        self.runtime.tasks.borrow_mut().clear();
+        self.runtime.mounts.borrow_mut().clear();
     }
 }