text_view: Use weak references to the state in async task to avoid memory leaks. (#1311)

This commit is contained in:
Sunli 2025-09-30 15:26:59 +08:00 committed by GitHub
parent 880c019fb8
commit 591cfc8dab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -518,17 +518,22 @@ impl Element for TextView {
});
cx.spawn({
let state = self.state.clone();
let state = self.state.downgrade();
async move |cx| {
while let Ok(parsed_result) = rx_result.recv().await {
_ = state.update(cx, |state, cx| {
state.parsed_result = Some(parsed_result);
if let Some(parent_entity) = state.parent_entity {
let app = &mut **cx;
app.notify(parent_entity);
}
state.clear_selection();
});
if let Some(state) = state.upgrade() {
_ = state.update(cx, |state, cx| {
state.parsed_result = Some(parsed_result);
if let Some(parent_entity) = state.parent_entity {
let app = &mut **cx;
app.notify(parent_entity);
}
state.clear_selection();
});
} else {
// state released, stopping processing
break;
}
}
}
})