From b0d7ad88b99c4c21ef74f1bc655d5e4265e6c101 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Thu, 23 Mar 2023 21:44:27 +0800 Subject: [PATCH] fix: not leaking closure --- crates/loro-wasm/deno_test/test.ts | 2 -- crates/loro-wasm/src/lib.rs | 11 ++++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/loro-wasm/deno_test/test.ts b/crates/loro-wasm/deno_test/test.ts index 6c45cbeb..0b8f8893 100644 --- a/crates/loro-wasm/deno_test/test.ts +++ b/crates/loro-wasm/deno_test/test.ts @@ -1,5 +1,4 @@ import init, { - enableDebug, Loro, LoroMap, PrelimList, @@ -15,7 +14,6 @@ import { await init(); setPanicHook(); -enableDebug(); Deno.test({ name: "loro_wasm", diff --git a/crates/loro-wasm/src/lib.rs b/crates/loro-wasm/src/lib.rs index fd06bb9f..71794142 100644 --- a/crates/loro-wasm/src/lib.rs +++ b/crates/loro-wasm/src/lib.rs @@ -6,7 +6,7 @@ use loro_internal::{ log_store::GcConfig, ContainerType, List, LoroCore, Map, Origin, Text, Transact, TransactionWrap, VersionVector, }; -use std::{cell::RefCell, ops::Deref, sync::Arc}; +use std::{cell::RefCell, ops::Deref, rc::Rc, sync::Arc}; use wasm_bindgen::{__rt::RefMut, prelude::*}; mod log; mod prelim; @@ -223,7 +223,10 @@ impl Loro { self.0.borrow_mut().subscribe_deep(Box::new(move |e| { let promise = Promise::resolve(&JsValue::NULL); let ob = observer.clone(); - let closure = Closure::new(move |_: JsValue| { + type C = Closure; + let drop_handler: Rc>> = Rc::new(RefCell::new(None)); + let copy = drop_handler.clone(); + let closure = Closure::once(move |_: JsValue| { ob.call1( &Event { local: e.local, @@ -231,9 +234,11 @@ impl Loro { } .into(), ); + + drop(copy); }); let _ = promise.then(&closure); - closure.forget(); + drop_handler.borrow_mut().replace(closure); })) }