leverage lock w and r (clean up after AI)

This commit is contained in:
2026-03-25 02:23:06 +08:00
parent 53248243e3
commit cb0b366e96
3 changed files with 20 additions and 46 deletions

View File

@@ -4,7 +4,7 @@ use tauri_specta::Event as _;
use tracing::warn; use tracing::warn;
use crate::{ use crate::{
get_app_handle, get_app_handle, lock_r, lock_w,
models::app_state::{AppState, NekoPosition}, models::app_state::{AppState, NekoPosition},
services::{app_events::AppStateChanged, neko_positions}, services::{app_events::AppStateChanged, neko_positions},
}; };
@@ -13,12 +13,12 @@ static APP_STATE: LazyLock<Arc<RwLock<AppState>>> =
LazyLock::new(|| Arc::new(RwLock::new(AppState::default()))); LazyLock::new(|| Arc::new(RwLock::new(AppState::default())));
pub fn get_snapshot() -> AppState { pub fn get_snapshot() -> AppState {
let guard = APP_STATE.read().expect("app state lock poisoned"); let guard = lock_r!(APP_STATE);
guard.clone() guard.clone()
} }
pub fn set_scene_setup_nekos_position(nekos_position: Option<NekoPosition>) { pub fn set_scene_setup_nekos_position(nekos_position: Option<NekoPosition>) {
let mut guard = APP_STATE.write().expect("app state lock poisoned"); let mut guard = lock_w!(APP_STATE);
guard.scene_setup.nekos_position = nekos_position; guard.scene_setup.nekos_position = nekos_position;
emit_snapshot(&guard); emit_snapshot(&guard);
drop(guard); drop(guard);
@@ -26,13 +26,13 @@ pub fn set_scene_setup_nekos_position(nekos_position: Option<NekoPosition>) {
} }
pub fn set_scene_setup_nekos_opacity(nekos_opacity: f32) { pub fn set_scene_setup_nekos_opacity(nekos_opacity: f32) {
let mut guard = APP_STATE.write().expect("app state lock poisoned"); let mut guard = lock_w!(APP_STATE);
guard.scene_setup.nekos_opacity = nekos_opacity.clamp(0.1, 1.0); guard.scene_setup.nekos_opacity = nekos_opacity.clamp(0.1, 1.0);
emit_snapshot(&guard); emit_snapshot(&guard);
} }
pub fn set_scene_setup_nekos_scale(nekos_scale: f32) { pub fn set_scene_setup_nekos_scale(nekos_scale: f32) {
let mut guard = APP_STATE.write().expect("app state lock poisoned"); let mut guard = lock_w!(APP_STATE);
guard.scene_setup.nekos_scale = nekos_scale.clamp(0.5, 2.0); guard.scene_setup.nekos_scale = nekos_scale.clamp(0.5, 2.0);
emit_snapshot(&guard); emit_snapshot(&guard);
} }

View File

@@ -8,7 +8,7 @@ use specta::Type;
use tauri_specta::Event as _; use tauri_specta::Event as _;
use crate::{ use crate::{
get_app_handle, lock_r, get_app_handle, lock_r, lock_w,
models::{dolls::DollDto, friends::FriendshipResponseDto}, models::{dolls::DollDto, friends::FriendshipResponseDto},
services::{app_events::FriendActiveDollSpritesUpdated, sprite}, services::{app_events::FriendActiveDollSpritesUpdated, sprite},
state::FDOLL, state::FDOLL,
@@ -29,27 +29,21 @@ pub fn sync_from_app_data() {
let next = build_sprites(&friends); let next = build_sprites(&friends);
let mut projection = FRIEND_ACTIVE_DOLL_SPRITES let mut projection = lock_w!(FRIEND_ACTIVE_DOLL_SPRITES);
.write()
.expect("friend active doll sprite projection lock poisoned");
*projection = next; *projection = next;
emit_snapshot(&projection); emit_snapshot(&projection);
} }
pub fn clear() { pub fn clear() {
let mut projection = FRIEND_ACTIVE_DOLL_SPRITES let mut projection = lock_w!(FRIEND_ACTIVE_DOLL_SPRITES);
.write()
.expect("friend active doll sprite projection lock poisoned");
projection.clear(); projection.clear();
emit_snapshot(&projection); emit_snapshot(&projection);
} }
pub fn remove_friend(user_id: &str) { pub fn remove_friend(user_id: &str) {
let mut projection = FRIEND_ACTIVE_DOLL_SPRITES let mut projection = lock_w!(FRIEND_ACTIVE_DOLL_SPRITES);
.write()
.expect("friend active doll sprite projection lock poisoned");
if projection.remove(user_id).is_some() { if projection.remove(user_id).is_some() {
emit_snapshot(&projection); emit_snapshot(&projection);
@@ -57,9 +51,7 @@ pub fn remove_friend(user_id: &str) {
} }
pub fn set_active_doll(user_id: &str, doll: Option<&DollDto>) { pub fn set_active_doll(user_id: &str, doll: Option<&DollDto>) {
let mut projection = FRIEND_ACTIVE_DOLL_SPRITES let mut projection = lock_w!(FRIEND_ACTIVE_DOLL_SPRITES);
.write()
.expect("friend active doll sprite projection lock poisoned");
match doll { match doll {
Some(doll) => match sprite::encode_doll_sprite_base64(doll) { Some(doll) => match sprite::encode_doll_sprite_base64(doll) {
@@ -88,9 +80,7 @@ pub fn set_active_doll(user_id: &str, doll: Option<&DollDto>) {
} }
pub fn get_snapshot() -> FriendActiveDollSpritesDto { pub fn get_snapshot() -> FriendActiveDollSpritesDto {
let projection = FRIEND_ACTIVE_DOLL_SPRITES let projection = lock_r!(FRIEND_ACTIVE_DOLL_SPRITES);
.read()
.expect("friend active doll sprite projection lock poisoned");
FriendActiveDollSpritesDto(projection.clone()) FriendActiveDollSpritesDto(projection.clone())
} }

View File

@@ -8,7 +8,7 @@ use specta::Type;
use tauri_specta::Event as _; use tauri_specta::Event as _;
use crate::{ use crate::{
get_app_handle, lock_r, get_app_handle, lock_r, lock_w,
models::app_state::NekoPosition, models::app_state::NekoPosition,
services::{ services::{
app_events::NekoPositionsUpdated, app_events::NekoPositionsUpdated,
@@ -48,9 +48,7 @@ pub fn sync_from_app_data() {
guard.user_data.friends.clone().unwrap_or_default() guard.user_data.friends.clone().unwrap_or_default()
}; };
let mut projection = NEKO_POSITIONS let mut projection = lock_w!(NEKO_POSITIONS);
.write()
.expect("neko positions projection lock poisoned");
projection.friend_active_dolls = friends projection.friend_active_dolls = friends
.into_iter() .into_iter()
@@ -71,9 +69,7 @@ pub fn sync_from_app_data() {
} }
pub fn clear() { pub fn clear() {
let mut projection = NEKO_POSITIONS let mut projection = lock_w!(NEKO_POSITIONS);
.write()
.expect("neko positions projection lock poisoned");
projection.self_cursor = None; projection.self_cursor = None;
projection.friend_cursors.clear(); projection.friend_cursors.clear();
@@ -83,18 +79,14 @@ pub fn clear() {
} }
pub fn update_self_cursor(position: CursorPositions) { pub fn update_self_cursor(position: CursorPositions) {
let mut projection = NEKO_POSITIONS let mut projection = lock_w!(NEKO_POSITIONS);
.write()
.expect("neko positions projection lock poisoned");
projection.self_cursor = Some(position); projection.self_cursor = Some(position);
emit_snapshot(&projection); emit_snapshot(&projection);
} }
pub fn update_friend_cursor(user_id: String, position: CursorPositions) { pub fn update_friend_cursor(user_id: String, position: CursorPositions) {
let mut projection = NEKO_POSITIONS let mut projection = lock_w!(NEKO_POSITIONS);
.write()
.expect("neko positions projection lock poisoned");
if !has_friend_active_doll(&mut projection, &user_id) { if !has_friend_active_doll(&mut projection, &user_id) {
if projection.friend_cursors.remove(&user_id).is_some() { if projection.friend_cursors.remove(&user_id).is_some() {
@@ -108,9 +100,7 @@ pub fn update_friend_cursor(user_id: String, position: CursorPositions) {
} }
pub fn remove_friend(user_id: &str) { pub fn remove_friend(user_id: &str) {
let mut projection = NEKO_POSITIONS let mut projection = lock_w!(NEKO_POSITIONS);
.write()
.expect("neko positions projection lock poisoned");
let removed_active_doll = projection.friend_active_dolls.remove(user_id).is_some(); let removed_active_doll = projection.friend_active_dolls.remove(user_id).is_some();
let removed_position = projection.friend_cursors.remove(user_id).is_some(); let removed_position = projection.friend_cursors.remove(user_id).is_some();
@@ -121,9 +111,7 @@ pub fn remove_friend(user_id: &str) {
} }
pub fn set_friend_active_doll(user_id: &str, has_active_doll: bool) { pub fn set_friend_active_doll(user_id: &str, has_active_doll: bool) {
let mut projection = NEKO_POSITIONS let mut projection = lock_w!(NEKO_POSITIONS);
.write()
.expect("neko positions projection lock poisoned");
projection projection
.friend_active_dolls .friend_active_dolls
@@ -135,16 +123,12 @@ pub fn set_friend_active_doll(user_id: &str, has_active_doll: bool) {
} }
pub fn refresh_from_scene_setup() { pub fn refresh_from_scene_setup() {
let projection = NEKO_POSITIONS let projection = lock_r!(NEKO_POSITIONS);
.read()
.expect("neko positions projection lock poisoned");
emit_snapshot(&projection); emit_snapshot(&projection);
} }
pub fn get_snapshot() -> NekoPositionsDto { pub fn get_snapshot() -> NekoPositionsDto {
let projection = NEKO_POSITIONS let projection = lock_r!(NEKO_POSITIONS);
.read()
.expect("neko positions projection lock poisoned");
build_snapshot(&projection) build_snapshot(&projection)
} }