Skip to content

Commit

Permalink
Add way to release memory management of callbacks to JS GC (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
devashishdxt authored Jan 25, 2024
1 parent 8d20e87 commit ea37af7
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/request/database/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ macro_rules! impl_database_request {
{
self.inner.on_upgrade_needed(callback)
}

/// Release memory management of the callbacks to JS GC.
///
/// > Note: This may leak memory. Read more about it
/// [here](https://docs.rs/wasm-bindgen/latest/wasm_bindgen/closure/struct.Closure.html#method.into_js_value).
pub fn forget_callbacks(&mut self) {
self.inner.forget_callbacks()
}
}

impl crate::Request for $type {
Expand Down
27 changes: 27 additions & 0 deletions src/request/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,33 @@ impl DatabaseRequest {
.set_onupgradeneeded(Some(closure.as_ref().unchecked_ref()));
self.upgrade_needed_callback = Some(closure);
}

/// Release memory management of the callbacks to JS GC.
///
/// > Note: This may leak memory. Read more about it
/// [here](https://docs.rs/wasm-bindgen/latest/wasm_bindgen/closure/struct.Closure.html#method.into_js_value).
pub fn forget_callbacks(&mut self) {
let success_callback = self.success_callback.take();
let error_callback = self.error_callback.take();
let blocked_callback = self.blocked_callback.take();
let upgrade_needed_callback = self.upgrade_needed_callback.take();

if let Some(callback) = success_callback {
callback.forget();
}

if let Some(callback) = error_callback {
callback.forget();
}

if let Some(callback) = blocked_callback {
callback.forget();
}

if let Some(callback) = upgrade_needed_callback {
callback.forget();
}
}
}

impl Request for DatabaseRequest {
Expand Down
10 changes: 10 additions & 0 deletions src/request/store/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ macro_rules! impl_store_request {
inner: $base_request,
}

impl $type {
/// Release memory management of the callbacks to JS GC.
///
/// > Note: This may leak memory. Read more about it
/// [here](https://docs.rs/wasm-bindgen/latest/wasm_bindgen/closure/struct.Closure.html#method.into_js_value).
pub fn forget_callbacks(&mut self) {
self.inner.forget_callbacks();
}
}

impl crate::Request for $type {
type Event = $event;

Expand Down
19 changes: 19 additions & 0 deletions src/request/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ pub struct StoreRequest {
error_callback: Option<Closure<dyn FnMut(Event)>>,
}

impl StoreRequest {
/// Release memory management of the callbacks to JS GC.
///
/// > Note: This may leak memory. Read more about it
/// [here](https://docs.rs/wasm-bindgen/latest/wasm_bindgen/closure/struct.Closure.html#method.into_js_value).
pub fn forget_callbacks(&mut self) {
let success_callback = self.success_callback.take();
let error_callback = self.error_callback.take();

if let Some(callback) = success_callback {
callback.forget();
}

if let Some(callback) = error_callback {
callback.forget();
}
}
}

impl Request for StoreRequest {
type Event = Event;

Expand Down
22 changes: 22 additions & 0 deletions src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ impl Transaction {
.set_onerror(Some(closure.as_ref().unchecked_ref()));
self.error_callback = Some(closure);
}

/// Release memory management of the callbacks to JS GC.
///
/// > Note: This may leak memory. Read more about it
/// [here](https://docs.rs/wasm-bindgen/latest/wasm_bindgen/closure/struct.Closure.html#method.into_js_value).
pub fn forget_callbacks(&mut self) {
let abort_callback = self.abort_callback.take();
let complete_callback = self.complete_callback.take();
let error_callback = self.error_callback.take();

if let Some(callback) = abort_callback {
callback.forget();
}

if let Some(callback) = complete_callback {
callback.forget();
}

if let Some(callback) = error_callback {
callback.forget();
}
}
}

impl TryFrom<EventTarget> for Transaction {
Expand Down

0 comments on commit ea37af7

Please sign in to comment.