fix: not leaking closure

This commit is contained in:
Zixuan Chen 2023-03-23 21:44:27 +08:00
parent 63ebbe2ddb
commit b0d7ad88b9
2 changed files with 8 additions and 5 deletions

View file

@ -1,5 +1,4 @@
import init, {
enableDebug,
Loro,
LoroMap,
PrelimList,
@ -15,7 +14,6 @@ import {
await init();
setPanicHook();
enableDebug();
Deno.test({
name: "loro_wasm",

View file

@ -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<dyn FnMut(JsValue)>;
let drop_handler: Rc<RefCell<Option<C>>> = 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);
}))
}