1
0
Fork 0
forked from wry/wry

toml-config: clear named-action reference cycles

This commit is contained in:
Julian Orth 2025-05-01 12:13:34 +02:00
parent c7bba173fb
commit 2f9af09795
5 changed files with 34 additions and 7 deletions

View file

@ -17,3 +17,4 @@ backtrace = "0.3.69"
error_reporter = "1.0.0"
serde_json = "1.0.114"
bstr = { version = "1.9.0", default-features = false, features = ["std"] }
run-on-drop = "1.0.0"

View file

@ -33,6 +33,7 @@ use {
},
bincode::Options,
futures_util::task::ArcWake,
run_on_drop::{OnDrop, on_drop},
std::{
cell::{Cell, RefCell},
collections::{HashMap, VecDeque, hash_map::Entry},
@ -100,6 +101,7 @@ pub(crate) struct Client {
on_del_drm_device: RefCell<Option<Callback<DrmDevice>>>,
on_idle: RefCell<Option<Callback>>,
on_switch_event: RefCell<HashMap<InputDevice, Callback<SwitchEvent>>>,
on_unload: Cell<Option<OnDrop<Box<dyn FnOnce()>>>>,
bufs: RefCell<Vec<Vec<u8>>>,
reload: Cell<bool>,
read_interests: RefCell<HashMap<PollableId, Interest>>,
@ -232,6 +234,7 @@ pub unsafe extern "C" fn init(
on_del_drm_device: Default::default(),
on_idle: Default::default(),
on_switch_event: Default::default(),
on_unload: Default::default(),
bufs: Default::default(),
reload: Cell::new(false),
read_interests: Default::default(),
@ -1250,6 +1253,12 @@ impl Client {
}
}
pub fn on_unload(&self, f: impl FnOnce() + 'static) {
if let Some(prev) = self.on_unload.replace(Some(on_drop(Box::new(f)))) {
prev.forget();
}
}
fn handle_msg(&self, msg: &[u8]) {
self.handle_msg2(msg);
self.dispatch_futures();

View file

@ -304,3 +304,13 @@ pub fn toggle_float_above_fullscreen() {
pub fn set_show_float_pin_icon(show: bool) {
get!().set_show_float_pin_icon(show);
}
/// Sets a callback to run when this config is unloaded.
///
/// Only one callback can be set at a time. If another callback is already set, it will be
/// dropped without being run.
///
/// This function can be used to terminate threads and clear reference cycles.
pub fn on_unload(f: impl FnOnce() + 'static) {
get!().on_unload(f);
}